4 ms·
Ha.. 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 Li
by jbooth 17y ago
Ha.. 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...