Created
October 11, 2014 18:00
-
-
Save chaosmail/3e10235e607cebebb234 to your computer and use it in GitHub Desktop.
Check performance of loops and arrays in Python
This file contains hidden or 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 __future__ import print_function | |
import cProfile | |
import random | |
num_elems = 1000000 | |
test = [random.randint(1,100) for i in range(num_elems)] | |
print("Loops\n*******") | |
print("Range and Len") | |
cProfile.run('[test[k] for k in range(len(test))]') | |
print("Enumerate") | |
cProfile.run('[test[k] for k,v in enumerate(test)]') | |
print("For") | |
cProfile.run('[v for v in test]') | |
print("Arrays\n*******") | |
some_value = "Test" | |
print("Append") | |
cProfile.run('for i in range(num_elems, 2*num_elems-1): test.append(some_value)') | |
print("Extend first") | |
cProfile.run('test.extend([]*num_elems)\nfor i in range(num_elems, 2*num_elems-1): test[i] = some_value') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment