0

After reading this Python's time.clock() vs. time.time() accuracy? and time.process_time(), among others, I'm pretty confused.

I need CPU time spend grinding out an algorithm. I want the time right before the call and right after it returns.

start = time.time() or time.process_time()
sorted = algorithm(stuff)
end = time.time() or time.process_time()
elapsed = end - start * (1e9)

time.process_time() seems like a great candidate, but I have no idea what units it returns on a Mac, can't seem to find a straight answer, and I need to convert to nanoseconds. I tried time.time() and the answer seemed wildly wrong. Thoughts on what I can do here?

Ryan
  • 1,312
  • 3
  • 20
  • 40
  • 2
    [`process_time`](https://docs.python.org/3/library/time.html#time.process_time) says pretty clearly what units it returns: "Return the value (in fractional seconds)…" As a general rule, time functions in Python that return a float are seconds, time functions that return an int are nanos iff they end in `_ns`. But rather than guessing, the docs (or `help` in your interactive interpreter) will always tell you the answer. – abarnert Jun 07 '18 at 01:29
  • I guess that's as good as it gets then. We don't know what fraction? No tenths, hundredths, etc? Just wanted to get my multiplier right, but it will be close enough for government work. EDIT: Just saw your clarification above very nice. – Ryan Jun 07 '18 at 01:32
  • 1
    Meanwhile, `time.time()` is just wall clock time. If you want that, it's usually easier to use `datetime` and not have to worry about units and epochs and so on. But wall clock time has little to do with how much CPU time you've spent. What you want is either `time.process_time` or `os.times` or a third-party lib. – abarnert Jun 07 '18 at 01:33
  • 1
    For more extra information other than the python docs you've already posted. You can take a look to the [source code](https://github.com/python/cpython/blob/master/Modules/timemodule.c#L1075) – BPL Jun 07 '18 at 01:48
  • @Ryan What do you mean by what fraction? It’s in seconds. It’s a float, so it’s got a few dozen bits of precision. But how accurate it is depends entirely on what your platform provides. – abarnert Jun 07 '18 at 01:58
  • @abarnert when I was swimming thru a sea of docs, I saw a table with several functions reporting results for different OS in millisecs, microsecs, etc. So, what I meant was "fractions of a second" seemed unusually vague for docs that are often very, very precise. – Ryan Jun 07 '18 at 02:09

1 Answers1

1

For benchmarking python code, consider using the bundled timeit (also in Python 3) package. It has a few advantages over doing it yourself: repeating the test multiple times to allow for OS/System things outside your control, disables garbage collection to prevent skewing of results, and uses the most accurate timer available for the current system (including Python 3.3+'s perf_counter).

You don't mention which Python version you're using, but if you are on 3.3+, perf_counter will give you the most precise timer available.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
  • This is definitely the right answer for benchmarking, but I don’t know whether the OP is benchmarking, profiling, accounting, or…? – abarnert Jun 07 '18 at 02:07