1

I use Python3, Ubuntu 14.04 to run the following snippet. The timing() function never terminate. I suppose it is due to the Timer() function. Why? How can I fix it?

import subprocess

def timing():
    args = ("./a.out")
    print('start')
    popen = subprocess.Popen(args, stdout=subprocess.PIPE)
    popen.wait()    
    print('end')

if __name__ == '__main__':
    import timeit
    print('main')
    t=timeit.Timer("timing()","from __main__ import timing")
    print(t.timeit())

In the terminal, it shows:

main
start
end
start
end
start
...

The skeleton of the code is from this answer.

Community
  • 1
  • 1
GY_
  • 398
  • 3
  • 17
  • Does `a.out` terminate? If you run `timing()` without the `timeit` part does it behave as you expect? – Eric Renouf Jun 18 '15 at 14:51
  • `a.out` will terminate. The execute time of `a.out` is supposed to be micro seconds. I tested to call `a.out` without using Timer(). It worked properly. – GY_ Jun 18 '15 at 14:54

1 Answers1

4

By default timeit() is going to try to run the sample code 1000000 times. With processes starting and stopping that's likely going to take a long time. You can pass a number to timeit() to make it run fewer tests, like say

print(t.timeit(100))

to just run it 100 times.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67