Skip to content

Instantly share code, notes, and snippets.

@mark-adams
Created May 6, 2016 02:03
Show Gist options
  • Save mark-adams/189a7f01e1de717ef3e81b671fd69e60 to your computer and use it in GitHub Desktop.
Save mark-adams/189a7f01e1de717ef3e81b671fd69e60 to your computer and use it in GitHub Desktop.
List ordering performance comparison
def test_1():
buf = []
for i in range(1000):
buf.append(i)
buf.reverse()
if __name__ == '__main__':
import timeit
print('Testing with append then reverse')
print(timeit.timeit("test_1()", setup="from __main__ import test_1"))
def test_2():
buf = []
for i in range(1000):
buf.insert(i, 0)
if __name__ == '__main__':
import timeit
print('Testing with prepend (via list.insert)')
print(timeit.timeit("test_2()", setup="from __main__ import test_2"))
@mark-adams
Copy link
Author

Results:

python test1.py && python test2.py

Testing with append then reverse
73.3084340096
Testing with prepend (via list.insert)
142.222936869

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment