Skip to content
Advertisement

Measure execution time in CPU cycles?

My laptop spent 1.3 seconds to complete the process posted below. The same code ran on another machine and the timing was different: 2.1 seconds. This is because another machine runs on different Operation System, it has different CPU, memory and etc.

I wonder if instead of timing the process in seconds there would be a way to measure CPU cycles it took for the computer to complete a given process. So if the same code is run on different machines the measurements taken would always result the same number, and the result would be something like: it took 10,000 CPU cycles for this process to complete….

import time
def run():
    for i in range(10000000):
        0+0
start_time = time.time()
run()
print 'processed in: %s sec'%(time.time() - start_time) 

Advertisement

Answer

on linux systems you have time command, ie:

# time ls
/bin/ls ${=LS_OPTIONS}  0.00s user 0.00s system 68% cpu 0.003 total
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement