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

I would like to write several functions in one Python module, each of which is a separate profiling test using timeit, so that I can use the command line argument to indicate which one to run. A naive example (profiling.py) would be:

import sys
import timeit

def foo():

    setup = """
    import random
    """

    foo_1 = """
    for i in range(1000):
        random.randint(0, 99) + random.randint(0, 99)
    """

    foo_2 = """
    for i in range(1000):
        random.randint(0, 99) + random.randint(0, 99)
    """

    foo_3 = """
    for i 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 do you want to run?'
        print 'available:'
        print '    foo'

However, when I try python profiling.py foo, I get an error as shown below:

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 does not match any outer indentation level

I searched for the usual place / tab fingerprint error in the code, but didn't find it. So I am wondering if this is due to the fact that I am completing a test timeitinside a function, and is this not allowed?

+5
source share
2 answers

It works:

import sys
import timeit

def foo():

    setup = """
import random
"""

    foo_1 = """
for i in range(1000):
    random.randint(0, 99) + random.randint(0, 99)
"""

    foo_2 = """
for i in range(1000):
    random.randint(0, 99) + random.randint(0, 99)
"""

    foo_3 = """
for i 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 do you want to run?'
        print 'available:'
        print '    foo'

, , setup foo_1, .., , . , timeit, , , . , ...

exec("    import sys")

.

+7

mgilson . - heredoc . python vim, .

, :

import sys
import timeit


def foo():

    setup = 'import random'

    foo_1 = '\n'.join([
        'for i in range(1000):',
        '    random.randint(0, 99) + random.randint(0, 99)',
    ])

    foo_2 = '\n'.join([
        'for i in range(1000):',
        '    random.randint(0, 99) + random.randint(0, 99)',
    ])

    foo_3 = '\n'.join([
        'for i 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 do you want to run?'
        print 'available:'
        print '    foo'

, .

0
source

All Articles