Debian Codegraph @

test/test-size.py

raw symbols SCIP
1 from __future__ import print_function
2 from sys import getsizeof, stderr
3 from itertools import chain
4 from collections import deque
5
6 try:
7 from reprlib import repr
8 except ImportError:
9 pass
10
11
12 def total_size(o, handlers={}, verbose=False):
13 """Returns the approximate memory footprint an object and all of its contents.
14
15 Automatically finds the contents of the following builtin containers and
16 their subclasses: tuple, list, deque, dict, set and frozenset.
17 To search other containers, add handlers to iterate over their contents:
18
19 handlers = {SomeContainerClass: iter,
20 OtherContainerClass: OtherContainerClass.get_elements}
21
22 """
23 dict_handler = lambda d: chain.from_iterable(d.items())
24 all_handlers = {
25 tuple: iter,
26 list: iter,
27 deque: iter,
28 dict: dict_handler,
29 set: iter,
30 frozenset: iter,
31 }
32 all_handlers.update(handlers) # user handlers take precedence
33 seen = set() # track which object id's have already been seen
34 default_size = getsizeof(0) # estimate sizeof object without __sizeof__
35
36 def sizeof(o):
37 if id(o) in seen: # do not double count the same object
38 return 0
39 seen.add(id(o))
40 s = getsizeof(o, default_size)
41
42 if verbose:
43 print(s, type(o), repr(o), file=stderr)
44
45 for typ, handler in all_handlers.items():
46 if isinstance(o, typ):
47 s += sum(map(sizeof, handler(o)))
48 break
49 return s
50
51 return sizeof(o)
52
53
54 # @profile
55 def doit():
56 print("HERE WE GO")
57 q1 = list(range(0, 2000))
58 q2 = list(range(0, 20000))
59 q3 = list(range(0, 200000))
60 r = range(0, 2000000)
61 q4 = []
62 for i in r:
63 q4.append(i)
64 # q4 = list(r)
65 z = 2000000 * getsizeof(1)
66 print(z)
67 print("q4", total_size(q4) / (1024 * 1024))
68 del q4
69 # print("q1", total_size(q1)/(1024*1024))
70 # print("q2", total_size(q2)/(1024*1024))
71 # print("q3", total_size(q3)/(1024*1024))
72
73
74 for i in range(12):
75 doit()