Efficient processing of large nuumeric data file
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Jan 18 17:41:52 EST 2008
On Fri, 18 Jan 2008 12:06:56 -0600, Tim Chase wrote:
> I don't know how efficient len() is (if it's internally linearly
> counting the items in data, or if it's caching the length as data is
> created/assigned/modifed)
It depends on what argument you pass to len().
Lists, tuples and dicts (and maybe strings?) know how long they are, so
len() takes essentially constant time.
Custom classes are free to define __len__ anyway they like.
class MyList(list):
"""Just like the built-in list, but stupider."""
def __len__(self):
# Untested, for obvious reasons.
import random
import sys
while True:
guess = random.randrange(0, sys.maxint)
count = 0
for i in self:
count += 1
if count == guess:
return count
Caching __len__ is left as an exercise to the reader...
--
Steven
More information about the Python-list
mailing list