12 ms·
The result of the distance() function is unused and can be optimized away by the compiler. Runs in 0.04s with LuaJIT 2.0. No type declarations needed. :-)
by mikemike 17y ago
The result of the distance() function is unused and can be optimized away by the compiler. Runs in 0.04s with LuaJIT 2.0. No type declarations needed. :-)
local radius = 6371
local function distance(latA, lngA, latB, lngB)
local latAr = math.rad(latA)
local lngAr = math.rad(lngA)
local latBr = math.rad(latB)
local lngBr = math.rad(lngB)
local deltaLat = latBr - latAr
local deltaLng = lngBr - lngAr
return radius * 2 * math.asin(math.sqrt(
math.sin(deltaLat/2)^2 + math.cos(latAr)
* math.cos(latBr) * (math.sin(deltaLng/2)^2)))
end
local start = os.clock()
for latA=-90,90,2.5 do
for lngA=-90,90,2.5 do
for latB=-90,90,2.5 do
for lngB=-90,90,2.5 do
distance(latA, lngA, latB, lngB)
end
end
end
end
local stop = os.clock()
print(stop-start)
- z8000 17y agoI'm diving into Lua and loving every second of it. Seeing LuaJIT work its magic is wonderful. Thanks for it.
- jbooth 17y agoHa.. of course, the real problem is that Java and Python are actually doing all of the work to compute their trigonometric functions, while C libraries that Lisp uses probably have them mapped into a table. Java does actually keep a table for trig functions on small values -- they just haven't widened the table definition, which is filed as a bug at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5005861 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5005861 "I just performed some simple benchmarks of my own, and noticed that summing e.g. sqrt, sin and tan for small values are just as fast as their C equivalents. This was a great relief to me. However, when computing sin in the range of pi/2 ... pi and 10 ... 10+pi/6 Java became 3-5 times slower, while the C program was just as fast. This is explained above by lack of precision, but for example sin(10.0) yields bit-to-bit exactly the same result in Java and C! This comparison was performed on a rather old 1GHz Mobile Intel Pentium, so I would assume that the precision has been fixed in virtually all modern processors, and therefore these operations could be intrinsified for a much larger value range (possibly the entire double range)."
- Estragon 17y agoJava and Python are actually doing all of the work to compute their trigonometric functions Not true in the case of CPython. It uses the underlying C implementation. See line 270 onwards. http://www.google.com/codesearch/p?hl=en#2T6lfGELm_A/trunk/Modules/mathmodule.c http://www.google.com/codesearch/p?hl=en#2T6lfGELm_A/trunk/M...
- jbooth 17y agoInteresting.. well, I'd think the trig functions would be by far the most expensive part of that program. Is Python really that much slower for control structures? That's literally the only other computation there.
- pbiggar 17y agoWell, every instruction in a Python program requires running through the interpreter loop. Then you have to decode, get the opcode arguments, then actually do the operation. For calling C functions from Python, you also have to extract the C types, call the C function, and wrap the result back up. Its not hugely slow, but yeah, it adds up, and its fairly easy to see why it compares poorly to compiled versions.
- igouy 17y ago>> sqrt, sin and tan for small values are just as fast as their C equivalents << So always use small values? http://shootout.alioth.debian.org/gp4/program.php?test=partialsums&lang=java&id=4#sourcecode http://shootout.alioth.debian.org/gp4/program.php?test=parti...