Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:20:42+00:00 2026-06-05T01:20:42+00:00

I would like to write multiple functions in the same Python module, each of

  • 0

I would like to write multiple functions in the same Python module, each of which is a separate profiling test using timeit, so that I can use command line argument to specify 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 error like the 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 does not match any outer indentation level

I have searched for the usual space/tab indention error in the code, but didn’t find any. Hence I wonder if it is because I wrap a timeit test inside a function, and this is not allowed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:20:44+00:00Added an answer on June 5, 2026 at 1:20 am

    This 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'
    

    The problem is that your strings that you’re passing as setup and foo_1, etc. are indented since you’ve lined them up with your indented code inside the function. However, when that string gets executed by timeit, it raises the indentation error you see because the code should not be indented. This is essentially the same thing that would happen if you tried…

    exec("    import sys")
    

    as the first thing in your interactive interpreter session.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a function in which there are multiple Exit Function
I would like to write a script which will edit multiple XML files, I
I would like to write a hql query using a dynamic instantiation with a
I've attached multiple functions in multiple files to $(document).ready and would like to attach
I would like to write the radian units of the axes as proportional to
I would like to write nested query with Doctrine; Sample SQL is like below:
I would like to write the a startup script in bash to make an
I would like to write inside that code (pretty-config.xml): <pretty-config xmlns=http://ocpsoft.com/prettyfaces/3.3.2 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://ocpsoft.com/prettyfaces/3.3.2 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.2.xsd>
I would like to write a cross-platform function in C++ that contains system calls.
I would like to write an if statement that will do something base on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.