Wow. Just wow.

I'm working on a script that evaluates probably millions of trig functions in a given run (it's a geometric ray trace of an arbitrary surface, so the math gets nasty fast...). I've been wrapping the numpy sin, cos, etc to work with the functions I get from mathematica, and it takes about 12 hours for me to process a few thousand data points in this way.

I just rewrote my wrapper script to use the math.py modules, and wow. That 12 hours just dropped to 3. I've always felt numpy to be superior, but I just got a good lesson in considering applications. The traces are running 4 times faster with the basic imports. While I lose the ability to handle arrays, I don't really care :D The code uses a differential approximation that isn't well suited for arrays anyway (as for as I know...)

So yeah...for applications where speed counts, (*cough* don't use python! :D *cough*), but if you must, math > numpy!

Nice info, thanks.

Have you tried the module psyco, a JIT compiler for i386 machines?
It compiles to i386 code directly rather than bytecode virtual code.

Here is a typical example, I think vegaseat posted that somewhere:

# time a function using time.time() and a decorator
# uses Psyco JIT i386 compiler to speed up code
# (on my computer 1.2 sec with Psyco, 5.3 sec without)
# download and install psyco-1.6.win32-py25.exe
# from: http://psyco.sourceforge.net/

import time
from psyco import full

# this will apply psyco just in time compile to full code
# (comment out to run the test without psyco)
full()

def print_timing(func):
    def wrapper(*arg):
        t1 = time.time()
        res = func(*arg)
        t2 = time.time()
        print '%s took %0.3fms' % (func.__name__, (t2-t1)*1000.0)
        return res
    return wrapper

# declare the @ decorator just above the function
# invokes print_timing()
@print_timing
def getPrimeList(n):
    """
    returns a list of prime numbers from 1 to < n using a sieve algorithm
    """
    if n < 2:  return []
    elif n == 2: return [2]
    # do only odd numbers starting at 3
    s = range(3, n+2, 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]


print "prime numbers from 2 to <10,000,000 using a sieve algorithm"
print "(speed improved with the psyco module)"
primeList = getPrimeList(10000000)
print "test print the first 20 primes ..."
print primeList[:20]

Another tidbit, Python25 is faster than Python30.

Wow I thought pysco was for only speeding debugger when I used That Old friend eclipse and pydev. Thanks all for info

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.