Skip to content

Instantly share code, notes, and snippets.

@5kg
Last active December 20, 2015 09:39
Show Gist options
  • Select an option

  • Save 5kg/6109197 to your computer and use it in GitHub Desktop.

Select an option

Save 5kg/6109197 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 500000; ++i) {
fprintf(stdout, "this is a test from error");
}
return 0;
}
package main
import "fmt"
func main() {
for i := 0; i < 500000; i++ {
fmt.Printf("this is a test from error");
}
}
real 0m0.054s
user 0m0.017s
sys 0m0.037s
0 2500001 12500000 output
real 0m1.861s
user 0m0.230s
sys 0m1.627s
0 2500001 12500000 output
#!/bin/bash
gcc c_printf.c -O2 -o c_printf
go build go_printf.go
time ./c_printf > output
wc output
time ./go_printf > output
wc output
@xiang90
Copy link
Copy Markdown

xiang90 commented Jul 30, 2013

The comparable c version should be

include <stdio.h>

int main()
{
int i;
for (i = 0; i < 500000; ++i) {
fprintf(stdout, "this is a test from error %d", i);
fflush(stdout);
}
return 0;
}

Go printf do not have buffer, it will write to fd each time you call it.
If you want to use bufio, refer http://golang.org/pkg/bufio/.

@xiang90
Copy link
Copy Markdown

xiang90 commented Jul 30, 2013

package main

import "fmt"
import "os"

func main() {
for i := 0; i < 500000; i++ {
fmt.Fprintf(os.Stdout, "this is a test from error %d", i);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment