C++ Is 9.4 Times Faster Than Python in Prime Number Test
learncplusplus.org
learncplusplus.org
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
I wonder how you can characterize the difference in speed between the two languages. I think I read somewhere that in the reference implementation of ruby, calling a method in the general case can require several hash table lookups to find the implementation associated with the name of the method. Is that mostly the same in python as well?
Right; here is a recent scientific publication which confirms about factor 50 speed-down from C++ to Python for the given micro benchmarks: https://authors.elsevier.com/a/1cP%7ELc7X4-XPM;
see table 4. The more time Python spends in native procedures, the more favorable the ratio is for Python; this is probably the reason why only around factor ten was measured here.
I added 3 lines to the Python code and got it 10x faster:
(venv) $ python prime.py
Number of primes between 0 to 1000000 = 78498
Elapsed time: 6.502481937408447
(venv) $ python prime.py
Number of primes between 0 to 1000000 = 78498
Elapsed time: 0.6258630752563477
Diff: 3c3,5
<
---
> import numba
>
> @numba.njit
14a17
> @numba.njitMy concern is that a particular mathematical function can be expressed in many different ways using Python and using C++. And practically speaking, it's impossible to be sure that you've created the fastest-possible implementation of that function in either language.
And even then some implementations' performances are dependent on the compiler, supporting libraries, and underlying processor architecture.
So AFAIK a headline like this leaves out way too many details to be generally true. Am I missing something?
I guess the article is more trying to make a statement along the lines of "here's how you benefit if you know python and pretend that means you know CPP too", (which seems like a fairly valid way to suck people into learning CPP to me)
https://github.com/adsharma/py2many
I didn't write the python -> C++ transpiler, but added a few features to common and added basic support in 6 other languages.
It should be possible to generate the C++ from python.
https://github.com/adsharma/py2many/tree/main/tests/expected
is a good place to understand what types of code this system can transpile.
Most languages are using templates/generics/auto etc. I added some type inference capabilities to common code and updated dart to take advantage of it.
Should be possible for other languages as well.
btrees python -> C++/rust seems feasible as a point of comparison.
Hard problem: translating library calls from one language to another. I'm thinking of some type of a plugin system that app developers can provide mappings.
What if you could write python and generate C++?
Using C or C++ for some domains would be painful, but regarding speed, memory usage or energy usage those languages tend to be the best. If only Rust would be less C++ like (I prefer "RISC" like languages)
from sympy import primepi
primepi(1e6)
which returns instantly.https://github.com/sympy/sympy/blob/219993505f88a8d6f825db1d...
https://web.archive.org/web/20010606131022/http://www.bagley...