| | |
Testing the Psyco Module to Speed Up Python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
The psyco module has been around for a while and has been used to speed up the Python interpreter. For those of you who think speed is all important, take a look at a typical example. Psyco is similar to Java's just in time compiler.
How does psyco do it? Well, Python normally compiles source code to byte-code. Byte-code is machine code for the Python Virtual Machine (PVM). Byte-code is cross platform and works on many different CPU types. The interpreter then takes the byte-code and translates it to CPU specific machine code for execution of the program.
Psyco works only on Intel i386 CPU machines, as it compiles much of the Python source code directly to i386 machine code. This takes the load off the interpreter and speeds things up about 2 to 100 times.
How does psyco do it? Well, Python normally compiles source code to byte-code. Byte-code is machine code for the Python Virtual Machine (PVM). Byte-code is cross platform and works on many different CPU types. The interpreter then takes the byte-code and translates it to CPU specific machine code for execution of the program.
Psyco works only on Intel i386 CPU machines, as it compiles much of the Python source code directly to i386 machine code. This takes the load off the interpreter and speeds things up about 2 to 100 times.
# time a function using time.time() and the @ function decorator # uses Psyco to speed up code (on my computer 1.6 sec with Psyco, 4.4 sec without) # download psyco-1.4.win32-py2.4.exe from # http://psyco.sourceforge.net/ # tested with Python24 vegaseat 22aug2005 import time from psyco import full full() def print_timing(func): def wrapper(*arg): t1 = time.time() res = func(*arg) t2 = time.time() print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0) return res return wrapper # declare the @ decorator just before the function, invokes print_timing() @print_timing def getPrimeList(n): """ returns a list of prime numbers from 2 to < n using a sieve algorithm""" if n < 2: return [] if n == 2: return [2] # do only odd numbers starting at 3 s = range(3, n+1, 2) # n**0.5 is slightly faster than math.sqrt(n) mroot = n ** 0.5 half = len(s) i = 0 m = 3 while m <= mroot: if s[i]: j = (m*m-3)//2 s[j] = 0 while j < half: s[j] = 0 j += m i = i+1 m = 2*i+3 return [2]+[x for x in s if x] if __name__ == "__main__": print "prime numbers from 2 to <10,000,000 using a sieve algorithm" print "(speed improved with the psyco module)" primeList = getPrimeList(10000000) time.sleep(2.5) """ my output --> prime numbers from 2 to <10,000,000 using a sieve algorithm (speed improved with the psyco module) getPrimeList took 1650.000 ms """
Similar Threads
- What does the sys module do in python? (Python)
- python with MySQLdb module (Python)
- Python unit testing framework (Python)
- python and mySQLdb module (Python)
- Testing DCOM APIs using Python (Python)
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime print program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython



