[Tutor] how to optimize this code?

Albert-Jan Roskam fomcl at yahoo.com
Sun Mar 27 21:57:24 CEST 2011


Hello,

I made a program that reads spss data files. I ran cProfile to see if I can 
optimize things (see #1 below).
It seems that the function getValueNumeric is a pain spot (see #2 below). This 
function calls a C function in a dll for each numerical cell value. On the basis 
of this limited amount of info, what could I do to further optimize the code? I 
heard about psyco, but I didn't think such tricks would be necessary as the 
function spssGetValueNumeric is is implemented in C already (which should be 
fast).

1 ## cProfile output
         50018706 function calls in 521.261 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000  521.261  521.261 <string>:1(<module>)
      247    0.001    0.000    0.001    0.000 <string>:1(fileno)
        1    0.275    0.275    0.436    0.436 SPSSio12.py:102(loadSavFile)
        1    0.000    0.000    0.000    0.000 
SPSSio12.py:125(getNumberofVariables)
      200    0.002    0.000    0.004    0.000 SPSSio12.py:132(getVarNameAndType)
        1    0.001    0.001    0.005    0.005 SPSSio12.py:140(getVarInfo)
        1    0.000    0.000    0.000    0.000 SPSSio12.py:150(getNumberofCases)
      100    0.002    0.000    0.002    0.000 SPSSio12.py:157(getVarHandle)
 10000000   96.090    0.000  103.074    0.000 SPSSio12.py:177(getValueNumeric)
      100    0.002    0.000    0.002    0.000 SPSSio12.py:206(getVarPrintFormat)
 10000000   57.937    0.000   57.937    0.000 SPSSio12.py:224(formatValue)
        1    0.007    0.007    1.403    1.403 SPSSio12.py:260(getFileReport)
 10000002  313.049    0.000  486.674    0.000 SPSSio12.py:270(savReader)
        1   34.181   34.181  521.261  521.261 SPSSio12.py:349(main)

## most time consuming function

 def getValueNumeric(fh, spssio, varHandle):
    numValue = ctypes.c_double()
    numValuePtr = ctypes.byref(numValue)
    retcode = spssio.spssGetValueNumeric(fh,
                               ctypes.c_double(varHandle),
                               numValuePtr)

    return retcode, numValue.value

Thanks in advance for your thoughts!

Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have the 
Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110327/cceb2c6a/attachment.html>


More information about the Tutor mailing list