What makes python slow




















Can we say that Python is slow in these use cases? Every single use case mentioned above requires a huge amount of calculations. Nevertheless, Python continues to be the most popular language used in machine learning or image processing. Now let me explain why. Back in the opening paragraph, I mentioned that Python is often accused of being slow, which is only partially true. If the project uses machine learning, more often than not, it will need to draw on some external libraries, such as SciKit and TensorFlow, both of which are available for Python.

The same is true for image preprocessing OpenCV library. Python is great for creating Web applications. Django is by far the most popular Python Web framework, and it powers some of the biggest apps and websites you know, including Spotify, Washington Post, and YouTube although it was initially built with PHP , and BitBucket.

Furthermore, assuming that the performance of a Web application depends only on the code and chosen language is a huge mistake. The world is actually much more complicated than that. Application performance is driven by a number of factors and properly written and optimized code is just one of them.

Overall performance is also limited by bottlenecks. What are they? Well, the name is rather self-explanatory. I've edited the tone to prevent its re- closure and re- deletion. It amazes me that none of the viewers of this question, many of whom voted up the question or answers, saw fit to dispute the closure reason or act to fix the argumentative tone of the question.

Add a comment. Active Oldest Votes. Mark Byers Mark Byers k gold badges silver badges bronze badges. To be pedantic: Python is not typically compiled to native code at compile time.

Python bytecode still must be interpreted. You haven't really explained why Python implementations tend to be so CPU-hungry. SamB: I've now added a comparison to other interpeted languages to address your point. The part I wrote about abstactions was not to explain why Python is slower to run, but to explain why it can be faster to program.

In retrospect, I think it's worth noting the global interpreter lock and the way all objects in Python are heap allocated objects, even a simple integer. Without getting into specific implementation details, these types of things, aside from the higher level abstractions, can weigh in quite heavily on performance.

That said, I still think most applications should be written primarily in a scripting language like Python. As Knuth states about premature optimization, only small parts of most applications are actually very performance-critical.

Show 3 more comments. Doesn't Unladen Swallow currently use slightly more memory? It sounds like lower memory is a goal, but not achieved yet. The JIT can notice that your code is doing something non-tricky and simple, and can optimize to some simple machine instructions.

Thus PyPy is now much faster than CPython anytime you do something simple in a loop. Better answer than the accepted one, although it's important to note that python is not always compiled.

It is really the implementation. It is interpreted more often than not in my experience. Femaref Femaref Interpreted vs compiled means nothing in terms of optimizability.

JVM and C can be either interpreted or compiled. In addition to being not-exactly-true, this answer doesn't talk about the real problem, which Longpoke explains reasonably well in his answer.

By your definition, Java is a scripting language too? Both languages have some sort of byte-code that is executed in a virtual machine. Only difference is, python compiles on-the-fly when needed, which Java normally doesn't do. Mattias: No; while you are correct that both use bytecode, Java compiles bytecode into native machine language either in advance or with a JIT compiler prior to execution.

In some cases, Java bytecode is the native machine language of certain microprocessors. CPython, on the other hand, is a strict bytecode interpreter. It does all that translation work on the fly, which is why it is often about twice as fast as other Python implementations but still not nearly as fast as Java.

Also CPython does not compile-on-the-fly so much as interpret bytecode on the fly. Typical implementations are going to re-translate the same bytecode over and over, like if you have a loop. That's why it's still considered a bytecode interpreter rather than a compiler. CPython does compile. The Java approach is more of a hybrid and it's not just because of the bytecode, but what it does with it. Okay, we're getting into questions of how you define words.

According to this, java is interpreted or was java. Unless of course, you use psyco with CPython, which generates machine code. Or unless you run Java byte code interpreted, which is also possible. That of course means you can't say something about a specific language without also specifying how the program is executed.

If someone were to put enough effort into it, they might be able to come up with something comparable in performance, but that just typically doesn't happen. When you have a language that's dynamically typed with mechanisms that can only be implemented at runtime like introspection, there is going to be a runtime cost for it and even the best implementers have not been able to make this cost completely negligible.

Maybe some day they'll find a revolutionary new way. Show 7 more comments. Internally, the reason for Python code executing more slowly is that the code is interpreted at runtime instead of being compiled to a native code at compiling time. Why is Python slower than other languages?

Dev Kumar. Previous Page Print Page. Previous Why is iterating over a dictionary slow in Python? Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments.



0コメント