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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:11:35+00:00 2026-06-18T02:11:35+00:00

I have a large code base, and something is now taking too long to

  • 0

I have a large code base, and something is now taking too long to execute. I have no idea what.

The code never raises an exception, it just appears to keep on processing something.

What I’d like to do is place timers around some functions to test which one is the culprit. But I’m not sure if that is the right approach, or how to do it.

I can’t easily raise exceptions at points around the code, because there are various loops that call the same functions, that perhaps only sometimes take too long.

What is the best strategy?

  • 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-18T02:11:36+00:00Added an answer on June 18, 2026 at 2:11 am

    One solution is to use cProfile, which comes built into Python, to tell what functions your code is spending the most time in. Critically, this profiling works even if you stop your code with a KeyboardInterrupt. Thus, you can start your code running and profiling, stop it after a minute or two, and then see where it was spending its time.

    Run your code with these extra -m and -o arguments:

    python -m cProfile -o profile.txt myscript.py
    

    and then once the program has finished running, run the following code (from another script, for example):

    import pstats
    p = pstats.Stats('profile.txt')
    p.strip_dirs().sort_stats("time").print_stats()
    

    This will print a list of the functions sorted by the total time you spent in them.

    Here’s a demonstration of using profiling to debug an infinite loop. Let’s say myscript.py had the following code.

    def f():
        while True:
            g(100000)
    
    def g(n):
        x = []
        for i in range(n):
            x.append(n)
    
    f()
    

    Of course this causes an infinite loop- g will be run many, many times. So I run the profiling command above, but then I stop it after about 30-40 seconds (could be much shorter even). Its profile will then be printed as:

    Wed Jan 30 10:58:50 2013    profile.txt
    
             115414854 function calls in 37.705 seconds
    
       Ordered by: internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
         1155   25.787    0.022   37.020    0.032 test3.py:5(g)
    115412541   10.060    0.000   10.060    0.000 {method 'append' of 'list' objects}
         1155    1.173    0.001    1.173    0.001 {range}
            1    0.685    0.685   37.705   37.705 test3.py:1(f)
            1    0.000    0.000   37.705   37.705 test3.py:1(<module>)
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    

    Notice that our infinitely looping function g is right at the top of the list in terms of time spent in the function.

    Note: Just because the code is spending all its time in one function doesn’t mean that the loop is directly around that function- it could be called by a function that is called by a function (etc) that is in an infinite loop (notice that append is near the top of the list since it is called inside g). One alternative trick is to sort them according to the cumulative time spent in each function, using .sort_stats("cum"). A combination of these two approaches, along with a little detective work (looking at the code and adding debug messages), should be able to identify the culprit.

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

Sidebar

Related Questions

I have just joined a project with a rather large existing code base. We
I have a large code base under source control (was subversion, now git). To
We have a large code base in MFC and VB. A few applications are
I have a large library of code which has a few (much too few)
We have rather large code base (150+ projects, 400000+ lines of Java code, some
I have a large C++ code base that I'm doing some refactoring on where
I have a large code base that resides on a unix server, and for
We have a large code base that makes copious use of the JSON v1
We have a fairly large code-base. The vast majority of the code is compiled
At work we have a very large code-base that we commonly export for a

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.