While true vs while 1 ; what do you think about it ? | Sololearn: Learn to code for FREE!

+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

7/20/2018 10:01:52 AM

DDD

17 Answers

New Answer

+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.

+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.

+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.

+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.

+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.

+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

+3

nice since the value to true is given directly as 1 (binary equivalent) the processing will be faster...

+2

+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.

+2

I've just saw strawdog commentary. I'm agree with him. its not significant, and use true/false is easiest to understand.

+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.

+1

You can understand the problem by more explanation here https://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference

+1

its very perfect

+1

+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

+1

Alfredo no problem Alfredo , always up for a challenge ; good luck πŸ™ƒ

-5