3 # Copyright 2011 Google Inc. All Rights Reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """measure the runtime of a command by repeatedly running it.
24 devnull = open('/dev/null', 'w')
26 def run(cmd, repeat=10):
31 for _ in range(repeat):
33 subprocess.call(cmd, stdout=devnull, stderr=devnull)
35 dt = (end - start) * 1000
36 print '%dms' % int(dt),
41 # We're interested in the 'pure' runtime of the code, which is
42 # conceptually the smallest time we'd see if we ran it enough times
43 # such that it got the perfect time slices / disk cache hits.
45 # Also print how varied the outputs were in an attempt to make it
46 # more obvious if something has gone terribly wrong.
47 err = sum(s - best for s in samples) / float(len(samples))
48 print 'estimate: %dms (mean err %.1fms)' % (best, err)
50 if __name__ == '__main__':
52 print 'usage: measure.py command args...'