Skip to content

Instantly share code, notes, and snippets.

@joshthecoder
Created November 12, 2009 16:04
Show Gist options
  • Save joshthecoder/233008 to your computer and use it in GitHub Desktop.
Save joshthecoder/233008 to your computer and use it in GitHub Desktop.
def while_true():
i = 0
while True:
i += 1
if i > 10000:
break
def while_1():
i = 0
while 1:
i += 1
if i > 10000:
break
from timeit import Timer
while_true_timer = Timer('while_true()', 'from __main__ import while_true')
while_1_timer = Timer('while_1()', 'from __main__ import while_1')
print('Running benchmarks...')
while_true_time = while_true_timer.timeit(1000)
while_1_time = while_1_timer.timeit(1000)
print('Results...')
print('While using True time: %f seconds' % while_true_time)
print('While using 1 time: %f seconds' % while_1_time)
print('While 1 was %f seconds faster' % (while_true_time - while_1_time))
~
Python "while True" vs "while 1"
Python 2.6 Results...
While using True time: 1.629400 seconds
While using 1 time: 1.021685 seconds
While 1 was 0.607715 seconds faster
Python 2.5 Results...
While using True time: 1.614696 seconds
While using 1 time: 1.110557 seconds
While 1 was 0.504139 seconds faster
Python 2.4 Results...
While using True time: 1.497838 seconds
While using 1 time: 1.033050 seconds
While 1 was 0.464788 seconds faster
Python 3.1 Results...
While using True time: 1.377948 seconds
While using 1 time: 1.363061 seconds
While 1 was 0.014887 seconds faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment