Is Python slower than Java and how to optimize its performance?

preview_player
Показать описание

Great question by Sonam:

"Is Python slower than Java , and how to optimize its performance?"

Python vs Java — which language is faster and how can you speed up your Python code?

* * *

FREE Python Coding Tutorials & News:
Рекомендации по теме
Комментарии
Автор

1.) It's also worth mentioning Boost.Python. This is a C++ library that allows you to take C/C++ code that you or someone else wrote, include some IDL code that tells the C++ compiler what to give a Python interface, and compile that into a dynamic library. Then imp, importlib, or "ctypes.cdll.LoadLibrary()" can be applied on the Python side to import this dynamic library, and you can run C++ code (e.g. convert between C++ and Python primitives, use functions, use classes, etc). Apparently it's better in certain instances than Cython, because it converts C++ classes into literal Python classes, whereas apparently Cython puts a wrapper around the C++ class before feeding it into a Python class. This option may be better for those who learned C++ first before moving to Python, but honestly a lot of Python people are learning C++ to use Cython, anyway, so this probably is an under-utilized tool.


3.) Learning about the rough O-notation of the Python containers (list, tuple, dict, set, and their standard lib variations) for their look up, insertion, del, and other operations.

Particularly (2) and (3) for Python programmers, I feel like we all really need to internalize these facts. I have definitely not fully done this yet, but I am getting closer to remembering roughly which collections to use in which instances.

GoldPhoenix
Автор

One thing to mention is what you are optimizing, just because the code runs faster doesn't mean it actually will do a real-world difference.

Back in the pre-2010 when I still wrote Java, Swing was the library to use (now I think it's evolved into JavaFX/SwingFX?), and even tho the Java code ran a lot faster then my Python+Qt code, for the end-users Python felt a lot faster because the time it took Qt to draw it on screen was a lot quicker so it felt more responsive. And the same thing is true is with these web-ui frameworks regards to performance and Javascript's "native web-applications" where e.g you have users that complain something is slow, even if you gained 40% more performance on the back-end, it doesn't mean the users will notice due to what's used on the front-end such as template-engine/javascript and all that stuff.

renegadevi
Автор

BTW, Golang, which runs almost as fast as C, has package called grumpy which transcompiles Python 2.7 code. Not tried it yet how it's useful.

TadasTalaikis
Автор

It's not even close, even pypy can't keep up with Java's HotSpot in terms of raw speed

theshermantanker
Автор

Python is interpreted, Its about 100 times slower than Java...

jasonking
visit shbcf.ru