[Tutor] Nine Language Performance Round-up: Benchmarking Math & File I/O

Christian Wyglendowski Christian.Wyglendowski at greenville.edu
Fri Jan 9 10:49:28 EST 2004


I suppose that some of you might have looked at the article mentioned in
the subject line.  For those who haven't, the reviewer pits Python,
Python/Psycho, the .NET languages and Java against each other in math
and file I/O operations.  Here is a link to the article:
http://www.osnews.com/story.php?news_id=5602


In the mathematical operations benchmarks, Python does not fair so well
against the other languages.  I know that these sort of ops aren't
Python's strong suit, but I was interested to see if there was anyway to
speed up the stock Python performance in this test.  I only messed with
the integer math function. 

Here is a link to the python code used in the benchmark:
http://www.ocf.berkeley.edu/~cowell/research/benchmark/code/Benchmark.py

Here is my change to it:

def intArithmeticNew(intMax):

    startTime = time.clock()

    i = 1
    intResult = 1
    while i < intMax:
        intResult = intResult - i
        
        intResult = intResult + i
        
        intResult = intResult * i
        
        intResult = intResult / I
        ###########
        i = i + 4 # instead of doing an assignment for each operation,
we now simply do one assignment for all four
        ###########
    stopTime = time.clock()
    elapsedTime = (stopTime - startTime) * 1000 # convert from secs to
millisecs.
    print "Int arithmetic elapsed time:", elapsedTime, "ms with intMax
of", intMax
    print " i:", i
    print " intResult:", intResult
    return elapsedTime

Here are the results on my system running the origial code and my
modified code:
>>> Benchmark.intArithmetic(1000000000)
Int arithmetic elapsed time: 293486.648163 ms with intMax of 1000000000
 i: 1000000001
 intResult: 1
293486.64816338394
>>> Benchmark.intArithmeticNew(1000000000)
Int arithmetic elapsed time: 207132.665464 ms with intMax of 1000000000
 i: 1000000001
 intResult: 1
207132.66546446539

My change yielded close to a 30% speed increase.  I got the idea to
reduce the amount of assignments from a comment on the article.  What
other methods could be used to speed this up?

Christian
http://www.dowski.com



More information about the Tutor mailing list