Same code but different execution times in C++4.3.2 and C++14.

Problem

I submitted the solution to this problem in C++(gcc-4.3.2) and the execution time was 0.04s

https://www.codechef.com/viewsolution/13190989

I submitted the same code but this time I chose C++14 and the execution time was 0.02s

https://www.codechef.com/viewsolution/13190992

Can someone please explain why the difference in execution time was observed?

Can we infer from this that C++14 is faster than C++(gcc-4.3.2)?

2 Likes

If I were in place of you, I would assume so. (I am not 100% confident, but if you ask me, I would agree with your inference.)

A similar discussion took place here

Quoting something I found useful-

User 1-

There may be many reasons for one compiler version being slower than the other. It may depend on some parameters like the load on the judge server at the time of the submission, the data structure that is used in the submission etc. 

The user above also mentioned something interesting in the answer, you can have a look (its the topmost answer)

User 2-

I observed the behavior when I went deeper into the mechanism of gcc, and reading the dump files it produces during compilation, the gimple and control flow graphs that are created all internally. After a lot of research, it was found that different versions might have different optimizations for the same program.
2 Likes

I also had same type of problem in long long int and int.

yes, if you have older version of compiler or windows or not enough RAM then it take different time on same code.

1 Like

Yes. long long int takes almost twice the time in execution! Too many long long int variables, especially if inside loops/nested loops can cause TLE even at correct algo. So we should use them cautiously!

User1’s answer wasn’t exactly my case as there was no code change in both of my submissions and also there was no load on the judge server at the time I suppose(no active contests) but User2 does give a vague insight about the issue I faced. Would love to know more about this, @vijju123 if you find something more on this, feel free to post it here. Much appreciated.

I will surely post any other relevant material I come across. But telling the gist of what I read and saw, different versions of c++ do differ a bit in terms of memory and time.

I actually checked entire codechef forums for this, and those two quotes were most useful ones. That’s why I quoted the relevant parts of theirs. I even checked quora, and found a very relevant question, but the answer wasn’t satisfactory according to me.

I will surely keep a track of this, and post any relevant info I find. :slight_smile:

In case you still want to check out-

Quora

Codechef

https://discuss.codechef.com/questions/37474/same-solution-submitted-twice-and-execution-time-differs-by-06-points-why

Most of the answers end up on the note that “Internal factors related to codechef servers can also play a role and cause difference in time for even same code. Further, he difference is insignificant and hence can be promptly ignored. We need not worry about it.”

1 Like

+1 for the links :slight_smile:

1 Like