Finding maximum value in Python vs. C++

Python is an interpreted language. Python has to read the text file with the python code, parse it, and only then begin executing it.

By the time the C++ code executes, the C++ compiler already did all the heavy lifting of compiling C++ into native machine language code that gets directly executed by the CPU.

It is possible to precompile Python code; this’ll save some overhead, but the C++ code will still get the benefit of C++ compile-time optimization. With a small array size, an aggressive C++ compiler is likely to unroll the loop, and maybe even compute the maximum value at compile time, instead of at runtime; so all you end up executing is:

cout << "Biggest number: " << 89 << endl;

This is something that, theoretically, Python can also do; however that’ll take even more CPU cycles to figure out, at runtime.

Leave a Comment