python performance

Padraig Brady padraig at linux.ie
Sun Sep 15 15:57:27 EDT 2002


I was wondering about the performance characteristics
of python and ran a simple test. The 2 programs
below are functionally equivalent and just read
in fields into a list. file.fields contains
720 fields in each of 405 lines of the form of
repeating: oneoneone twotwo "thre e three"

The time to run is shown above each program
from which I've inferred the following:

1. The function call version is (6.3%) faster because
    the cumulative cost of parsing the simpler expressions
    and function call overhead is smaller than parsing the
    1 single complex expression? Or the function is parsed
    only once and doesn't have to be reparsed. This would
    suggest that top level code is parsed for each iteration?

2. Anyway I thought that parsing affects would be removed by doing the
    parsing only once, i.e. compiling the code to .pyc (I used
    py_compile.compile()). However this makes no difference at all?
    Surely compiling is not just for code obfuscation.

Note I did do the test several times and averaged the results.

cheers,
Pádraig.

----------------------------
2.514s
----------------------------
#!/usr/bin/env python2.2
import re

reFieldFinder = re.compile('[^ "]+|"[^"]+"') #unquoted|quoted
def getFields(line):
     fields = reFieldFinder.findall(line)
     return [field.replace('"', '') for field in fields]

for line in open("file.fields").readlines():
     listLine = getFields(line[:-1])

----------------------------
2.672s
----------------------------
#!/usr/bin/env python2.2
import re

reFieldFinder = re.compile('[^ "]+|"[^"]+"') #unquoted|quoted
for line in open("file.fields").readlines():
     listLine = [field.replace('"', '') for field in 
reFieldFinder.findall(line[:-1])]




More information about the Python-list mailing list