7

I've been trying to figure this out for a while now and I haven't been able to. Basically what I want to do is get the Time it takes to complete a specific task.

For Example:

def find(x): if x in stuff: return "X was found, Search Time: [TIME IT TOOK]" 

I would like it to be something like "Search Time: 0.03 seconds". This is a really bad example but it's midnight and i'm trying to complete a python project for school so all answers are greatly appreciated.

Thanks

10
  • 5
    You can use timeit module. Commented Dec 10, 2014 at 7:14
  • 1
    This is just the time taken to run, time complexity is different. Commented Dec 10, 2014 at 7:14
  • That's not time complexity, it's just wall-clock time. Time complexity reflects the scalability of an algorithm (to get wall-clock time, use timeit, as Marcin suggests) Commented Dec 10, 2014 at 7:14
  • How else you would interpret "Search Time: [TIME IT TOOK]"? I dont think its O(log(n)) or whatever. I think its about time in seconds or microseconds. Commented Dec 10, 2014 at 7:17
  • True, timeit is the right direction but it would be nice to see the best way of using it and saving the result at the same time Commented Dec 10, 2014 at 7:17

1 Answer 1

9

As I understand your goal, it's not a profiler that you're after. For something as simple as logging how long a specific processing step took (wall-clock time), I usually use time.time() to get the start and end times and take the difference of them.

Example:

import time start = time.time() # do something pass end = time.time() delta = end - start print "took %.2f seconds to process" % delta 

For a possibly greater resolution (microseconds), and for its independence from the system clock, also have a look at time.clock(). Note that this measures CPU time, not wall-clock time (at least on Linux - this may not be what you need).

For reference:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.