how to handle cpu cache in python ( or fastest way to call a function once)

Steven D'Aprano steve at pearwood.info
Sun Aug 23 14:42:56 CEST 2015


On Sun, 23 Aug 2015 10:07 pm, Vladimir Ignatov wrote:

> Hi,
> 
>>> for i in range(1000000): #just to create a time interval, seems this
>>> disturb cpu cache?
>>>     pass
> 
> Python interpreter consumes memory quite extensively because
> "everything is object".  So constructions like:
> 
> range(1000000):
> 
> _take_ memory.  Additionally it will trigger garbage collecting code
> on deallocation time so expect even more delay.

Normally you would be correct, but as my timing results show, using xrange
instead of range does not make any difference.

Whatever is going on here, it isn't as simple as "range(1000000) builds a
giant list".



-- 
Steven



More information about the Python-list mailing list