Asynqueue icon indicating copy to clipboard operation
Asynqueue copied to clipboard

Wrong benchmarking

Open divan opened this issue 10 years ago • 1 comments

Your code for Go uses almost ~250ns per iteration only for concatenation and strconv.Itoa(). You can easily benchmark this using builtin benchmarking, just add the file chanperf_test.go with following content:

package main

import (
    "strconv"
    "testing"
)

func BenchmarkStrconv(b *testing.B) {
    for i := 0; i < b.N; i++ {
        strconv.Itoa(i)
    }
}

func BenchmarkStrconvConcat(b *testing.B) {
    var str = "New"
    for i := 0; i < b.N; i++ {
        _ = str + strconv.Itoa(i)
    }
}

and run go test -bench . to see your results.

So, to not change much of your code, if you really want to measure channels performance you can either preallocate those strings or use, for example, channel of ints, to avoid spending CPU cycles on string conversation and concatenation. With

var qout = make(chan int)
...
for i := 0; i < numMessages; i++ {
    qout <- i
}

you probably will get more fair results.

You probably need to update your C# code also.

By the way, with builtin benchmarking you could create these benchmarking more easily.

divan avatar Jul 25 '15 16:07 divan

Hey, thanks! There's another discrepancy that I found yesterday, too. My C# implementation is basically the equivalent of a buffered Go channel, in that writes in my C# code are async/non-blocking, but in my Go code are synchronous. When I modified C# to match Go's behavior, Go turned out to be more than 2x faster than C#.

Next week, I'll be updating the readme/code accordingly... and also probably switching to Go professionally. :)

chrisdavies avatar Jul 26 '15 17:07 chrisdavies