I'm currently doing some work in uni that requires generating multiple benchmarks for multiple short C programs. I've written a python script to automate this process. Up until now I've been using the
start = time.time()
successful = run_program(path)
end = time.time()
runtime = end - start
p = subprocess.Popen(path, shell=True, stdout=subprocess.PIPE)
if (p.returncode > 1):
command = 'p = subprocess.Popen(\'time ' + path + '\', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE); out, err = p.communicate()'
result = timeit.Timer(command, setup='import subprocess').repeat(1, 10)
timeit will measure the CPU time used by the Python process in which it runs. Execution time of external processes will not be "credited" to those times.