1

i am using Dev-c++ version 5.11 and running a c++ code.

 #include <iostream>
 #include <time.h>

using namespace std ;

int main()
{
int start = clock();
for(int x = 0; x <=100000; x++)
{
cout << x << endl;
}
int stop = clock();
cout << endl;
cout << (stop-start)/double(CLOCKS_PER_SEC);
return 0;

}

It is taking 80sec - 75sec on windows 8.1 pavilion g6 2.5Ghz but in python it executes in under 3 seconds how to decrease execution time in c++ code.

Python Code -- (Pycharm python ver.3)

 import time as t
 num = 100000
 start = t.time()
 for z in range(0, num):
     print(z)
 print(t.time() - start)

execution time - 2.95 seconds

Nilay_
  • 19
  • 1
  • 6
  • 3
    And that's what you get for trying to learn C++ from random snippets from the internet. In other words, look up what language features do before using them. (Done properly, the program terminates in .18s on my system.) – Baum mit Augen Apr 01 '17 at 19:44
  • 3
    Note that [`clock`](http://en.cppreference.com/w/cpp/chrono/c/clock) doesn't return an `int`. – Some programmer dude Apr 01 '17 at 19:46
  • Also, are you doing the exact same thing in the Python script? Can you show it too? – Some programmer dude Apr 01 '17 at 19:51
  • Lastly, try removing that `endl` inside the loop. That causes the output to be flushed, and output in the Windows console is really slow. If you want to print a newline, print an actual newline (`'\n'`) instead. – Some programmer dude Apr 01 '17 at 19:52
  • @BaummitAugen How did you speed up the program? On my system it seems to be the console output takes all the time. The loop doesn't take much time at all. – wally Apr 01 '17 at 19:59
  • 2
    The windows cmd console is about as fast as a teletype. – molbdnilo Apr 01 '17 at 20:00
  • @Muscampester Use `'\n` instead of `endl`. Absolute numbers will vary between systems ofc, but not (much) between Python and C++ on the same system. – Baum mit Augen Apr 01 '17 at 20:04
  • Windows console apparently isn't optimized for fast scrolling. See [Why is MSVC compiler weirdly slower than gcc on Linux and Xcode C++ compiler on Mac](http://stackoverflow.com/questions/43041589/why-msvc-compiler-is-wiredly-slower-than-gcc-on-linux-and-xcode-c-compiler-on) – Bo Persson Apr 01 '17 at 20:05
  • @BaummitAugen I could not replicate the result you mention with the change mentioned. When you state *done properly* did you mean *just use linux*? – wally Apr 01 '17 at 20:32

0 Answers0