Skip to content

Instantly share code, notes, and snippets.

@LetMeFly666
Created February 23, 2024 03:30
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