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

  • SEARCH
  • Home
  • 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 7836803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:19:13+00:00 2026-06-02T14:19:13+00:00

In the command line, I can do: python -m timeit ‘a = 1’ According

  • 0

In the command line, I can do:

python -m timeit 'a = 1'

According to the docs:

If -n is not given, a suitable number of loops is calculated by trying successive powers
of 10 until the total time is at least 0.2 seconds.

This works great, but how can I get the same behavior when using timeit in my program? If I leave out the number argument to the timeit.timeit call, it will simply default to 1000000.

  • 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-02T14:19:22+00:00Added an answer on June 2, 2026 at 2:19 pm

    The docs don’t make it obvious that there exists such functionality, I’ll assume there isn’t. Fortunately, defining a wrapper that does it for you is isn’t too hard:

    import timeit
    
    def auto_timeit(stmt='pass', setup='pass'):
        n = 1
        t = timeit.timeit(stmt, setup, number=n)
    
        while t < 0.2:
            n *= 10
            t = timeit.timeit(stmt, setup, number=n)
    
        return t / n # normalise to time-per-run
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can import MySQLdb from the interactive python command line but Zope gives me
Can anybody help me how to run a python script through command line? My
I have a script in python that can be invoked from the command-line and
How can I handle utf8 using Perl (or Python) on the command line? I
In Python, how can we find out the command line arguments that were provided
With Perl's Getopt::Long you can easily define command-line options that take a variable number
In Python one can say this: python script.py from the command line and receive
From command line I can't import appengine, this might be something with my python
Using Python I can test my code in the terminal / command line by
Using IPython .10-2, I can do the following from a python command line program:

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.