Reputation: 21
I have wanted to make a comparison of some programming languages, and I took the C, the C++ and the Python.
Here is my C++ code of a test loop :
#include <iostream>
int main(){
for(int i=0;i<10000;i++){
std::cout<<i;
}
}
After compiling it with Code::blocks's GCC(I don't think it'll make a big difference, and it's not just a without optimization compiling problem, else it'll just take 1000* less time ), it took around 3 (2 for a C version) seconds to display all(without newline, gives a text soup).
I've made a simple Python version :
for i in range(10000):
print(i,end="")
And it runs in 0.12 seconds.(with sys.write 0.05)
My question is simple : Why my C++ program is so slow ? (relative to this simple Python version)
Upvotes: 0
Views: 530
Reputation: 10825
On Windows, the default behavior is to not buffer stdout if the output is an interactive console. While other platforms do something similar, this coupled with the default Windows console being surprisingly slow, means if you output a lot of text to it, your program will be slow.
Python, on the other hand, defaults to a line-based buffering approach. So that it will only attempt to draw to stdout once when run.
For the Python side, if you run your script with a command like this:
python -u script_name.py
It should take just as long as the C/C++ versions do, roughly because it's doing the same amount of work, as far as the console is concerned.
On the other hand, if you run a C++ program like this:
auto t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000; i++) {
std::cout << i;
}
std::cout << std::endl;
auto t2 = std::chrono::high_resolution_clock::now();
setvbuf(stdout, NULL, _IOLBF, 8192);
for (int i = 0; i < 10000; i++) {
std::cout << i;
}
std::cout << std::endl;
auto t3 = std::chrono::high_resolution_clock::now();
std::cout << "Default behavior: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << std::endl;
std::cout << "Buffered behavior: " << std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t2).count() << std::endl;
This will show the performance hit from using the default buffering mode versus a buffering mode that more closely resembles the Python buffering technique.
On my machine it outputs this at the end, showing more than a 20 time speed up from buffering:
Default behavior: 751
Buffered behavior: 34
Upvotes: 4