Python performance

J

JCosta

I did some work in c# and java and I converted some application to Python; I noticed Python is much slower than the other languages.

Is this normal ?
Thanks
 
C

Chris Angelico

I did some work in c# and java and I converted some application to Python; I noticed Python is much slower than the other languages.

Is this normal ?
Thanks

The first thing to look at is the conversion. If you convert idiomatic
Java code into the nearest-equivalent Python, it won't be idiomatic
Python, and it'll probably underperform. (This is especially true if
you create a whole lot of objects, use long chains of classes with
dots, and so on. Java follows dotted name chains at compile time,
Python does at run time.)

Another thing to consider is that Python, while very convenient, isn't
always the fastest at heavy numerical computation. For that, there are
some dedicated libraries, like NumPy, which can do that for you.

But it's also worth checking whether the speed difference even
matters. Are you able to see a real difference, as a human, or is this
just benchmarks? It's not a problem for something to take 5ms in
Python that would take 2ms in Java, if that time is spent responding
to a user's click - the user won't see that difference!

If you post a bit of code, we can help you to see what's going on.
Best bit of code to post would be the slowest - and since you're
talking about performance, you _have_ profiled your code and found
which bit's the slowest, right? :)

ChrisA
 
T

Tim Chase

I did some work in c# and java and I converted some application to
Python; I noticed Python is much slower than the other languages.

Is this normal ?

It depends.

Did you write C#/Java in Python (i.e., use C# or Java idioms in
Python), or did you write Pythonic code?

Check your algorithms and storage classes for performance
characteristics (if you used an O(1) algorithm/container in C#/Java
but used an O(N) algorithm/container in Python) and make sure they
match.

What sorts of operations are you doing? Are you CPU-bound, I/O
bound, or memory-bound? Have you profiled to see where the hot-spots
are?

Personally, I've found that most of my code is I/O-bound (disk or
network), and that very rarely has CPU been much of a problem
(usually checking my algorithm if there's trouble; occasionally I'm
stuck with an O(N^2) algorithm and no language-choice. For some
folks, using one of the specialty-math libraries can speed up numeric
processing. If I know that memory could be an issue, I tend to switch
to a disk-based data-stores to head off any trouble.

-tkc
 
M

Marko Rauhamaa

JCosta said:
I did some work in c# and java and I converted some application to
Python; I noticed Python is much slower than the other languages.

Is this normal ?

Yes. The main reason is the dot notation, which in C through Java is
implemented by the compiler as a fixed offset to a memory structure.
High-level programming languages such as Python implement it through a
hash table lookup.

That's the price of keeping everything dynamic: the structural content
is free to change any time during the execution of the program. I have
heard (but not experienced first-hand) that some ingenious heuristic
optimizations have made Common Lisp code come close to C-style
performance. Google was gung ho about repeating the feat on Python, but
seem to have given up.

The second costly specialty of Python is the way objects are
instantiated. Each object is given a "personalized" dispatch table. That
costs time and memory but is extremely nice for the programmer.

In a word, Python is a godsend if its performance is good enough for
your needs. For other needs, you have other programming languages, and
you buy the performance dearly.

Java is a great programming language, as C# must also be. However, for
the needs where you need to drop out of Python, one must ask if you
weren't better off writing some core parts in C and integrating them
with Python.


Marko
 
J

JCosta

Sábado, 8 de Março de 2014 12:53:57 UTC, JCosta escreveu:
I did some work in c# and java and I converted some application to Python; I noticed Python is much slower than the other languages.



Is this normal ?

Thanks

................

Thanks for the help (Chris, Tim and Marko) and it´s clear now for me ...
 
N

Ned Batchelder

I did some work in c# and java and I converted some application to Python; I noticed Python is much slower than the other languages.

Is this normal ?
Thanks

Your question, and the replies so far in this thread, have overlooked
the difference between language and implementation. Python as a
language has no inherent speed. Your question is really about CPython,
the reference and most-common implementation of the language. It
interprets virtual-machine bytecode, and so will pay a penalty for
compute-bound code.

But PyPy is another implementation of Python. It uses a JIT to produce
native code automatically, and can impressively speed up the execution
of Python programs. You should give it a try to see if it will help in
your situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top