Reputation: 110123
How would I figure out how much memory an operation takes? For example:
memory_start = memory()
reader = csv.reader(file)
memory_end = memory()
memory_of_reader = math.abs(memory_end - memory_start)
Upvotes: 1
Views: 164
Reputation: 107287
If you want to get the size of an object you can use sys.getsizeof
that return the size of an object in bytes.
sys.getsizeof(object[, default])
Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.
If given, default will be returned if the object does not provide means to retrieve the size. Otherwise a
TypeError
will be raised.
getsizeof()
calls the object’s__sizeof__
method and adds an additional garbage collector overhead if the object is managed by the garbage collector.
example :
>>> import sys
>>> a=2
>>> sys.getsizeof(a)
24
>>> a='this is a test'
>>> sys.getsizeof(a)
51
And for more advanced tasks you can use memory_profiler that is a module for monitoring memory usage of a python program
Upvotes: 2