-8

Why this code is taking 3.87 seconds in C++?

#include <stdio.h>
#include <time.h>

int main() {

    int iterations=999999;
    int size=1000;
    int i,k;

    clock_t tStart = clock();
    for (k=0;k<iterations;k++){
        for(i=0; i<size; i++){
          //ANYTHING (the content is not important)
        }
    }

    printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
    return 0;
}

I am using it on Eclipse on Ubuntu 16. This is the command that Eclipse is using to compile it:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/myexample.d" -MT"src/myexample.o" -o "src/myexample.o" "../src/myexample.cpp"

I tried the same code on Java and it takes only 0.006 seconds.

What am I doing wrong?

Thank you!

EDIT

Sorry I didn't want to say that Java is better than C++. I just expected a better result in C++ and I wanted to know why I was obtaining a bad performance, so I tried the same code in Java.

SOLVED

Using -O3 I get miliseconds.

EDITED AGAIN

Yes, I know that is a nested loop, but don't worry about that. I know what I am doing (the code was simplified to formulate the question, it is much more complex in the full version). The error was in the compiler command. Read @chqrlie answer.

Thank you!

tronic
  • 15
  • 3
  • 2
    Why tag `java` and `c` when talking about `C++`? Please avoid language spamming. – Turing85 Jun 21 '18 at 21:18
  • 3
    Why are you tagging `java` and `c` for your `c++` issues? – Christian Gibbons Jun 21 '18 at 21:18
  • 4
    Because `-O0` tells the compiler to not optimze the code at all. Try `-O2` or `-O3` and see what you get. – NathanOliver Jun 21 '18 at 21:19
  • Java probably optimized your null loop away. – Andrew Henle Jun 21 '18 at 21:19
  • 2
    Downvoting due to the OP not putting forth the effort to formulate the question properly. – EJoshuaS - Stand with Ukraine Jun 21 '18 at 21:19
  • Did you also disable optimizations on Java? Even if you did, JIT might be getting in the way of a good metric. – Silvio Mayolo Jun 21 '18 at 21:19
  • 1
    You've got 2 large nested loops. What did you expect? – emsimpson92 Jun 21 '18 at 21:19
  • Are you building your code with optimizations enabled? – Jesper Juhl Jun 21 '18 at 21:20
  • Without seeing the actual Java code you used, we can't say for sure why it took such a short amount of time. Also see https://stackoverflow.com/q/504103/2891664. – Radiodef Jun 21 '18 at 21:24
  • You’re turning off optimizer – Sailanarmo Jun 21 '18 at 21:26
  • 1
    @EJoshuaS: What was missing from the question or formulated incorrectly? The OP supplied complete source, the compiler command, and reasonable information about the platform. Their question is clear and has an answer. – Eric Postpischil Jun 21 '18 at 21:41
  • The problem with this question is more that it's very hard to see the logic why the OP expects the code to run fast when they specifically have added code that anyone would expect to be slow (the nested loops). It's basically "I'm hitting myself in the head with a brick. Why does my head hurt?" question. A better way to formulate this would be to ask why it's fast in Java and slow in C++ which would at least make more sense. – JJJ Jun 22 '18 at 08:13
  • Sorry @JJJ, but I do not agree with you. I simplified the code to make the question easier to understand, of course, the code was larger than this. I was updating other things inside the "nested loop", but a nested loop have not to be slow if what you are doing is fast. After the change suggested by chqrlie the result was as expected (just a few miliseconds). The question wasn't as hard to understand. I was getting a very bad performance in C++ and that was not normal (3 seconds a nested loop when I update an array haha, sorry but that is not normal). You can read the answer of chqrlie. – tronic Jun 25 '18 at 12:00
  • Then this is an even worse question, because the real problem was not mentioned in the question at all. If you have stuff inside the loops then chqrlie's answer doesn't apply because even `-O3` doesn't optimize the loops away. It's optimizing whatever you're doing inside the loops. – JJJ Jun 25 '18 at 14:01
  • Sorry @JJJ but you are wrong. You can try the code without anything inside and you get 3 seconds. If you update an array inside the loop then you get 3 seconds (the same time). If you use the chqrlie answer (-O3) then you get miliseconds. Then, you are wrong, and I am right. You can try both codes in your preffer IDE, with an array inside and without anything inside and you will understand that the loop content is not important in my question. Just test it and then critize others in forums. – tronic Jun 25 '18 at 23:31
  • I never said that the code doesn't work that way. I said that this is a bad question because you never explained *why* you expect the code to be fast when you obviously have a slow operation in it. But sure, maybe this is a brilliant question and those 9 downvotes are just a glitch. – JJJ Jun 26 '18 at 06:12

1 Answers1

3

You explicitly ask the compiler to generate unoptimized code with -O0. The result is indeed rather slow as the nested loops do not get optimized away. The java compiler probably does that and removes the empty loops, a rather easy optimization. If you could require the java compiler to generate unoptimized byte code and prevent the runtime from JITing it, the result would certainly be even slower.

You can play with Godbolt's Compiler Explorer and see the effect of the optimizer, changing the -O0 to -O1, -O2 and -O3: https://godbolt.org/g/CyWuhB

chqrlie
  • 131,814
  • 10
  • 121
  • 189