Skip to content

Instantly share code, notes, and snippets.

@nooperpudd
Last active August 23, 2017 05:30
Show Gist options
  • Save nooperpudd/787294d02ed8dc6e5ef8b9a1e18d55d9 to your computer and use it in GitHub Desktop.
Save nooperpudd/787294d02ed8dc6e5ef8b9a1e18d55d9 to your computer and use it in GitHub Desktop.
list filter remove momory profile and execute time profile
from memory_profiler import profile
from itertools import filterfalse
@profile(precision=1)
def test():
a = []
for i in range(100000):
a.append((10.23 + i, {"data": i}))
b = filterfalse(lambda x: float(x[0]) >= 1000.26, a)
@profile(precision=1)
def test2():
a = []
for i in range(100000):
a.append((10.23 + i, {"data": i}))
for b in a[:]:
if float(b[0]) >= 1000.23:
a.remove(b)
# memory profile
# pip install memory-profiler
# Line # Mem usage Increment Line Contents
# ================================================
# 4 35.3 MiB 0.0 MiB @profile(precision=1)
# 5 def test():
# 6 35.3 MiB 0.0 MiB a = []
# 7 69.5 MiB 34.2 MiB for i in range(100000):
# 8 69.5 MiB 0.0 MiB a.append((10.23 + i, {"data": i}))
# 9
# 10 70.2 MiB 0.7 MiB b = filterfalse(lambda x: float(x[0]) >= 10.26, a)
# 11 70.2 MiB 0.0 MiB print(list(b))
# memory profile
# Line # Mem usage Increment Line Contents
# ================================================
# 4 35.3 MiB 0.0 MiB @profile(precision=1)
# 5 # @profile()
# 6 # @profile
# 7 def test():
# 8 35.3 MiB 0.0 MiB a = []
# 9 69.7 MiB 34.4 MiB for i in range(100000):
# 10 69.7 MiB 0.0 MiB a.append((10.23 + i, {"data": i}))
# 11
# 12 # b = filterfalse(lambda x: float(x[0]) >= 1000.26, a)
# 13
# 14 71.2 MiB 1.5 MiB for b in a[:]:
# 15 71.2 MiB 0.0 MiB if float(b[0]) >= 1000.23:
# 16 71.2 MiB 0.0 MiB a.remove(b)
# time
# pip install line_profiler
# for liner-profile need to just add decorator @profile, no need to import from other library
# @profile
# def slow_function(a, b, c):
# ...
# >>> kernprof -l list_filter.py
# >>> python3 -m line_profiler list_filter.py.lprof
# Timer unit: 1e-06 s
# Total time: 0.286401 s
# File: aa.py
# Function: test at line 6
# Line # Hits Time Per Hit % Time Line Contents
# ==============================================================
# 6 @profile
# 7 def test():
# 8 1 1 1.0 0.0 a = []
# 9 100001 106407 1.1 37.2 for i in range(100000):
# 10 100000 119515 1.2 41.7 a.append((10.23 + i, {"data": i}))
# 11
# 12 1 3 3.0 0.0 b = filterfalse(lambda x: float(x[0]) >= 10.26, a)
# 13 1 60475 60475.0 21.1 print(list(b))
# Timer unit: 1e-06 s
# Total time: 5.62963 s
# File: aa.py
# Function: test at line 6
# Line # Hits Time Per Hit % Time Line Contents
# ==============================================================
# 6 @profile
# 7 def test():
# 8 1 1 1.0 0.0 a = []
# 9 100001 98297 1.0 1.7 for i in range(100000):
# 10 100000 98972 1.0 1.8 a.append((10.23 + i, {"data": i}))
# 11
# 12 # b = filterfalse(lambda x: float(x[0]) >= 10.26, a)
# 13 # print(list(b))
# 14
# 15 100001 89726 0.9 1.6 for b in a[:]:
# 16 100000 95212 1.0 1.7 if float(b[0]) >= 1000.23:
# 17 99010 5247421 53.0 93.2 a.remove(b)
if __name__ == "__main__":
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment