python - Can we run multiple functions each with timeit in the same module -


i write multiple functions in same python module, each of separate profiling test using timeit, can use command line argument specify 1 run. naive example (profiling.py) be:

import sys import timeit  def foo():      setup = """     import random     """      foo_1 = """     in range(1000):         random.randint(0, 99) + random.randint(0, 99)     """      foo_2 = """     in range(1000):         random.randint(0, 99) + random.randint(0, 99)     """      foo_3 = """     in range(1000):         random.randint(0, 99) + random.randint(0, 99)     """      print 'foo_1', timeit.timer(foo_1, setup).timeit(1000)     print 'foo_2', timeit.timer(foo_2, setup).timeit(1000)     print 'foo_3', timeit.timer(foo_3, setup).timeit(1000)  if __name__ == '__main__':     if (len(sys.argv) > 1):         if (sys.argv[1] == 'foo'):             foo()     else:         print 'which profiling want run?'         print 'available:'         print '    foo' 

however, when try python profiling.py foo, error following:

foo_1 traceback (most recent call last):   file "profiling.py", line 32, in <module>     foo()   file "profiling.py", line 25, in foo     print 'foo_1', timeit.timer(foo_1, setup).timeit(1000)   file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/timeit.py", line 136, in __init__     code = compile(src, dummy_src_name, "exec")   file "<timeit-src>", line 6     _t0 = _timer()                  ^ indentationerror: unindent not match outer indentation level 

i have searched usual space/tab indention error in code, didn't find any. hence wonder if because wrap timeit test inside function, , not allowed?

this works:

import sys import timeit  def foo():      setup = """ import random """      foo_1 = """ in range(1000):     random.randint(0, 99) + random.randint(0, 99) """      foo_2 = """ in range(1000):     random.randint(0, 99) + random.randint(0, 99) """      foo_3 = """ in range(1000):     random.randint(0, 99) + random.randint(0, 99) """      print 'foo_1', timeit.timer(foo_1, setup).timeit(1000)     print 'foo_2', timeit.timer(foo_2, setup).timeit(1000)     print 'foo_3', timeit.timer(foo_3, setup).timeit(1000)  if __name__ == '__main__':     if (len(sys.argv) > 1):         if (sys.argv[1] == 'foo'):             foo()     else:         print 'which profiling want run?'         print 'available:'         print '    foo' 

the problem strings you're passing setup , foo_1, etc. are indented since you've lined them indented code inside function. however, when string gets executed timeit, raises indentation error see because code should not indented. same thing happen if tried...

exec("    import sys") 

as first thing in interactive interpreter session.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -