While true vs while 1 ; what do you think about it ? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 27

While true vs while 1 ; what do you think about it ?

import time start_time = time.time() idx = 0 while True: idx += 1 if idx == 100000000: break print('%2.2f sec' % (time.time() - start_time)) start_time = time.time() idx = 0 while 1: idx += 1 if idx == 100000000: break print('%2.2f sec' % (time.time() - start_time)) While True -> 7.20 sec While 1 -> 7.16 sec difference : 0.06 sec with one more 0: While True -> 74.55 sec While 1 -> 74.26 sec difference : 0.29sec

20th Jul 2018, 10:01 AM
DDD
DDD - avatar
17 Antworten
+ 8
True and False are keywords that are equal to 0 and 1 respectively, because they are considered as integers and according to docs act like them almost in every cases. Due to many factors not related to python code logic and the necessity to convert boolean referencec to their intege meanings there could be a little time differrence, but it is so insignificant you can count them both equal methods. Though, i believe, using integer will always be slightly faster on big iterations.
20th Jul 2018, 11:38 AM
strawdog
strawdog - avatar
+ 12
well that's an excellent conclusion and i think it is because computer takes binary input and if you yourselves give binary input, then computer will be able to execute much faster.
20th Jul 2018, 11:26 AM
jakkala vishnu
+ 7
Interesting, seems like for extremely large data sets; 0 and 1 are faster, which is strange because the python convention strongly enforces using True and False.
20th Jul 2018, 3:22 PM
Lachlan
+ 4
Nice testing . I guess you save time processing code AND you save a few extra 0.01 seconds by typing ÔÇť1ÔÇŁ instead of true.
21st Jul 2018, 6:15 PM
Nicolas Combe
Nicolas Combe - avatar
+ 4
Kind of interesting to see where one has to balance readability with efficiency, for example, a regular sized data set would not require using 1 and 0 as opposed to True and False but as your test shows, for ridiculously large data sets it saves a significant amount of time.
21st Jul 2018, 6:18 PM
Lachlan
+ 3
Great findings DDD . Like jakkala vishnu mentioned the computer doesn't have to first convert True/False to it's binary equivalent 1/0 thus this saves time
21st Jul 2018, 7:38 AM
E_E Mopho
E_E Mopho - avatar
+ 3
nice since the value to true is given directly as 1 (binary equivalent) the processing will be faster...
23rd Jul 2018, 5:59 AM
Ashita Udhani
Ashita Udhani - avatar
+ 2
nice findings
22nd Jul 2018, 9:00 AM
$┬ó­ŁÉÄÔé╣­Łöş!­ŁÉĘ­ŁôŁ
$┬ó­ŁÉÄÔé╣­Łöş!­ŁÉĘ­ŁôŁ - avatar
+ 2
I don't think it's because computer use binary. Here the 1 is a simple int not a binnary nomber. the true is a boolean and boolean are closer to binary than int. I don't know the reason, but we propably can respond if we know how python is build.
23rd Jul 2018, 5:56 AM
DDD
DDD - avatar
+ 2
I've just saw strawdog commentary. I'm agree with him. its not significant, and use true/false is easiest to understand.
23rd Jul 2018, 6:10 AM
DDD
DDD - avatar
+ 1
Interesting, seems like for extremely large data sets; 0 and 1 are faster, which is strange because the python convention strongly enforces using True and False.
21st Jul 2018, 10:49 AM
Priyank Sharma
Priyank Sharma - avatar
24th Jul 2018, 2:43 AM
umesh kumar Kumawat
umesh kumar Kumawat - avatar
+ 1
its very perfect
24th Jul 2018, 4:14 AM
David
+ 1
nice observations
24th Jul 2018, 4:37 AM
$┬ó­ŁÉÄÔé╣­Łöş!­ŁÉĘ­ŁôŁ
$┬ó­ŁÉÄÔé╣­Łöş!­ŁÉĘ­ŁôŁ - avatar
+ 1
hi sir, you are very good, itÔÇÖs a pleasure for me that i could challange you. Just drop by and say thank you (obviously idk how to send message here) for helping me get answers by keep accepting my challange! Nicolas Combe
3rd Sep 2018, 3:20 AM
Alfredo
Alfredo - avatar
+ 1
Alfredo no problem Alfredo , always up for a challenge ; good luck ­čÖâ
3rd Sep 2018, 3:24 AM
Nicolas Combe
Nicolas Combe - avatar
- 5
salam
21st Jul 2018, 2:02 AM
Milad Darvish
Milad Darvish - avatar