Russell's Blog

New. Improved. Stays crunchy in milk.

Python numerical stuff

Posted by Russell on March 30, 2007 at 6:16 p.m.
A few years ago Eric Hagemann wrote some nice articles for the ONLamp Python Devcenter about numerical programming in Python. Is there any way to encourage him to write more, with more up-to-date modules?
skorsky on April 24, 2007 at 4:37 a.m.

Java Rules! :) Seriously, Hard core work is done in C, as I am sure you know, but the next best thing is using Java. It has a very easy to program GUI module (AWT, Swing) and a slew of numerical libraries out there. I am writing a robotics controller using Java at the moment. I seriously recommend it.

Russell on April 24, 2007 at 5:21 a.m.

Having hacked on a large Java numerical project, I have to disagree. I found it to be very obtuse and pedantic. Swing is, I suppose, a little easier to program than the Windows GUI, but Python and GTK/Glade are an order of magnitude simpler.


C is indeed fast, but I encourage you to actually benchmark your algorithms in different languages. Don't make decisions based on theoretical assumptions when empirical evidence is so easily obtainable. In my own benchmarks, I have found that there is often no advantage whatsoever in writing something in C or Fortran instead of Python. It's sometimes a little shocking that a python script can, for example, numerically integrate a bunch of spherical Bessel functions at almost exactly the same speed as the same operation in C. You won't know unless you benchmark.


In any event, you will note that C and Fortran code can be seamlessly embedded into Python. In fact, one has a choice of several options about how the embedding can be handled. This is how Python code can keep up with C; you can use the fancy C and Fortran libraries without writing any C or Fortran. That alone is worth it.


Modern microprocessors are very good at making dumb code run fast. Instructions are swapped around in the pipeline, and whole branches of the program may be executed speculatively, just in case a test returns a different result next time through the loop. As a result, what you learned in your Algorithms class may be, in actual practice, utterly wrong.


But don't let me rain on your parade. The most important factor in selecting a programming language is your own proficiency. If you like Java, then you should probably use Java.

Ignore this field:
 optional; will not be displayed
Don't put anything in this field:
 optional
Don't put anything here:
Leave this empty:
URLs auto-link and some tags are allowed: <a><b><i><p>.