Skip to content

Instantly share code, notes, and snippets.

@mstrongdev
Last active December 25, 2015 11:09
Show Gist options
  • Save mstrongdev/6967163 to your computer and use it in GitHub Desktop.
Save mstrongdev/6967163 to your computer and use it in GitHub Desktop.
A quick performance test between the "while(True) and break" method of control flow logic and the "while(conditional)" method.
import timeit
def while_var():
count = 1000
while(count > 0):
count -= 1
def while_break():
count = 1000
while(1):
count -= 1
if(count <= 0):
break
n = 100000
print("While Loop with Conditional (n="+str(n)+"):")
print(timeit.timeit('while_var()', setup='from __main__ import while_var', number=n))
print("While Loop with break statement (n=" + str(n) + "):")
print(timeit.timeit('while_break()', setup='from __main__ import while_break', number=n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment