On Fri, 31 Oct 2008 18:41:58 +0000, Paulo J. Matos wrote: > Hi all, > > What's the best way to know the amount of memory allocated by a function > and the time it took to run? While the latter is simple to implement > using a wrapper function, the former is striking me as something that > needs to be primitive to python. Any tips?
It's impossible to do that in a generic way in any language. You've got to analyze the code to determine how much memory it uses. Some knowledge of the language's (actually the implementation's) internal can help. For example, list is stored as array of pointers to object, that means the amount of memory used for a list of integers is: len(ls) + sizeof(int) * len(ls) note: sizeof is not a real function or the size of a list of arbitrary content: len(ls) + sum(sizeof(item) for item in ls) PS: In reality, it's not as simple as that. CPython allocates more memory than it actually needs so it doesn't need to reallocate memory again and again. Terry Reedy says: > def zeros(n): return [0]*n > > how much memory is that That is a tricky question, because CPython caches small integers, including 0. The memory required would be something like len(ls) + 1 -- http://mail.python.org/mailman/listinfo/python-list