1

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)

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That is so much easier. Thanks a lot!
Thanks for the suggestion. Coming from a .NET/C# background, I found timeit very un-intuitive, especially with large functions and chunks of code. Just want to add that - I am currently on Python 3.8 and found the function time.time_ns() very useful. docs.python.org/3/library/time.html#time.time
1

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:

1 Comment

I'm sure that this works great, but it's a little more complicated than what I'm looking for. I was really looking to add a few lines to test parts of a function (module) instead of significantly altering my code. Thanks though, I'm sure that this will come in handy another time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.