Can you make this faster?

Christopher T King squirrel at WPI.EDU
Mon Jun 28 02:18:18 CEST 2004


What's the context you're using this in? Consider the following 
possibilities:

Do you actually need to keep the format strings, or do are you just using
them to dump binary data to a file? If it's the latter, then just have the 
function write the data directly, piece by piece, rather than building a 
format string first.

Are you storing data that needs to be readable for other applications, or 
are you just storing data for your own application's (temporary) needs? If 
it's the latter, then consider using pickle or cPickle -- these 
(especially the latter) will be much faster than doing it in Python. Don't 
use them for long-term data storage, though.

If neither of the above apply to you, or give you the speedup you need, 
try using the Psyco JIT compiler to compile the function. After installing 
Psyco, all you need to do is to 'import psyco' and 'psyco.bind(fmtstring)' 
at the top of the file your function resides in.

If that still doesn't help, you could try re-writing the code in pure C (I 
doubt Pyrex would provide much more of a speedup than Psyco in this case). 
Then you can use mutable, fixed-length strings to store the format 
characters in.




More information about the Python-list mailing list