Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Efficient processing of large nuumeric data file
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Steven D'Aprano  
View profile  
 More options Jan 18 2008, 5:41 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: Fri, 18 Jan 2008 22:41:52 -0000
Local: Fri, Jan 18 2008 5:41 pm
Subject: Re: Efficient processing of large nuumeric data file

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.