Last active
December 17, 2015 23:09
-
-
Save terriyu/5687203 to your computer and use it in GitHub Desktop.
Benchmarking different methods of running for loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
pygame.init() | |
clock = pygame.time.Clock() | |
a = list(range(10000000)) | |
def do_with_range(): | |
clock.tick() | |
k = 0 | |
for idx in range(len(a) - 1): | |
a[idx] < a[idx + 1] | |
print(clock.tick()) | |
def do_with_xrange(): | |
clock.tick() | |
k = 0 | |
for idx in xrange(len(a) - 1): | |
a[idx] < a[idx + 1] | |
print(clock.tick()) | |
def do_with_enumerate(): | |
clock.tick() | |
prev_num = None | |
for idx, num in enumerate(a): | |
if prev_num is not None: | |
prev_num < num | |
prev_num = num | |
print(clock.tick()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from benchmark import * | |
>>> do_with_range() | |
1492 | |
>>> do_with_xrange() | |
977 | |
>>> do_with_enumerate() | |
955 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment