How Does the Order of IF Statements Improves the Runtime

Правка en2, от ldyllic, 2022-07-20 13:04:25

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.

История

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