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

Daniel Ehrenberg littledanehren at yahoo.com
Sun Jan 11 15:19:27 EST 2004


Christian Wyglendowski wrote:
> 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

Are you sure that does the same calculation? I would
have done it more like this to optimize it (this is
just the loop):

for i in range(1, intMax, step=4):
    intResult = ((((
        intResult - i)
        + (i+1))
        * (i+2))
        / (i+3))

Getting rid of temporary variables and using range()
should make for a speedup, but I haven't tested it.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus



More information about the Tutor mailing list