You may want to use time.Sleep instead for these purposes
If the loop runs 5 instructions 1e9 times in 2 seconds, that's 2.5e9
instructions in one second, or one instruction every 0.4 nanosecond.
The speed of light is about 299.7M meters per second which is vaguely
29.97 cm per nanosecond, so while the CPU executes one instruction
light has traveled about 29.97 * 0.4 = ~12cm. Thanks to the CPU
designers for that speed, and to the Go designers for not screwing up
a simple operation.
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/plus
http://niemeyer.net/twitter
http://niemeyer.net/blog
-- I never filed a patent.
On the other hand, we could find, due to various microbenchmarks
focusing on compute bound operations, that Go is currently about 3 times
slower than C and C++. And, indeed Java.
For example, I have a number of programs computing Pi by quadrature (cf.
the Bazaar branch at http://www.russel.org.uk/Bazaar/Pi_Quadrature),
which is an embarrassingly parallel problem due to the commutativity and
associativity of addition.
In the sequential case the 10⁹ loop takes, on my twin 2.33GHz Xeon
machine running Debian Testing, about 8s in C and C++, about 9s in Java
and about 22s in Go. I have not run these to create statistically
significant results, but this could be done — I am confident the
anecdotal numbers presented here would be borne out.
Moreover, C, C++, and Java (using the appropriate parallelism frameworks
for the language) show near to optimal linear scaling with processor
count. Go using Goroutines shows clear speedups, indeed it looks fairly
linear except that it is not the near optimal linearity C, C++ and Java
achieve. There is clearly an overhead in the goroutine system affecting
the measure, i.e. the slope is not 1.
I appreciate that the core Go development team have eschewed worrying
about optimization of code generation and other optimization issues, but
in doing so they are missing appealing to those C and C++ folk who have
compute bound tasks.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel...@ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@russel.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Lua still requires the virtual machine to execute. Try LuaJIT which is
likely to optimize the loop to native code. You're basically comparing
a CPU to a virtual machine, which doesn't make all that much sense =)
just a quick note (note that it makes any difference this time AFAICS):
it looks as if you're looking at the output of 6g -S. this is not
the final assembly code, as the linker does a reasonable amount
of optimisation itself. the output of 6l -a is what you want.
-rob