Skip to content

Instantly share code, notes, and snippets.

@LetMeFly666
Created February 23, 2024 03:30
Show Gist options
  • Select an option

  • Save LetMeFly666/9b1fdc0bf38c80d6cbec70a74730d1da to your computer and use it in GitHub Desktop.

Select an option

Save LetMeFly666/9b1fdc0bf38c80d6cbec70a74730d1da to your computer and use it in GitHub Desktop.
Big O: What is the efficiency of python's list slicing?
'''
Author: LetMeFly
Date: 2024-02-23 11:22:26
LastEditors: LetMeFly
LastEditTime: 2024-02-23 11:25:03
'''
import time
# pop(last element)
start = time.time()
a = [0 for _ in range(100000)]
while a:
a.pop()
end = time.time()
print(f'pop consume: {end - start}s')
# slice(first element)
start = time.time()
a = [0 for _ in range(100000)]
while a:
a = a[1:]
end = time.time()
print(f'slice consume: {end - start}s')
pop consume: 0.008001089096069336s
slice consume: 14.755402326583862s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment