Message233066
| Author |
sbromberger |
| Recipients |
josh.r, lemburg, ncoghlan, pmoody, r.david.murray, rhettinger, sbromberger, serhiy.storchaka |
| Date |
2014年12月23日.23:18:35 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1419376715.58.0.734090310723.issue23103@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
As a test, I tried the following (taken mostly from http://codesnipers.com/?q=python-flyweights):
class Foo(object):
_Foo = weakref.WeakValueDictionary()
def __new__(cls, addr):
obj = Foo._Foo.get(addr, None)
if obj is None:
obj = object.__new__(cls)
Foo._Foo[addr] = obj
obj.addr = addr
return obj
I created 10 million instances of Foo(34) in an array. Total space taken: ~80 MB. Times: CPU times: user 6.93 s, sys: 48.7 ms, total: 6.98 s
Wall time: 6.98 s
I then created 10 million instances of a non-flyweight object, assigning an int to an instance variable:
class Bar(object):
pass
Total space taken: ~1.4 GB. Times:
CPU times: user 7.64 s, sys: 794 ms, total: 8.44 s
Wall time: 8.44 s
This corresponds (roughly) to the space taken by 10 million IPAddr objects.
So it appears, informally, that caching / flyweight results in modest time and significant memory savings.
I understand that the ship has sailed for a stdlib implementation, but these results are compelling enough for me to create a separate package. |
|