Pretty Interesting C++ Behavior

Правка en3, от ldyllic, 2022-06-09 22:28:21

Hello, CodeForces!

I have just solved 1 problem and I made quite a lot new observations for me. Could anybody explain me, please, the reason behind the behaviors that I will explain here? Thanks a lot!

That is the problem — nothing outstanding, simple implementation 1575J - Jeopardy of Dropped Balls. However to be able to help me out, you will need to get into this problem to see what I will state below :)

A bit of story: while doing CP I was often wondering about the engines behind the code, how and what executes, what takes care of what etc. Hence, I decided to make some experiments on this problem.

First code: 160046810 — [1980ms]. In a while loop I access an element with parameter 2 at the else statement. This parameter just increases x and moves forward, while 2 first statements increase / decrease y + change an element in a matrix. Container for a data is 0-indexed.

Second code: 160047806 — [483ms]. Here in a while loop I access an element with parameter 2 at the first if statement and other statements where I need to change matrix elements follow after. Array is still 0-indexed.

Third code: 160047849 — [280ms]. Everything is similar to the second code, but array is 1-indexed.

I am mostly interested in a reason of difference between 1st and 2nd code, but if you could point out the same between 2nd and 3rd — that will be amazing!

Теги c++ behavior, runtime

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский ldyllic 2022-06-10 11:27:46 31
en3 Английский ldyllic 2022-06-09 22:28:21 20
en2 Английский ldyllic 2022-06-09 22:17:58 3 (published)
en1 Английский ldyllic 2022-06-09 22:16:40 1477 Initial revision (saved to drafts)