Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: module trace and counts

26 views
Skip to first unread message
Message has been deleted

Steven D'Aprano

unread,
Nov 17, 2012, 12:47:58 PM11/17/12
to
On Sat, 17 Nov 2012 09:25:02 -0800, rh wrote:

> Is it for or range that is executed 8000 times when I for i in
> range(3,8000,2):

I'm sorry, I don't understand that question.

Is it for what? What "it" are you talking about?

If you run the code:

for i in range(3, 8000, 2):
pass

the `for` statement executes once, giving 3999 iterations in total.
`range` gets executed once. Nothing is executed 8000 times.


> Maybe the for and the range contributes to that total.

What total?

I'm sure the context of your question is obvious to you, but we're not
mind-readers and have no idea what you have done and what total you have
got. Please explain your question.


--
Steven

Dave Angel

unread,
Nov 17, 2012, 1:37:23 PM11/17/12
to rh, pytho...@python.org
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

Message has been deleted
Message has been deleted

Steven D'Aprano

unread,
Nov 17, 2012, 8:30:41 PM11/17/12
to
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

Dave Angel

unread,
Nov 17, 2012, 9:54:31 PM11/17/12
to Steven D'Aprano, pytho...@python.org
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

0 new messages