How Does the Order of IF Statements Improves the Runtime

Правка en18, от ldyllic, 2022-07-20 13:38:49

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 so-called effect, that's why I would like to share it here and hope it'll help you out someday.

1st code 164992399 executes in a time of 1918ms.
2nd code 164991769 executes in a time of 280ms.

Notice 2 drastically different runtimes (6x difference!). The reason for that is pretty straightforward: in this code one of three IF statements gets a true response way more often than others (IF statement with 2). While the first code has an IF statement with 2 at the end, the second code has such IF statement at the beginning.

Knowing this we can make a conclusion: the order of IF statements matters, and sometimes matters a lot (you get AC or TLE). So whenever you hit (or feel you might hit) the bound of the time limit you always want to check whether all IF statements are placed in the right order and if not — a little change might make you happier by seeing this lovely green Accepted word.

История

 
 
 
 
Правки
 
 
  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)