Re: module trace and counts

Discussion in 'Python' started by Dave Angel, Nov 17, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 11/17/2012 12:25 PM, rh wrote:
    > Is it for or range that is executed 8000 times when I
    > for i in range(3,8000,2):

    Nothing is executed 8000 times. I figure it at 3998 times. Anyway,
    neither the for nor the range is executed multiple times. Deciphering
    this depends on whether this is Python 2.x or Python 3.x.

    If Python 2.x, range returns a list of 3998 items, and that is iterated
    over.
    If Python 3.x, range returns a range object, which has an __iter__()
    special method. That method gets called 3999 times, and the last time
    it throws an exception to end the loop.

    >
    > Maybe the for and the range contributes to that total.
    >
    > Can anyone recommend their favorite trace or profile type tool that
    > spits out an html page? I like trace because I don't have to insert my
    > program into it but would like a html page too.
    >



    --

    DaveA
     
    Dave Angel, Nov 17, 2012
    #1
    1. Advertising

  2. On Sat, 17 Nov 2012 13:37:23 -0500, Dave Angel wrote:

    > On 11/17/2012 12:25 PM, rh wrote:
    >> Is it for or range that is executed 8000 times when I for i in
    >> range(3,8000,2):

    > Nothing is executed 8000 times. I figure it at 3998 times.


    Off by one.

    py> len(range(3, 8000, 2))
    3999


    --
    Steven
     
    Steven D'Aprano, Nov 18, 2012
    #2
    1. Advertising

  3. Dave Angel

    Dave Angel Guest

    On 11/17/2012 08:30 PM, Steven D'Aprano wrote:
    > On Sat, 17 Nov 2012 13:37:23 -0500, Dave Angel wrote:
    >
    >> On 11/17/2012 12:25 PM, rh wrote:
    >>> Is it for or range that is executed 8000 times when I for i in
    >>> range(3,8000,2):

    >> Nothing is executed 8000 times. I figure it at 3998 times.

    > Off by one.
    >
    > py> len(range(3, 8000, 2))
    > 3999
    >
    >

    I should have actually let Python figure it. Instead I knocked off one
    because the interval is half-open, and another because it starts at 3.
    Then I checked it by subtracting the first from the last and dividing by
    2 ( (7999-3)/2 ), but then made the stupid mistake of not adding one
    because my interval WAS closed.



    --

    DaveA
     
    Dave Angel, Nov 18, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rukmal Fernando
    Replies:
    4
    Views:
    879
    Richard Grimes [MVP]
    Oct 27, 2003
  2. Matthias S.
    Replies:
    1
    Views:
    674
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  3. Jiho Han
    Replies:
    0
    Views:
    547
    Jiho Han
    Aug 22, 2006
  4. rh

    module trace and counts

    rh, Nov 17, 2012, in forum: Python
    Replies:
    2
    Views:
    180
  5. rh
    Replies:
    0
    Views:
    189
Loading...

Share This Page