How Does the Order of IF Statements Improves the Runtime

Revision en3, by ldyllic, 2022-07-20 13:10:34

For a great while of my CP career I had no idea how such, at a first glance, insignificant things might affect the runtime of the code. And till this day I keep seeing many beginners having lack of knowledge about this effect, that's why I would like to share it here and hope it'll help you out someday.

First code: 164992399 — [1918ms]. 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.

Second code: 164991769 — [280ms]. 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.

You might not want to get in depth with all stuff written above, just notice 2 drastically different runtimes. The reason for that is pretty straightforward: in this code one of three IF statements gets a true response way more often than others. While the first code has an IF statement with 2 at the end, the second code has such IF statement at the beginning.

That is why you might want to dive into such things into your code when you get a TLE verdict or you feel you might hit the bound of some problem.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en31 English ldyllic 2022-07-21 09:57:51 15
en30 English ldyllic 2022-07-21 09:56:00 28
en29 English ldyllic 2022-07-20 17:55:54 16
en28 English ldyllic 2022-07-20 17:28:15 3 Tiny change: 'ng where is better' -> 'ng where it is better'
en27 English ldyllic 2022-07-20 17:18:46 0 (published)
en26 English ldyllic 2022-07-20 17:18:28 267 (saved to drafts)
en25 English ldyllic 2022-07-20 16:47:18 19 (published)
en24 English ldyllic 2022-07-20 16:46:56 28
en23 English ldyllic 2022-07-20 16:06:12 54
en22 English ldyllic 2022-07-20 16:05:14 50
en21 English ldyllic 2022-07-20 15:46:04 4
en20 English ldyllic 2022-07-20 13:40:00 1 Tiny change: 'nt with ==2). While ' -> 'nt with == 2). While '
en19 English ldyllic 2022-07-20 13:39:43 2 Tiny change: 'ment with 2). While ' -> 'ment with ==2). While '
en18 English ldyllic 2022-07-20 13:38:49 22 Tiny change: 'han others. While th' -> 'han others (IF statement with 2). While th'
en17 English ldyllic 2022-07-20 13:33:13 3 Tiny change: ' you hit (feel you m' -> ' you hit (or feel you m'
en16 English ldyllic 2022-07-20 13:32:12 34
en15 English ldyllic 2022-07-20 13:31:14 14 Tiny change: 'y green _Accepted_ word. \n' -> 'y green _ACCEPTED_ word. \n'
en14 English ldyllic 2022-07-20 13:28:48 342
en13 English ldyllic 2022-07-20 13:22:37 17
en12 English ldyllic 2022-07-20 13:22:05 194
en11 English ldyllic 2022-07-20 13:21:24 8
en10 English ldyllic 2022-07-20 13:19:32 12
en9 English ldyllic 2022-07-20 13:19:01 4
en8 English ldyllic 2022-07-20 13:18:22 58
en7 English ldyllic 2022-07-20 13:13:55 5 Tiny change: 't. <br/> <br/>\nSecond c' -> 't. <br/> \nSecond c'
en6 English ldyllic 2022-07-20 13:13:25 91
en5 English ldyllic 2022-07-20 13:12:30 2
en4 English ldyllic 2022-07-20 13:12:14 233
en3 English ldyllic 2022-07-20 13:10:34 521
en2 English ldyllic 2022-07-20 13:04:25 6 Tiny change: 'rix. <br/>\nSe' -> 'rix. <br/> <br/>\nSe'
en1 English ldyllic 2022-07-20 13:03:48 853 Initial revision (saved to drafts)