简体   繁体   中英

Calling function using Timeit

I'm trying to time several things in python, including upload time to Amazon's S3 Cloud Storage, and am having a little trouble. I can time my hash, and a few other things, but not the upload. I thought this post would finally, get me there, but I can't seem to find salvation. Any help would be appreciated. Very new to python, thanks!

import timeit

accKey = r"xxxxxxxxxxx";
secKey = r"yyyyyyyyyyyyyyyyyyyyyyyyy";

bucket_name = 'sweet_data'

c = boto.connect_s3(accKey, secKey)
b = c.get_bucket(bucket_name);
k = Key(b);

p = '/my/aws.path'
f = 'C:\\my.file'

def upload_data(p, f):
    k.key = p
    k.set_contents_from_filename(f)
    return

t = timeit.Timer(lambda: upload_data(p, f), "from aws_lib import upload_data; p=%r; f = %r" % (p,f))

# Just calling the function works fine
#upload_data(p, f)

I know this is heresy in the Python community, but I actually recommend not to use timeit , especially for something like this. For your purposes, I believe it will be good enough (and possibly even better than timeit !) if you simply use time.time() to time things. In other words, do something like

from time import time

t0 = time()
myfunc()
t1 = time()
print t1 - t0

Note that depending on your platform, you might want to try time.clock() instead (see Stack Overflow questions such as this and this ), and if you're on Python 3.3, then you have better options , due to PEP 418 .

You can use the command line interface to timeit .

Just save your code as a module without the timing stuff. For example:

# file: test.py
data = range(5)
def foo(l):
    return sum(l)

Then you can run the timing code from the command line, like this:

$ python -mtimeit -s 'import test;' 'test.foo(test.data)' 

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM