[Tutor] More problems with Learning Python example (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Aug 12 09:58:57 CEST 2005



---------- Forwarded message ----------
Date: Fri, 12 Aug 2005 00:29:00 -0500 (CDT)
From: "->Terry<-" <tvbare at socket.net>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] More problems with Learning Python example

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Today (Aug 11, 2005) at 7:00pm, Danny Yoo spoke these wise words:

<snip>

- ->Developing this further, it might be a good thing to explicitely write a
- ->helper to run a function n times.  If we imagine that we have something
- ->like this:
- ->
- ->######
- ->def time_trial(func, num_times):
- ->    """Applies a function func num_times."""
- ->    ... # fill me in
- ->######
- ->
- ->
- ->then the logic of the block in do_timing() reduces to:
- ->
- ->####
- ->for func in funcs:
- ->    totals[func] = 0.0
- ->    time_trial(func, num_times)
- ->####
- ->
- ->
- ->I'm still a little shocked that Learning Python would have such code,
- ->though.  Can anyone double check this?  We should be sending errata
- ->reports if there are serious bugs like this in the book..
- ->
- ->
- ->If you have any questions on this, please ask questions.  Good luck!

Ok, I struck out on my own here and using your advice,
I came up with the following:

- --------------------------------
* makezeros.py
- --------------------------------

def lots_of_appends():
     zeros = []
     for i in range(10000):
         zeros.append(0)

def one_multiply():
     zeros = [0] * 10000


- --------------------------------
* my_timings.py
- --------------------------------

#!/usr/bin/python

import time, makezeros

def time_trial(func, num_times):
    total = 0.0
    starttime = time.clock()
    for num in range(num_times):
	apply(func)
    stoptime = time.clock()
    elapsed = stoptime - starttime
    total = total + elapsed
    return total

tries = 100
funcs = [makezeros.lots_of_appends, makezeros.one_multiply]

for func in funcs:
    took = time_trial(func, tries)
    print "Running %s %d times took %.3f seconds." % (func.__name__, tries, took)

Entering:

python my_timings.py >> results.txt

in a shell 10 times I get the following:

Running lots_of_appends 100 times took 0.400 seconds.
Running one_multiply 100 times took 0.020 seconds.
Running lots_of_appends 100 times took 0.460 seconds.
Running one_multiply 100 times took 0.000 seconds.   <<----- ?
Running lots_of_appends 100 times took 0.440 seconds.
Running one_multiply 100 times took 0.010 seconds.
Running lots_of_appends 100 times took 0.390 seconds.
Running one_multiply 100 times took 0.010 seconds.
Running lots_of_appends 100 times took 0.450 seconds.
Running one_multiply 100 times took 0.010 seconds.
Running lots_of_appends 100 times took 0.450 seconds.
Running one_multiply 100 times took 0.010 seconds.
Running lots_of_appends 100 times took 0.440 seconds.
Running one_multiply 100 times took 0.010 seconds.
Running lots_of_appends 100 times took 0.440 seconds.
Running one_multiply 100 times took 0.010 seconds.
Running lots_of_appends 100 times took 0.460 seconds.
Running one_multiply 100 times took 0.020 seconds.
Running lots_of_appends 100 times took 0.410 seconds.
Running one_multiply 100 times took 0.010 seconds.

Is the indicated result a fluke value which I can just
disregard or is there a problem with my code? The 0.000
value shows up about once in every 25-30 runs.

Any other comments?

This whole thing really had me going in circles. Surely
others have run into this problem. Again, many thanks
to Bob and especially to Danny for being so helpful
and spending time helping me.

Sincerely,
- --
     Terry

      ,-~~-.___.     Terry Randall <tvbareATsocketDOTnet>
     / |  '     \
    <   )        0    Linux Counter Project User# 98233
     \_/, ,-----'
        ====          //
       /  \-'~;    /~~~(0)
      /  __/~|   /      |   If only Snoopy had Slackware...
    =( ______| (________|

"He is your friend, your partner, your defender, your dog.
You are his life, his love, his leader. He will be yours,
faithful and true, to the last beat of his heart. You owe
it to him to be worthy of such devotion."    -- Unknown

          (Best viewed with a mono-spaced font.)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFC/DOhQvSnsfFzkV0RAoMGAJ0dPAZsnQHraHcTUi/Plm6GFl5z5wCeJude
mS9NbsMjKxViRI0j6NfqsSU=
=P4MJ
-----END PGP SIGNATURE-----




More information about the Tutor mailing list