As Jan said "apples and oranges", in this case comparing *os.Stdout with C File *stdout is not fair. The equivalent of *os.Stdout in C is the filedescriptor 1 (STDOUT macro), and the equivalent of *os.Stdout.Write is write(2) (the syscall), not fwrite or fputs. If you retry your microbenchmark using the syscall write(2) with filedescriptor 1, you'll probably see very similar (if not identical) results.
Personally I prefer the explicit buffering proposed by the Go standard library, rather than the implicit one of the C stdio.h header. When I was starting using C, I've already been bitten by the fact that my fwrites were being implicitly buffered, until I discovered the setbuf(3) function and read the documentation. Having different default buffering schemes depending on the output used (yes stderr is not buffered by default), I find more confusing. On Sun, 20 Dec 2020 at 21:32, ben...@gmail.com <benh...@gmail.com> wrote: > And os.Stdout (and friends) are all regular *os.File objects (which as Jan > said, don't buffer). It was non-intuitive to me that stdout didn't buffer > by default, because it's such a bad thing for efficiently writing lots of > output, but I guess it makes sense when you want terminal output to appear > right away. So I realized it made sense, and gives you more control. And > it's so easy to wrap it in a bufio.NewWriter() ... Flush() if you need > buffering. > > I ran into this exact same issue when implementing GoAWK ... a 10-line fix > gave me a 10x speedup. > https://github.com/benhoyt/goawk/commit/60745c3503ba3d99297816f5c7b5364a08ec47ab > > -Ben > > On Monday, December 21, 2020 at 12:27:43 AM UTC+13 arn...@gmail.com wrote: > >> Ah, that is it, thank you! >> >> On Sunday, 20 December 2020 at 11:06:05 UTC Jan Mercl wrote: >> >>> On Sun, Dec 20, 2020 at 11:53 AM Arnaud Delobelle <arn...@gmail.com> >>> wrote: >>> >>> > TLDR; a simple test program appears to show that Go's (*os.File).Write >>> is 10x slower than C's fputs (on MacOS). >>> >>> Apples and oranges. fputs buffers, os.File does not. Rewrite the >>> benchmark using bufio. >>> >> -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/909fa4da-1c74-4c33-98c1-185e6bae9d40n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/909fa4da-1c74-4c33-98c1-185e6bae9d40n%40googlegroups.com?utm_medium=email&utm_source=footer> > . -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGjxhKkJHf1BoKFD%2BDvkhJkjS0crC94PJpffW3wbvaZw6fpmfA%40mail.gmail.com.