Live data from Hacker News

Make Lisp 15x faster than Python or 4x faster than Java

blog.postabon.com

71–80 of 100 posts

Re: Make Lisp 15x faster than Python or 4x faster than Java

#71
post #67

Earlier quoted context omitted.

Incorrect code is silly. It is not unlikely that the different languages are performing a different number of iterations in these tests, correcting that by being precise about the range to be iterated over is not silly. The integer indices here produce the behavior that seems to be intended by the ` The speed difference is negligible since all but the inner multiplication is hoisted out, and each inner loop takes ove…

The speed difference is hardly negligible especially when talking about benchmarks. It's actually about 15% on my OS X laptop, less but still measurable on a Linux server. Your change actually has a greater impact on execution time than off-by-ones in the iteration counts which don't really take place.

15% is a bit higher than I benchmark, but here is a version that is guaranteed to do the correct number of iterations, increments in floating point instead of performing the slow cast, and is faster than performing the comparison in floating point math (because it occupies a different execution unit). Note that it is also less accurate than the cast for most definitions of what this function might be attempting to measure.

http://gist.github.com/288980

Re: Make Lisp 15x faster than Python or 4x faster than Java

#72

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 *…

I'm diving into Lua and loving every second of it. Seeing LuaJIT work its magic is wonderful. Thanks for it.

Re: Make Lisp 15x faster than Python or 4x faster than Java

#73
post #64

Earlier quoted context omitted.

1. There is no C function. What do you call public static float distance(float latA, float lngA, float latB, float lngB) { As I said but that's not a huge deal. Yes it's not a major slowdown, but a tight inner loop with a function call is begging for an inline.

> import java.util.Calendar; might be your first clue... > but a tight inner loop with a function call is begging for an inline. The loop body makes several other function calls, you would be very hard pressed to measure the difference. And if it was a C function in a larger project, you probably wouldn't want to inline it because that would slow your compile times and cause you to lose modularity (i.e. you'd have to…

Yep, sorry, I was not really paying attention and saw what I was expecting to see.

Re: Make Lisp 15x faster than Python or 4x faster than Java

#74
post #47

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 *…

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 "I just p…

  Java 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/M...

Re: Make Lisp 15x faster than Python or 4x faster than Java

#76
post #21

I ran the Python script through Shedskin, a partial Python implementation that compiles Python to machine code via an intermediate layer of C++. I did not have to declare any types for any variables, or add declarations for speed/safety; Shedskin automatically determined the types. The finished program went down in runtime from 533.7s on my machine, to 24.42 seconds. If we adjust for machine speed (since the author's…

Here's the blog: http://shed-skin.blogspot.com/

And the project: http://code.google.com/p/shedskin/

Re: Make Lisp 15x faster than Python or 4x faster than Java

#77
post #47

Earlier quoted context omitted.

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 "I just p…

Java 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/M...

Interesting.. 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.

Re: Make Lisp 15x faster than Python or 4x faster than Java

#79
post #20
post #17

Any guesses as to why the Java implementation is relatively slow? JNI boundary crossings for floating point arithmetic, or lack of use of the math coprocessor?

I don't know, but it's much slower than I expected. It's the latest Java installable by apt on Debian (all these tests are run in Debian running under VMWare). $ java -version java version "1.6.0_0" OpenJDK Runtime Environment (build 1.6.0_0-b11) OpenJDK 64-Bit Server VM (build 1.6.0_0-b11, mixed mode)

Java sin and cos might not be using the machine hardware.

Plain vanilla use of Math.sin and Math.cos = 9.15s Constrain to the range +45 to -45 degrees = 4.88s

As demonstrated 20 months ago http://shootout.alioth.debian.org/gp4/program.php?test=parti...

Re: Make Lisp 15x faster than Python or 4x faster than Java

#80
post #47

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 *…

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 "I just p…

>> 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=parti...

Post reply on HN