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 7532953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:36:05+00:00 2026-05-30T05:36:05+00:00

Possible Duplicate: How to profile my code? Whats are best practices and tools for

  • 0

Possible Duplicate:
How to profile my code?

Whats are best practices and tools for profiling and performance testing python code?
Any quick wins here or recommendations.

CProfile seams popular and some great notes/answers below, both are very good answers/tutorials. Vote away and I’ll pick the top one in a day or two. Thanks @senderle and @campos.ddc

Once a problem area is found are there any idioms and/or tips for converting code to make it faster?

  • 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-05-30T05:36:06+00:00Added an answer on May 30, 2026 at 5:36 am

    cProfile is the classic profiling tool. The basic way to use it is like so:

    python -m cProfile myscript.py
    

    Here I’ve called it on the test routine of a reference implementation of the mersenne twister that I wrote.

    me@mine $ python -m cProfile mersenne.twister.py 
    True
    True
    1000000
             1003236 function calls in 2.163 CPU seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    2.163    2.163 <string>:1(<module>)
            1    0.001    0.001    2.162    2.162 mersenne.twister.py:1(<module>)
            3    0.001    0.000    0.001    0.000 mersenne.twister.py:10(init_gen)
      1000014    1.039    0.000    1.821    0.000 mersenne.twister.py:19(extract_number)
            1    0.000    0.000    0.000    0.000 mersenne.twister.py:3(Twister)
         1603    0.766    0.000    0.782    0.000 mersenne.twister.py:33(generate_numbers)
            1    0.000    0.000    0.000    0.000 mersenne.twister.py:4(__init__)
            1    0.317    0.317    2.161    2.161 mersenne.twister.py:42(_test)
            1    0.001    0.001    2.163    2.163 {execfile}
            1    0.000    0.000    0.000    0.000 {len}
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
         1608    0.038    0.000    0.038    0.000 {range}
    

    ncalls is the number of times a function was called. tottime is the total time spent in a function, excluding the time spent in sub-function calls. percall is tottime / ncalls. cumtime is the time spent in the function including the time spent in sub-function calls. And the remaining data is as follows: filename:lineno(func_name).

    In most cases, look at ncalls and tottime first. In the above data, you can see that the large majority of the time spent by this program happens in extract_number. Furthermore, we can see that extract_number is called many (1000014) times. So anything I can do to speed up extract_number will significantly speed up the execution of this test code. If it gains me a microsecond, then the gain will be multiplied by 1000014, resulting in a full second gain.

    Then I should work on generate_numbers. Gains there won’t matter as much, but they may still be significant, and since that function burns another .7 seconds, there’s some benefit to be had.

    That should give you the general idea. Note, however, that the tottime number can sometimes be deceptive, in cases of recursion, for example.

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

Sidebar

Related Questions

Possible Duplicate: How can you profile a Python script? What visual tools do you
Possible Duplicate: What's your favorite profiling tool (for C++) Are there any good tools
Possible Duplicate: How to profile and and get Javascript performance I have a page
Possible Duplicate: Xcode - iPhone - profile doesn’t match any valid certificate-/private-key pair in
Possible Duplicate: What is the best way to profile javascript execution? I have a
Possible Duplicate: How to profile the performance overhead of exception handling Performance when exceptions
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: Upload photo to users profile from photo URL, not input file field
Possible Duplicate: Locking main() thread Below you'll find my code, Main calls two threads,
Possible Duplicate: Simplest way to profile a PHP script We are building this online

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.