c++ Beginner issue ( code works but slow)

Pages: 12
My issue is that when I run both problems, starting up both end up with c++ being the loser in this case.. Is this normal? Did I program bad?

I'm thinking about learning more c++ but if its going to be slower than python and take more time I see no need...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    int bacon = 0;

    while(bacon <=1000000)
                {
                cout << bacon << endl;
                bacon = bacon + 1;
                }

    return 0;
}


now the python code in this case is utterly simple


1
2
3
4
b = 1
while b <=1000000:
	print b
	b+=1
Last edited on
cout is probably more expensive to run than print is in python, you can do bacon++ in c++, hence the name, or bacon+=1 too if you prefer that.

yes, python is simpler, but so are many other languages, and python is an interpreted language unlike c++, there isn't much point comparing the two.
well, for one, instead of bacon = bacon + 1; you could just type bacon++;
I've already mentioned that.
Thanks for pointing that out Zeph, many of us can read the same thing twice and not realise they're the same thing; it's good that you're here to point out when two people give the same answer, so that we don't have to realise for ourselves that it's a race condition caused by the nature of the forum.
I know nothing about python, but the main thing that is slowing your application down is the output to console.

You should never need to cout that many times in a row, and even if you did for some strange reason, you can't see more than about 100 lines in a Windows console anyways.

Your test is therefore not practical as it tests a particularly time-intensive process.

Write to a file instead. This is much more practical and faster.
Last edited on
Any time Mosch.
sheesh, sorry Zep. you must have submitted your response while I was typing mine.
Jadax, I already made that clear. :p
Write to a file instead. This is much more practical and faster.

And if you do that, do not flush the buffer after every line. Use '\n' instead of endl.

Not that either is a particularly good indicator, because the code that ends up taking execution time is neither the Python nor the C++ code, but rather the code of the operating system and/or the terminal.

C++ performs significantly better than Python where any sort of number crunching is involved, as is the case with the benchmarks used on this site:
Take this: http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&lang=python3&lang2=gpp
Last edited on
when you say bacon++ do you mean editing the code to put bacon++ in this way?

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int bacon = 0;

    while(bacon <=1000000)
                {
                cout << bacon << endl;
                bacon++;
                }

    return 0;
}


if so, thanks a lot for the bacon++ tip guys :) but It's still slow by 30 seconds :( ( improved however from 40 seconds).

I understand that python is an interpreted language unlike c++.Thats why I'm really confused to see that its moving slower than it does in python.

@ stewbond
Both programs are being ran in a console. If anything running in console would improve the speed, not make it slower right?

True you can only see 100 lines of code.

How do you take the jump from "you cant see more than 100 lines in a console" therefore the test isn't practical? surely, c++ should be outperforming python in anything it do as it is a lower level language than python.

edit: its like saying assembly is slower in processing in some cases but when writing to a text file, it "should" be faster than c++. right? When in fact, assembly should be kicking c++'s butt in every single application.

but nevertheless, im interested! I know how to write to a file but I don't know how to record the time it takes for it complete :( .. Which is why I did a simple test like this, because the same process is happening in both consoles
Last edited on
That is not the behaviour that I'm observing.
Both take almost the same, as is the printing the bottleneck.
Killing the printing (send it to /dev/null) makes c++ 3 times faster. (0.5 against 1.5 seconds)
$ time ./a.out > /dev/null
$ time python2 print.py > /dev/null
However, I don't think that that is fair for python, as I'm counting the time to start the interpreter.

@Moschops: http://cplusplus.com/forum/beginner/65081/#msg351865
well, is that really true? why does the fact the code is lower level mean it should always be faster? if the code is written poorly it should be slower, just look at how you improved the performance of the c++ code by changing a single line, you made it faster and therefore better, I'm sure there are more optimizations that can be made to c++'s code, either your code or cout, why not try replacing << std::endl; with << "\n"; since std::endl; flushes the buffer? that should speed it up, and it also depends on your compiler, after all it it is the compiler that turns c++ into assembly (or directly into binary, whatever its method).
No! No recursive threads!
if so, thanks a lot for the bacon++ tip guys :) but It's still slow by 30 seconds :( ( improved however from 40 seconds).

That just shows that the test is unreliable, because whether you write bacon++ or bacon = bacon + 1; makes absolutely no difference.

its like saying assembly is slower in processing in some cases but when writing to a text file, it "should" be faster than c++. right?

No. In both cases, after preparing the data, a write function provided by the operating system is called, which does the actual work of writing to the file. Needless to say, it cares little about which language it was called from. However, one can still improve performance by buffering more data before making a call to write. It's possible that Python does this particularly well.
Last edited on
why not skip the cout for both languages and just cout when it is done counting?
@ne555

Thanks for your results :)

@athar yeah, I'm thinking that python just does some things well in certain places.

@jadax I'm going to try that. Thats easy enough for a noob like me to do.
That test would be even more stupid. Not to mention that in C++ the loop will be optimized away completely, because it does not do anything. Which leaves a result of 0.00 seconds.

If you want to compare performance, use something that solves an actual problem of the real world.
@jadax

two things I found out was that not only does python take up more memory for the same thing, but that it goes slower when processing about


@ne555

I'm coming up with the same results as you. Having killed the cout function on both of the codes and just having it state when its done, I was able to find out that c++ gets the job done with a fraction of a second while python takes 1.5 seconds longer.

that gap wasn't satisfying for me so I took it to "while bacon 999999999 "and c++ completely smoked python. 3.34 seconds to 134 seconds.

@athar
3.34 seconds to 134 seconds. Bigger numbers in python causes it to slow down... Not what I need for what I am writing :).
3.34 seconds? I suppose you compiled without optimizations (-O3 switch)?
Last edited on
Pages: 12