This blog post presents the buffered IO speed test I've done with the standard implementation of the programming language Google Go, as compared to C on Linux amd64. The program I've run just copies everything from standard input to standard output, character by character using the default buffered IO. The conclusion: C (with the stdio in glibc) is about 2.68 times faster than Google Go (with its bufio).
$ cat cat.c /* by pts@fazekas.hu at Thu Nov 12 12:04:49 CET 2009 */ #includeint main(int argc, char **argv) { int c; while ((c = getchar()) >= 0 && putchar(c) >= 0) {} fflush(stdout); if (ferror(stdin)) { perror("error reading stdin"); return 1; } if (ferror(stdout)) { perror("error reading stdin"); return 1; } return 0; }
$ cat cat.go // by pts@fazekas.hu at Thu Nov 12 11:56:06 CET 2009 package main import ( "bufio"; "os"; "syscall"; "unsafe"; ) func Isatty(f *os.File) bool { const TCGETS = 0x5401; // Linux-specific var b [256]byte; _, _, e1 := syscall.Syscall( syscall.SYS_IOCTL, uintptr(f.Fd()), uintptr(TCGETS), uintptr(unsafe.Pointer(&b))); return e1 == 0; } func main() { r := bufio.NewReader(os.Stdin); w := bufio.NewWriter(os.Stdout); defer w.Flush(); wisatty := Isatty(os.Stdout); for { c, err := r.ReadByte(); if err == os.EOF { break } if err != nil { panic("error reading stdin: " + err.String()) } err = w.WriteByte(c); if err != nil { panic("error writing stdout: " + err.String()) } if (wisatty && c == '\n') { err = w.Flush(); if err != nil { panic("error writing stdout: " + err.String()) } } } }
Compilation and preparation:
$ gcc -s -O2 cat.c # create a.out $ ls -l a.out -rwxr-xr-x 1 pts pts 4968 Nov 12 11:59 a.out $ 6g cat.go && 6l cat.6 # create 6.out $ ls -l 6.out -rwxr-xr-x 1 pts pts 325257 Nov 12 11:55 6.out $ dd if=/dev/urandom of=/tmp/data bs=1M count=256 $ time ./6.out /dev/null # median of multiple runs ./6.out < /tmp/data > /dev/null 15.31s user 0.16s system 99% cpu 15.497 total $ time ./6.out /dev/null # median of multiple runs ./a.out < /tmp/data > /dev/null 5.92s user 0.20s system 99% cpu 6.153 total
Update: My naive implementation of zcat (gzip decompressor) in Google Go is only about 2.42 times slower than the C implementation (gcc -O3), and the C implementation is about 5.2 times slower than zcat(1).
No comments:
Post a Comment