Is Python slower than Java / C #?

Is Python slower than Java / C #?

performance-comparison-c-java-python-ruby-jython-jruby-groovy

Here is a project that optimizes CPython: unladen-swallow

+58
java performance python c #
Mar 23 '09 at 10:26
source share
13 answers

Do not combine language and runtime.

Python (language) has many runtime implementations.

  • CPython is usually interpreted and will be slower than C # native code. This may be slower than Java, depending on the Java JIT compiler.

  • JYthon is interpreted in the JVM and has the same performance profile as Java.

  • IronPython relies on the same .NET and IL libraries as C #, so the performance difference will be relatively small.

  • Python can be converted to native code through PyREX, PyToC, and others. In this case, it usually also performs C ++. You can - to some extent - further optimize C ++ and perhaps squeeze out a bit better performance than the unoptimized output from PyREX.

    For more information see http://arcriley.blogspot.com/2009/03/so-long-pyrex.html

Please note that Python (language) is not slow. Some Python runtime (like CPython) will be slower than native C ++ code.

+109
Mar 23 '09 at 11:25
source share

It is not entirely correct to ask why Python is slower than Java / C #. How fast is Java? Well, naive translators are about ten times slower than optimized compilers. I believe JavaScript uses a Java byte interpreter, which is probably not very fast. Thus, the proposed question is as follows: "Why is the CPython language system slower than the equivalent environment of Sun, IBM and Oracle JRE and Microsoft.NET?"

I believe that the correct answer is non-technical. The fastest Java and .NET runtimes are faster because they have large technical teams that develop them in a competitive, high-performance environment.

Dynamic language systems are easy to implement. Any idiot can do this. I have. Static language systems are more difficult to develop and implement. A simple static system will run much faster than the equivalent fair dynamic equivalent. However, it is possible that highly optimized dynamic systems work just as fast. I understand that the implementation of Smalltalk was not bad. A frequently cited example of a developed dynamic system is the MIT Lisp Machine .

Also, if real grunting is done by library code, then the language system may not matter. Alternatively, a language can encourage (or give time (!)) To develop more efficient algorithms that can easily erase persistent differences in performance factors.

+54
Mar 23 '09 at 11:30
source share

As mentioned in other answers, this depends on the runtime system as well as the task. Thus, standard (C) Python is not necessarily slower than Java or C #. Some of its modules are implemented in C. Thus, the combination of the speed of its own implementation with the Python language.

We conducted a small experiment: we compared the execution time of factorial calculation in different languages. The test was actually designed to evaluate the performance of implementations of integers of arbitrary accuracy.

 testee.  language arbitrary-precision integers run-time

      1. Java java.math.BigInteger JRE 6.13
      2. .NET System.Numerics.BigInteger MS CLR 4.0
      3. Python long Active Python 2.6.2.2
      4. Squeak BigInt Squeak 3.10.2
      5. .NET Mono.Math.BigInteger MS CLR 4.0

 results:

                  1) 2) 3) 4) 5)
    10,000!  343 ms 137 ms 91 ms 1.200 ms 169 ms
    20,000!  1.480 ms 569 ms 372 ms 1.457 ms 701 ms
    30,000!  3.424 ms 1.243 ms 836 ms 3.360 ms 1.675 ms
    40,000!  6.340 ms 2.101 ms 1.975 ms 6.738 ms 3.042 ms
    50,000!  10.493 ms 3.763 ms 3.658 ms 10.019 ms 5.242 ms
    60,000!  15.586 ms 7.683 ms 5.788 ms 14.241 ms 10.000 ms

alt text http://www.mycsharp.de/wbb2/attachment.php?attachmentid=6909&sid=0d5aa62b522d005d9e7089785b5d19f1

The histogram shows the results. Python is the clear winner. As far as I know, Python uses the Karatsuba algorithm to multiply large integers, which explains the speed.

Additionally, the Python arbitrary integer precision type is an inline long . Therefore, you don’t even need the special type handling that is required for the Java BigInteger class.

+36
Jul 24 '09 at 13:13
source share

Simple - Python is slow .

No matter which interpreter (currently available) you use, it is slower than Java and C. In different tests, it is slower than Ruby and PHP. Regardless of other answers, check and verify yourself.

http://benchmarksgame.alioth.debian.org/u64q/benchmark.php?test=all&lang=python3&lang2=java&data=u64q

Personally, I do not think that a very big contribution and development is to get python faster. Since performance is good in python and it solves some problems directly, speed / performance is not taken seriously. There are some architecture issues that also prevent Python from getting performance tweaks.

Disclaimer This answer is likely to hurt Python lovers. I am also a Python developer, I love developing webapps in Django / Flask / Pyramid, not Spring (Java). But I see in my work and experience how Python works more slowly. Speed ​​is not always my priority. But I support them, who say that Python Interpreter should get grease, grease, or a complete engine change, at least in the marathon. This is the main programming language.

+21
Jul 22 '13 at 15:08
source share

As stated in the comments, you really should provide a test case. The reasons for the performance differences will vary depending on the test being performed.

However, I would suggest that a static and dynamic nature can have much in common with this. For non-virtual calls, JIT-compiled C # / Java is extremely cheap because it can be precisely defined in JIT time. Even virtual calls include only one level of redirection. When snapping becomes dynamic, there is a wider range of issues to consider.

I don’t know enough details about Python to require an understanding of its precise runtime behavior, which I suspect may vary depending on version and implementation. There is such a thing as β€œpython bytecode,” which is then executed by the virtual machine - does this virtual machine actually perform JIT compilation or not. Another thing.

+15
Mar 23 '09 at 10:30
source share

This boils down to the fact that at the compilation stage there is less information to work with, and therefore the runtime should do more work in the case of duck typed (dynamically typed) languages.

Thus, if I make a call to the foo.bar () method, in the case of Java or C ++, the call to bar can be optimized during compilation by opening the type β€œfoo” and then directly calling the method in memory, where the compiler knows that he will be found. Since python or any other dynamically typed language compiler does not know what type the foo object belongs to, it must perform type checking at runtime, and then look up the address of the bar method and then call it.

There are other difficulties that the python writer / compiler encounters, although one of them, we hope, gives adequate guidance. Thus, even with the best compiler authors, statically typed languages ​​are likely to work much better at runtime.

If dynamically typed language ratings are usually under development. Due to fewer lines of code for writing and maintaining, and also not for compiling wait times for developers, development often goes much faster.

+14
Mar 23 '09 at 12:19
source share

What you have is a prime example of writing Java in Python:

  def __init__(self,size): self.first = None last = None for i in range(size): current = Person(i) if self.first == None : self.first = current if last != None : last.next = current current.prev = last last = current self.first.prev = last last.next = self.first 

A bit more pythonic:

  def __init__(self,size): chain = [Person(i) for i in range(size)] self.first = chain[0] chain = zip(chain, chain[1:].append(chain[0])) for p,n in chain: p.next = n n.prev = p 
+9
Mar 23 '09 at 11:47
source share

I think, ultimately, that Python does not do this with optimizations. Most optimization methods that are common relate to static languages. There are optimization methods for dynamic languages, but modern ones don't seem to use them as much as possible. Steve Yegg has a great blog post on the topic .

EDIT : I just wanted to point out that I do not necessarily state that this is critical of Python. I prefer the simplicity of excessive speed on any day.

+6
Mar 23 '09 at 13:53
source share

It has nothing to do with the languages ​​themselves, it's just the fact that the java implementation and time system (JVM) are of very high quality, and that over the years many resources have been spent on stability, scalability and performance.

Compare this to the fact that the CPython implementation has recently been implemented, for example, streaming in the interpreter, which gave it a performance increase of up to 20% for certain problems. This is not very good, it seems to be bad, because such a basic optimization should be there from day one.

+5
Mar 23 '09 at 13:30
source share

This question cannot be answered only by qualitative reasoning; you need good tests to support it. Here is one set that compares Python 3 vs C # Mono and finds Python 3 to 300 times slower. The results of Python and Java are similar. (The usual caveats for interpreting tests apply).

These tests also reported the size of the source code, and Python was significantly shorter than Java and C #.

+3
Jul 22 '09 at 12:58
source share

I think the opposite. I can make a simple program in Python faster than in Java, and these Python scripts are very fast.

Of course, your question without examples is hard to answer. You may have found a slow library, bug, etc. Give us more details.

+1
Mar 23 '09 at 10:33
source share

I would say that the ease and simplicity of writing Python code allows you to write more complex code; for example, code that takes advantage of multi-core processors. Since performance for 1 second has been mostly stagnant for the last 5-10 years, I don’t think it’s clear that Python programs (regardless of whether they run on CPython or something else) are ultimately slower.

+1
Mar 23 '09 at 13:04
source share

Since it is interpreted and not compiled, it should be slower at runtime.

As the table mentioned in the book "Completed" (second edition), p. 600,

C # is equal to C ++ at runtime (1: 1). And Python is slower than hundreds of times than C ++ at runtime (> 100: 1).

And Java is slower than C ++ one and a half times (1.5: 1).

These statistics are average. I do not know who did this research, but it seems interesting.

+1
Apr 26 '09 at 16:55
source share



All Articles