Accelerating the calculation of a function
Hi list, I am integrating an ODE. I thus have a complicated function that I call a lot of times. What limits my execution times (currently too high) is the call to this function, which happens a great number of times. I therefore need to speed this up. If it where a simple function I could use weave.inline or weave.blitz but this is a rather complicated function involving a lot of python magic that I would be very happy to keep ( things such as : sum( [F(k) for k in lasers]) ). How can I speed this up without rendering my function unreadable ? Is there a compiler (Just in time, maybe) that could speed this up ? Is psyco of any use ? Is their a way to inline my functions (I would like to keep them as functions for the sake of readability) ? Any tricks or ideas appreciated. Cheers -- Gaël
On Sun, 6 Aug 2006 22:40:39 +0200 Gael Varoquaux <gael.varoquaux@normalesup.org> wrote:
Hi list,
I am integrating an ODE. I thus have a complicated function that I call a lot of times. What limits my execution times (currently too high) is the call to this function, which happens a great number of times. I therefore need to speed this up.
If it where a simple function I could use weave.inline or weave.blitz but this is a rather complicated function involving a lot of python magic that I would be very happy to keep ( things such as : sum( [F(k) for k in lasers]) ).
How can I speed this up without rendering my function unreadable ? Is there a compiler (Just in time, maybe) that could speed this up ? Is psyco of any use ?
My tool of choice has been pyrex. (Note that it cannot handle list comprehensions.) Psyco does not understand numpy arrays (read: slow), but it does understand (ie. optimize for) the python array type.
Is their a way to inline my functions (I would like to keep them as functions for the sake of readability) ?
Yes, I hear you. I recently found this bytecodehacks/psyco post: http://groups.google.com/group/comp.lang.python/browse_frm/thread/eab35852b7... Simon. -- Simon Burton, B.Sc. Licensed PO Box 8066 ANU Canberra 2601 Australia Ph. 61 2 6249 6940 http://arrowtheory.com
Gael Varoquaux wrote:
If it where a simple function I could use weave.inline or weave.blitz but this is a rather complicated function involving a lot of python magic that I would be very happy to keep ( things such as : sum( [F(k) for k in lasers]) ).
You're most probably doing it already (I just wondered about the list comprehension): Can you do numpy.sum(F(lasers)) in your function (that is, is 'lasers' a simple array)? Note that numpy.sum (or a.sum()) is much faster then the builtin sum() for large arrays. Also use numpy.amin(<array>) instead of min() and stuff like that. HTH steve -- Random number generation is the art of producing pure gibberish as quickly as possible.
On Mon, Aug 07, 2006 at 05:26:04AM +0200, Steve Schmerler wrote:
( things such as : sum( [F(k) for k in lasers]) ).
You're most probably doing it already (I just wondered about the list comprehension): Can you do numpy.sum(F(lasers)) in your function (that is, is 'lasers' a simple array)?
I have been turning this around in my head wondering whether to do it or not. The problem is that F is already a 2D array, and that I would have to add an extra dimension to it to be able to sum it (and it has matrix and array products in its definition, so writing it starts getting really ugly, and my current problem is rather finding the errors in the different formulas). len(laser) = 6, so this shouldn't be to bad. Actually the best way to implement that sum would a macro (in another language then python). Hint for the numpy developpers: maybe a nice function to make code readable and fast would be to have a function that does this with arrays without having to write F compatible with arrays (ie no "frames are not aligned" problems). Something like: sumover(F,lasers,1) where F would be a callable, lasers an array, and the last argument (optional) the direction of the array in which the sum should happen. I do not know if this is possible but this would be useful. -- Gaël
participants (3)
-
Gael Varoquaux -
Simon Burton -
Steve Schmerler