The cost of counting: The silliest optimization…

time to read 1 min | 162 words

For a lecture that one of the guys is giving, we started to talk about the approach to take to explain a certain feature (distributed counters).

I suggested that we’ll measure how fast a single thread can count, which resulted in the following code:


On my machine, this results in 25,943,321 in 00:00:01.0000060. Which is great, but seems to be very low. So we set out to see how fast we can count things. Which is about the silliest thing ever. However, it is fun.

It is obvious that the cost here is the Stopwatch calls. So let’s try reducing them. I changed it so it will only check every so often.

This one gives us 205,840,000 in 00:00:01.0010240, so that is much nicer. Can we do better? How about is we changed how we check?

This give me 644,081,297 in 00:00:01.0005354. And I don’t think that we can do better than this very easily.