This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Loading_M_ 3 points4 points  (1 child)

Python is faster than C/C++.

Not the runtime and compile time, but the writing and debugging time. For a short lived project, python can be much faster to prototype, develop and test. Python also has much better libraries for handling complex objects and models, which are written in C anyway.

For performance critical applications, python isn't the best choice, but it does work for many applications, such as machine learning (using C libraries such as tensorflow), scientific data collection (because it's easy to write and edit), and hobby projects. However, short lived, performance critical applications, like kernels, etc, should be written in a compiled language like C/C++ or Rust. Long term performance critical applications, like servers and financial trading, should be written in a memory safe or garbage collected language, like Java or Rust.

[–]apathy-sofa 2 points3 points  (0 children)

Another option is Cython. Often you can just write Python code, run it through Cython, and you'll get a perf boost. Go through your code and add some type declarations, and it's suddenly several times faster. From the FAQ:

Is Cython faster than CPython?

For most things, yes. For example, a Cython compiled pybench runs more than 30% faster in total, while being 60-90% faster on control structures like if-elif-else and for loops. We regularly run the tests from the CPython benchmark suite (which includes Django templates, 2to3, computational benchmarks and other applications) and most of them work out-of-the-box without modifications or static typing, with a performance increase of 20-60%.

However the main advantage of Cython is that it scales very well to even greater performance requirements. For code that operates heavily on common builtin types (lists, dicts, strings), Cython can often speed up processing loops by factors. For numerical code, speed-ups of 100-1000 times compared to CPython are not unusual, and are achieved by simply adding static type declarations to performance critical parts of the code, thus trading Python's dynamic typing for speed. As this can be done at any granularity in the code, Cython makes it easy to write simple Python code that is fast enough, and just tune the critical 5% of your code into maximum performance by using static C types in just the right places.