By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Re: Function Memory Usage

P: n/a
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

Nov 1 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.