Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
 I DID have setup code, the x=0.  I now notice that if the x=0 is 
 not stated as the setup code, the time difference is enormous, 
 132-to-1 in this case.
 
 python -m timeit  -sx=0 while x100:   x+=1
 1000 loops, best of 3: 0.116 usec per loop
 
 python -m timeit x=0 while x100:   x+=1
 10 loops, best of 3: 15.3 usec per loop

timeit runs the setup code once, then runs the timed code many times 
with the timer running. If x=0 is outside the loop, then the while 
loop only runs once, because x == 100 after the first time through the 
loop. So your first version is effectively timing this:

setup:
x=100

timed code:
while x100:
   x+=1

which is of course a lot faster than actually running the loopp 100 times.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 03:05 AM 10/3/2006, Kent Johnson wrote:
Dick Moores wrote:
  I DID have setup code, the x=0.  I now notice that if the x=0 is
  not stated as the setup code, the time difference is enormous,
  132-to-1 in this case.
 
  python -m timeit  -sx=0 while x100:   x+=1
  1000 loops, best of 3: 0.116 usec per loop
 
  python -m timeit x=0 while x100:   x+=1
  10 loops, best of 3: 15.3 usec per loop

timeit runs the setup code once, then runs the timed code many times
with the timer running. If x=0 is outside the loop, then the while
loop only runs once, because x == 100 after the first time through the
loop. So your first version is effectively timing this:

setup:
x=100

timed code:
while x100:
x+=1

which is of course a lot faster than actually running the loopp 100 times.

Thanks, Kent. I was beginning to understand this, and now you've 
nailed it down for me.

Dick



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
 At 03:05 AM 10/3/2006, Kent Johnson wrote:
 timeit runs the setup code once, then runs the timed code many times
 with the timer running. If x=0 is outside the loop, then the while
 loop only runs once, because x == 100 after the first time through the
 loop. So your first version is effectively timing this:

 setup:
 x=100

 timed code:
 while x100:
x+=1

 which is of course a lot faster than actually running the loopp 100 times.
 
 Thanks, Kent. I was beginning to understand this, and now you've 
 nailed it down for me.

You might want to look at the source, timeit.py. There is a code 
template (called 'template') near the beginning. Your setup and timed 
code are inserted into the template, then it is compiled and run.

One of the dangers of timeit is that you may time something different 
than what you think you are timing, as you did. It's an easy mistake to 
make and hard to protect against.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 04:35 AM 10/3/2006, Kent Johnson wrote:
Dick Moores wrote:
  At 03:05 AM 10/3/2006, Kent Johnson wrote:
  timeit runs the setup code once, then runs the timed code many times
  with the timer running. If x=0 is outside the loop, then the while
  loop only runs once, because x == 100 after the first time through the
  loop. So your first version is effectively timing this:
 
  setup:
  x=100
 
  timed code:
  while x100:
 x+=1
 
  which is of course a lot faster than actually running the loopp 100 times.
 
  Thanks, Kent. I was beginning to understand this, and now you've
  nailed it down for me.

You might want to look at the source, timeit.py. There is a code
template (called 'template') near the beginning. Your setup and timed
code are inserted into the template, then it is compiled and run.

Very interesting. I thought a line of that template looked familiar. 
I was seeing _t0 = _timer() regularly when I had the -s option set 
without any setup:

C:\python -m timeit  -r 3 -sfor x in range(1):  x*x
Traceback (most recent call last):
   File E:\Python25\lib\runpy.py, line 95, in run_module
 filename, loader, alter_sys)
   File E:\Python25\lib\runpy.py, line 52, in _run_module_co
 mod_name, mod_fname, mod_loader)
   File E:\Python25\lib\runpy.py, line 32, in _run_code
 exec code in run_globals
   File E:\Python25\lib\timeit.py, line 285, in module
 sys.exit(main())
   File E:\Python25\lib\timeit.py, line 249, in main
 t = Timer(stmt, setup, timer)
   File E:\Python25\lib\timeit.py, line 116, in __init__
 code = compile(src, dummy_src_name, exec)
   File timeit-src, line 4
 _t0 = _timer()
   ^
But I don't understand what the error has to do with _t0 . That's OK, 
don't bother to explain. I don't understand classes yet anyway. 
Should get into them soon, with Wes Chun's book.

One of the dangers of timeit is that you may time something different
than what you think you are timing, as you did. It's an easy mistake to
make and hard to protect against.

Yes. thanks, Kent.

Dick




Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
 Very interesting. I thought a line of that template looked familiar. 
 I was seeing _t0 = _timer() regularly when I had the -s option set 
 without any setup:
 
 C:\python -m timeit  -r 3 -sfor x in range(1):  x*x
 Traceback (most recent call last):
File E:\Python25\lib\runpy.py, line 95, in run_module
  filename, loader, alter_sys)
File E:\Python25\lib\runpy.py, line 52, in _run_module_co
  mod_name, mod_fname, mod_loader)
File E:\Python25\lib\runpy.py, line 32, in _run_code
  exec code in run_globals
File E:\Python25\lib\timeit.py, line 285, in module
  sys.exit(main())
File E:\Python25\lib\timeit.py, line 249, in main
  t = Timer(stmt, setup, timer)
File E:\Python25\lib\timeit.py, line 116, in __init__
  code = compile(src, dummy_src_name, exec)
File timeit-src, line 4
  _t0 = _timer()
^
 But I don't understand what the error has to do with _t0 . That's OK, 
 don't bother to explain. I don't understand classes yet anyway. 
 Should get into them soon, with Wes Chun's book.

If you substitute your code into the template by hand and look at the 
actual exception (not shown above) you should see the problem. It 
doesn't have anything to do with classes.

Kent

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 05:54 AM 10/3/2006, Kent Johnson wrote:
Dick Moores wrote:
Very interesting. I thought a line of that template looked 
familiar. I was seeing _t0 = _timer() regularly when I had the -s 
option set without any setup:
C:\python -m timeit  -r 3 -sfor x in range(1):  x*x
Traceback (most recent call last):
File E:\Python25\lib\runpy.py, line 95, in run_module
  filename, loader, alter_sys)
File E:\Python25\lib\runpy.py, line 52, in _run_module_co
  mod_name, mod_fname, mod_loader)
File E:\Python25\lib\runpy.py, line 32, in _run_code
  exec code in run_globals
File E:\Python25\lib\timeit.py, line 285, in module
  sys.exit(main())
File E:\Python25\lib\timeit.py, line 249, in main
  t = Timer(stmt, setup, timer)
File E:\Python25\lib\timeit.py, line 116, in __init__
  code = compile(src, dummy_src_name, exec)
File timeit-src, line 4
  _t0 = _timer()
^
But I don't understand what the error has to do with _t0 . That's 
OK, don't bother to explain. I don't understand classes yet anyway. 
Should get into them soon, with Wes Chun's book.

If you substitute your code into the template by hand and look at 
the actual exception (not shown above) you should see the problem. 
It doesn't have anything to do with classes.

I meant timeit.py has classes.

OK, I called timeit.py with this template:

template = 
def inner(_it, _timer):
 %(setup)s
 _t0 = _timer()
 for _i in _it:
 %(x=0 while x100:  x*x)s
 _t1 = _timer()
 return _t1 - _t0

And got this error:

E:\Python25\Scripts-NotMinetimeit.py
Traceback (most recent call last):
   File E:\Python25\Scripts-NotMine\timeit.py, li
 sys.exit(main())
   File E:\Python25\Scripts-NotMine\timeit.py, li
 t = Timer(stmt, setup, timer)
   File E:\Python25\Scripts-NotMine\timeit.py, li
 src = template % {'stmt': stmt, 'setup': setup
KeyError: 'x=0 while x100:  x*x'

So what's the error? I've probably misunderstood how to do the substitution..

Dick



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Kent Johnson
Dick Moores wrote:
 At 05:54 AM 10/3/2006, Kent Johnson wrote:
 Dick Moores wrote:
 Very interesting. I thought a line of that template looked 
 familiar. I was seeing _t0 = _timer() regularly when I had the -s 
 option set without any setup:
 C:\python -m timeit  -r 3 -sfor x in range(1):  x*x
 Traceback (most recent call last):
File E:\Python25\lib\runpy.py, line 95, in run_module
  filename, loader, alter_sys)
File E:\Python25\lib\runpy.py, line 52, in _run_module_co
  mod_name, mod_fname, mod_loader)
File E:\Python25\lib\runpy.py, line 32, in _run_code
  exec code in run_globals
File E:\Python25\lib\timeit.py, line 285, in module
  sys.exit(main())
File E:\Python25\lib\timeit.py, line 249, in main
  t = Timer(stmt, setup, timer)
File E:\Python25\lib\timeit.py, line 116, in __init__
  code = compile(src, dummy_src_name, exec)
File timeit-src, line 4
  _t0 = _timer()
^
 But I don't understand what the error has to do with _t0 . That's 
 OK, don't bother to explain. I don't understand classes yet anyway. 
 Should get into them soon, with Wes Chun's book.
 If you substitute your code into the template by hand and look at 
 the actual exception (not shown above) you should see the problem. 
 It doesn't have anything to do with classes.
 
 I meant timeit.py has classes.
 
 OK, I called timeit.py with this template:
 
 template = 
 def inner(_it, _timer):
  %(setup)s
  _t0 = _timer()
  for _i in _it:
  %(x=0 while x100:  x*x)s
  _t1 = _timer()
  return _t1 - _t0
 

OK, let's go back to your first example and I will explain in more detail.

timeit.py contains this template:
template = 
def inner(_it, _timer):
 %(setup)s
 _t0 = _timer()
 for _i in _it:
 %(stmt)s
 _t1 = _timer()
 return _t1 - _t0


Whatever you specify for setup code is substituted for %(setup)s; the 
timed statement is substituted for %(stmt)s. This is done using standard 
string formatting operations. The result of the substitutions is a 
function definition which is compiled and run.

So if you run
python -m timeit -sfor x in range(1):  x*x

the generated function looks like this:
def inner(_it, _timer):
 for x in range(1):
 _t0 = _timer()
 for _i in _it:
  x*x
 _t1 = _timer()
 return _t1 - _t0

The for statement should begin an indented block; the next statement is 
*not* indented, so you get an IndentationError.

Does that help?
Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-03 Thread Dick Moores
At 07:08 AM 10/3/2006, Kent Johnson wrote:
Dick Moores wrote:
  At 05:54 AM 10/3/2006, Kent Johnson wrote:
  Dick Moores wrote:
  Very interesting. I thought a line of that template looked
  familiar. I was seeing _t0 = _timer() regularly when I had the -s
  option set without any setup:
  C:\python -m timeit  -r 3 -sfor x in range(1):  x*x
  Traceback (most recent call last):
 File E:\Python25\lib\runpy.py, line 95, in run_module
   filename, loader, alter_sys)
 File E:\Python25\lib\runpy.py, line 52, in _run_module_co
   mod_name, mod_fname, mod_loader)
 File E:\Python25\lib\runpy.py, line 32, in _run_code
   exec code in run_globals
 File E:\Python25\lib\timeit.py, line 285, in module
   sys.exit(main())
 File E:\Python25\lib\timeit.py, line 249, in main
   t = Timer(stmt, setup, timer)
 File E:\Python25\lib\timeit.py, line 116, in __init__
   code = compile(src, dummy_src_name, exec)
 File timeit-src, line 4
   _t0 = _timer()
 ^
  But I don't understand what the error has to do with _t0 . That's
  OK, don't bother to explain. I don't understand classes yet anyway.
  Should get into them soon, with Wes Chun's book.
  If you substitute your code into the template by hand and look at
  the actual exception (not shown above) you should see the problem.
  It doesn't have anything to do with classes.
 
  I meant timeit.py has classes.
 
  OK, I called timeit.py with this template:
 
  template = 
  def inner(_it, _timer):
   %(setup)s
   _t0 = _timer()
   for _i in _it:
   %(x=0 while x100:  x*x)s
   _t1 = _timer()
   return _t1 - _t0
  

OK, let's go back to your first example and I will explain in more detail.

Sorry to be so dumb.


timeit.py contains this template:
template = 
def inner(_it, _timer):
  %(setup)s
  _t0 = _timer()
  for _i in _it:
  %(stmt)s
  _t1 = _timer()
  return _t1 - _t0


Whatever you specify for setup code is substituted for %(setup)s; the
timed statement is substituted for %(stmt)s. This is done using standard
string formatting operations. The result of the substitutions is a
function definition which is compiled and run.

So if you run
python -m timeit -sfor x in range(1):  x*x

the generated function looks like this:
def inner(_it, _timer):
  for x in range(1):
  _t0 = _timer()
  for _i in _it:
   x*x
  _t1 = _timer()
  return _t1 - _t0

The for statement should begin an indented block; the next statement is
*not* indented, so you get an IndentationError.

Yes, that's exactly what I got when using timeit with that code at 
the command line, which I didn't understand.  John Fouhy explained it.

Does that help?

Yes, you've shown me a couple of things. How to substitute in a 
template. How it can be useful to look at the code of modules. And in 
this case I'll be able to use that template in a copy of timeit.py 
without going to the command line, with which I usually struggle.

Thanks again, again.

Dick

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-02 Thread Kent Johnson
Dick Moores wrote:
 C:\python -m timeit  -sx=0 while x100:   x+=1
 1000 loops, best of 3: 0.123 usec per loop
 
 C:\python -m timeit -sfor x in range(100): x+=1
 Traceback (most recent call last):
File E:\Python25\lib\runpy.py, line 95, in run_module
  filename, loader, alter_sys)
File E:\Python25\lib\runpy.py, line 52, in _run_module_code
  mod_name, mod_fname, mod_loader)
File E:\Python25\lib\runpy.py, line 32, in _run_code
  exec code in run_globals
File E:\Python25\lib\timeit.py, line 285, in module
  sys.exit(main())
File E:\Python25\lib\timeit.py, line 249, in main
  t = Timer(stmt, setup, timer)
File E:\Python25\lib\timeit.py, line 116, in __init__
  code = compile(src, dummy_src_name, exec)
File timeit-src, line 4
  _t0 = _timer()
^
 IndentationError: expected an indented block
 
 
 Is there a better way to indicate the indentation of   x+=1  (2 
 spaces in this case) ? For me, this usually works in a while loop, 
 but so far, never in a for loop.

Strange, your example works for me in Python 2.4 and 2.5:
D:\python -m timeit  -sx=0 while x100:   x+=1
1000 loops, best of 3: 0.102 usec per loop

Are you using 2.5 final or one of the earlier releases?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-02 Thread Dick Moores
At 03:08 AM 10/2/2006, Kent Johnson wrote:
Dick Moores wrote:
  C:\python -m timeit  -sx=0 while x100:   x+=1
  1000 loops, best of 3: 0.123 usec per loop
 
  C:\python -m timeit -sfor x in range(100): x+=1
  Traceback (most recent call last):
 File E:\Python25\lib\runpy.py, line 95, in run_module
   filename, loader, alter_sys)
 File E:\Python25\lib\runpy.py, line 52, in _run_module_code
   mod_name, mod_fname, mod_loader)
 File E:\Python25\lib\runpy.py, line 32, in _run_code
   exec code in run_globals
 File E:\Python25\lib\timeit.py, line 285, in module
   sys.exit(main())
 File E:\Python25\lib\timeit.py, line 249, in main
   t = Timer(stmt, setup, timer)
 File E:\Python25\lib\timeit.py, line 116, in __init__
   code = compile(src, dummy_src_name, exec)
 File timeit-src, line 4
   _t0 = _timer()
 ^
  IndentationError: expected an indented block
 
 
  Is there a better way to indicate the indentation of   x+=1  (2
  spaces in this case) ? For me, this usually works in a while loop,
  but so far, never in a for loop.

Strange, your example works for me in Python 2.4 and 2.5:
D:\python -m timeit  -sx=0 while x100:   x+=1
1000 loops, best of 3: 0.102 usec per loop

Are you using 2.5 final or one of the earlier releases?

2.5 final.

Dick



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timeit at the command line

2006-10-02 Thread Dick Moores
At 01:50 PM 10/2/2006, John Fouhy wrote:
On 02/10/06, Dick Moores [EMAIL PROTECTED] wrote:
C:\python -m timeit -sfor x in range(100): x+=1
Traceback (most recent call last):

The -s option specifies the setup code.  In this case, you don't have
any setup code.  Try this:

python -m timeit for x in range(100):  x += 1

HTH!

--
John.

Terrific!
E:\Python25\devpython -m timeit for x in range(100):  x += 1
10 loops, best of 3: 14.9 usec per loop

So with
python -m timeit  -sx=0 while x100:   x+=1
1000 loops, best of 3: 0.123 usec per loop

I DID have setup code, the x=0.  I now notice that if the x=0 is 
not stated as the setup code, the time difference is enormous, 
132-to-1 in this case.

python -m timeit  -sx=0 while x100:   x+=1
1000 loops, best of 3: 0.116 usec per loop

python -m timeit x=0 while x100:   x+=1
10 loops, best of 3: 15.3 usec per loop

Thanks, John.

Dick




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor