0

I use Python 2.7.6 to measure the running time of an algorithm:

starting_time = time.clock()      
# run algorithm
finishing_time = time.clock()
running_time = (finishing_time - starting_time)

I run the algorithm with different input sizes. The algorithm is O(n-squared) and I get the following results:

Input Size Running Time

  1K        0.079382

  10K       8.310275

  100K      838.066953

  1000K    -2040.323576

When the input size is 1000K, running_time should be about 83800. Instead, I get a negative result. Is it because of the range of time.clock() function?

yildizabdullah
  • 1,895
  • 5
  • 24
  • 37
  • 1
    Why don't you use `timeit` or an actual profiler? – jonrsharpe Oct 27 '15 at 11:25
  • Python docs page says: time.clock() - On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms. – yildizabdullah Oct 27 '15 at 11:59

1 Answers1

1

Yes, this is because of the limitations of the underlying system call. The results you'll get really depend on your platform.

On Windows, the system call always returns a signed 64-bit integer (which is the divided by the counter frequency). So it should not wrap on any reasonable time scale.

On Linux the underlying integer is either signed 32-bit or signed 64-bit depending on whether your platform is 32-bit or 64-bit system. It is also further divided by the frequency of the counter so a 32-bit integer could definitely wrap after several hours.

On Linux you should really be using the newer clock_gettime() system call rather than clock(), which shouldn't have this wrap around problem. If you have Python 3.3 or newer it's as simple as:

time.clock_gettime(time.CLOCK_PROCESS_CPUTIME_ID)

If you have an older version of Python, you can either install a C API package like posix_timers to give you clock_gettime(). Check out: https://pypi.python.org/pypi/posix_timers/

Alternatively, you can use ctypes to access it your self. There is a stack overflow question that shows how to do this with a monotonic clock here: How do I get monotonic time durations in python?

The only difference you would have to make is to define CLOCK_PROCESS_CPUTIME_ID = 2 and use that instead of CLOCK_MONOTONIC_RAW.

However, the best answer is probably the one given by jonrsharpe, which is to use timeit or one of the other profiling tools that comes with Python.

Community
  • 1
  • 1
hetman
  • 919
  • 6
  • 16