Forums

Cobra vs Python - speed

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Cobra vs Python - speed

Postby Benjamin » Wed Nov 17, 2010 11:52 am

I did a little performance comparison between Cobra and Python. I like the results, so I want to share it with the community.
The test consists of two scripts (Python and Cobra):

Python
Code: Select all
from time import clock

start = clock()
for i in range(200):
    for j in range(200):
        for y in range(200):
            x = i+j+y
duration = clock() - start

input('duration: %s\n\nDone...'%duration)


Cobra
class Speedtest
def main
sw = System.Diagnostics.Stopwatch()
sw.start
for i in 200
for j in 200
for y in 200
x = i+j+y
sw.stop
print 'duration: [sw.elapsed] ms'


Results
Python: 2.5s :lol:
Cobra: 0.0056s :shock:

Conclusion
2.5 / 0.0056 = 446.42
This means 1 Cobra can beat 446 Pythons :P

Maybe this only seems cool to me because I have very little experience with compiled languages.

Benjamin
Benjamin
Benjamin
 
Posts: 8
Location: Holland

Re: Cobra vs Python - speed

Postby torial » Wed Nov 17, 2010 1:23 pm

I'd be interested in some numbers w/ PyPy, Psyco, and IronPython... do you have any of those installed?
torial
 
Posts: 229
Location: IA

Re: Cobra vs Python - speed

Postby torial » Wed Nov 17, 2010 1:30 pm

Also, perhaps change the
x = i + j + k

to
x += i + j + k

And then print out x at the end, so that way no compiler optimizations might affect this (not sure if this is an issue, but I believe it could be since cobra --> C# for compilation).
torial
 
Posts: 229
Location: IA

Re: Cobra vs Python - speed

Postby Charles » Wed Nov 17, 2010 2:15 pm

I'll help you understand the coolness. 8-)

It's always been possible to get those kind of speed ups by using a compiled language such as C++. What's new is getting great performance and clean coding and QA features like unit tests and contracts, all at the same time in one language.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Cobra vs Python - speed

Postby Charles » Wed Nov 17, 2010 2:16 pm

Btw speed comparisons should be done with "python -o" and "cobra -turbo". -turbo turns off extra run-time checks and passes /o to the C# back-end. See "cobra -help" for full details.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Cobra vs Python - speed

Postby Charles » Wed Nov 17, 2010 2:20 pm

torial wrote:I'd be interested in some numbers w/ PyPy, Psyco, and IronPython... do you have any of those installed?

I had two problems with Psyco when I checked it out some number of years ago. One was that it was not stable enough for production code at the time. Another was a more subtle point. Because it was not a main part of the Python project only so many people were developing and testing it.

The thing I like about Mono and .NET is that, like garbage collection, the machine code generation is part of the base package. That means that everyone is flexing it all the time which flushes out bugs which in turn keeps it mature.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Cobra vs Python - speed

Postby Benjamin » Wed Nov 17, 2010 4:26 pm

I updated the Cobra & Python scripts.

I made the total variable a int64 because int, int32, int48 turn into a negative number.
Code: Select all
class Speedtest
    def main
        sw = System.Diagnostics.Stopwatch()
        sw.start
       
        total as int64 = 0
       
        for i in 200
            for j in 200
                for y in 200
                    total += i+j+y
        sw.stop
        print 'total count: [total]\nduration: [sw.elapsedMilliseconds] ms'


cobra -turbo
total count: 2.388.000.000
duration: 0.021 s

This total reference realy influences the benchmarks of cobra. So about the compiler optimizations is probably true.

Python 3.1 -o
total count: 2.388.000.000
duration: 3.375 s

Python 2.6 -o
total count: 2.388.000.000
duration: 2.342 s

Python is slowed down as well: 0.4 seconds.

Some how python 2 runs faster on my machine than python 3.1 ... :?: :?: :?: no clue

About PyPy, Phsyco & IronPython: I tried to install all of them, I didn't get a single one to work.
Benjamin
Benjamin
 
Posts: 8
Location: Holland

Re: Cobra vs Python - speed

Postby gradha » Thu Nov 18, 2010 4:00 pm

2.5 is more mature than 3.1, the developers themselves explained people should expect a slowdown.

Also, sometimes the machine and configuration affects the script. On an old macbook I had a program which ran in about 15 minutes, it featured a lot of array in memory modifications which in compiled languages are ultra fast. When I upgraded to a macbook pro and expected it faster, it also upgraded the base python to 2.6, where they somehow messed the scheduling of the global interpreter lock. The script now run 1 hour, four times slower than an older machine, just because it had two cores which were deadlocking each other.

Then I rewrote part of the script in Cython and now it takes less than 2 minutes to run, precisely the parts where array accesses are being done.

Benchmarks are highly volatile.
Last edited by gradha on Fri Nov 19, 2010 12:34 am, edited 1 time in total.
gradha
 
Posts: 23

Re: Cobra vs Python - speed

Postby torial » Thu Nov 18, 2010 6:33 pm

Benjamin wrote:About PyPy, Phsyco & IronPython: I tried to install all of them, I didn't get a single one to work.


Thanks for trying! I may give it a try if I can get all my headless chicken act issues resolved...
torial
 
Posts: 229
Location: IA

Re: Cobra vs Python - speed

Postby helium » Sat Nov 27, 2010 12:50 pm

On my not that fast machine:

Python 3:
1.7s

IronPython:
1.2s
helium
 
Posts: 14

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 17 guests

cron