Python vs C++ speed

I'm not asking which language is faster.

Recently, I made two 'printing' programs with Python and C++. Mainly because I wanted to know which language is faster.


speed_test.py

*************

x = 0

while x <= 100000:
print x
x += 1

****************


speed_test.cpp

****************

#include <iostream>

using namespace std;


int main(){
for(int x = 0; x <= 100000; x++){
cout << x << endl;
}

return 0;
}

********************


And I ran these two programs with command prompt. Surprisingly, Python was 3x faster than C++. Do you guys know why this is happening?
Last edited on
Its probably the way that you are printing things with C++. Rather than flushing the output stream every time, just output a new line:
1
2
3
4
5
6
7
#include <iostream>

int main() {
    for (int x = 0; x <= 100000; ++x)
        std::cout << x << '\n';
    return 0;
}

Also, make sure to optimise the C++ code when you compile it, e.g. for clang or g++:
clang++ -O2 -s -o speed_test speed_test.cpp
Are you running C++ in release mode or in debug mode?

Anyway, a language's speed is not proven by printing characters. What exactly happens when you print them can be system-specific, compiler-specific, etc.

If you're really interested in the speed differences, you should look up some well setup benchmarkings on the internet. They'll probably give you a way clearer image!

Also see: http://stackoverflow.com/questions/801657/is-python-faster-and-lighter-than-c
What is clang++? is that another c++ compiler? I'm using g++ with command prompt.

g++ speed_test.cpp -o speed_test.exe

is this Debug mode or Release mode?

And also, still Python is 2x faster even if I use your code...
Last edited on
I've only done a few little programs where I've done comparable things in both Python and C++. Like the others have said, print statements are not a good measure of a language's efficiency. Dynamic languages are inheritantly slower than compiled.

I remember making a large-resolution PNG image of the mandelbrot fractal using both PyPNG and C++ LodePNG libraries. Not only did PyPNG crash at larger resolutions, it was significantly slower than C++ at calculating each pixel. I hadn't tried Python's PIL, but I'm sure it's a similar story.
I understand that compiled languages are faster than interpreted languages. But why is Python faster at this particular situation? is it because Python's print functions requires less processing?

Also, what's the difference between Debug mode and Release mode? and how do I change these options in command prompt using g++?
By "release mode", people mean "turn on optimisations".

To make performance measurements, compile with:
g++ -std=c++11 -O3 -Wall -Wextra -pedantic-errors speed_test.cpp -o speed_test.exe

To enable debug support, compile with:
g++ -std=c++11 -g -Wall -Wextra -pedantic-errors speed_test.cpp -o speed_test.exe
sorry to ask more questions...but what does -std=c++11, -03 and pedantic-errors do?

are they necessary to make my code executable? because I don't want to use too much options...
- -std=c++11 means processing the input files according to the C++11 standard.
- -O3 means the third level of optimisation, i.e. more speed over executable size.
- pedantic-errors means to follow the standard 'pedantically', and throw errors instead of warnings

As for the 'print' statements... generally they are not a good measure of a languages speed, becase normally it is not the generated code that is the bottleneck, but the actual printing process. Also, you'll probably find that the Python code for 'print' is actually written in C anyway.

A better way to test language speed for these kinds of things would be scientific or mathematical number-crunching of some kind.

And yes, clang++ is the C++ frontend of the LLVM compiler. I prefer it to the GNU compiler g++, but they both generally work fine.
-std=c++11 See: https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/C-Dialect-Options.html#C-Dialect-Options

-pedantic-errors https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Warning-Options.html#Warning-Options


> are they necessary to make my code executable?

No.

They are necessary to make sure that a diagnostic is issued for non-conforming program constructs if the International Standard for C++ requires that a diagnostic must be issued for that incorrect construct (and where g++ conforms to this requirement by the standard).


> because I don't want to use too much options...

Most shells support command history and command aliases.
Most shells support command history and command aliases.


Or... you know... use an IDE. =P
GNU make is the first tool on the path from command history to fully integrated build systems.


The "Debug" and "Release" builds are terms used by Microsoft's compiler. It will generate entirely different code in those two modes. The GCC and Clang have a large selection of options for both optimization and debugging that each (may) change the produced code, and rather than deciding "debug OR release" you tell them "how much".
The GCC and Clang have a large selection of options for both optimization and debugging that each (may) change the produced code, and rather than deciding "debug OR release" you tell them "how much".


You can do that with VS, too... the whole Debug/Release interface is just a simplified way to switch between two common build configurations. But you can add additional build configurations and tweak settings for each of them individually.
Topic archived. No new replies allowed.