Skip to content

Instantly share code, notes, and snippets.

@mat128
Last active December 15, 2015 07:19
Show Gist options
  • Save mat128/5222228 to your computer and use it in GitHub Desktop.
Save mat128/5222228 to your computer and use it in GitHub Desktop.
cpython
mmitchell:~/projects/formation/iweb_koans$ python --version
Python 2.7.2
mmitchell:~/projects/formation/iweb_koans$ /usr/bin/python profiling.py
Filename: profiling.py
Line # Mem usage Increment Line Contents
================================================
4 @profile
5 def test():
6 7.922 MB 0.000 MB # Create the big lists in advance to avoid skewing the memory counts
7 15.555 MB 7.633 MB seq1 = [None] * 10**6 # Big list of references to None
8 16.320 MB 0.766 MB seq2 = seq1[::10]
9
10 # Create and reference a lot of smaller lists
11 141.488 MB 125.168 MB seq1[:] = [[] for x in range(10**6)] # References all the new lists
12 143.020 MB 1.531 MB seq2[:] = seq1[::10] # Grab a second reference to 10% of the new lists
13
14 # Memory fragmentation in action
15 150.652 MB 7.633 MB seq1[:] = [None] * 10**6 # 90% of the lists are no longer referenced here
16 69.047 MB -81.605 MB seq2[:] = seq1[::10] # But memory freed only after last 10% are dropped
Filename: profiling.py
Line # Mem usage Increment Line Contents
================================================
18 @profile
19 7.918 MB 0.000 MB def hello():
20 test()
done
mmitchell:~/projects/formation/iweb_koans$ pypy profiling.py
Filename: profiling.py
Line # Mem usage Increment Line Contents
================================================
4 @profile
5 def test():
6 26.824 MB 0.000 MB # Create the big lists in advance to avoid skewing the memory counts
7 35.410 MB 8.586 MB seq1 = [None] * 10**6 # Big list of references to None
8 36.176 MB 0.766 MB seq2 = seq1[::10]
9
10 # Create and reference a lot of smaller lists
11 182.875 MB 146.699 MB seq1[:] = [[] for x in range(10**6)] # References all the new lists
12 182.875 MB 0.000 MB seq2[:] = seq1[::10] # Grab a second reference to 10% of the new lists
13
14 # Memory fragmentation in action
15 191.461 MB 8.586 MB seq1[:] = [None] * 10**6 # 90% of the lists are no longer referenced here
16 191.461 MB 0.000 MB seq2[:] = seq1[::10] # But memory freed only after last 10% are dropped
Filename: profiling.py
Line # Mem usage Increment Line Contents
================================================
18 @profile
19 26.793 MB 0.000 MB def hello():
20 test()
21 print "wow"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment