Search code examples
pythontimecprofile

Why is my code so slow?


I'm new to cProfile.

I ran cProfile on my program and it spit out this:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1  290.732  290.732  313.069  313.069 newad.py:1(<module>)

The first line of newad.py is:

1  from datetime import date

Is this line supposed to take this much time? What can I do instead?


Solution

  • cProfile is just showing you the time spent in that module. The line number seems to just indicate the first statement processed in that module - because you have a multiline docstring, it shows the last line of the docstring.

    """
    Test module for cProfile stats
    
    """
    
    
    
    import time
    
    def wait(t):
        time.sleep(t)
    
    wait(5)
    

    Gives:

       $ python -m cProfile test.py
             4 function calls in 5.002 seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    5.001    5.001 test.py:10(wait)
            1    0.001    0.001    5.002    5.002 test.py:4(<module>)
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
            1    5.001    5.001    5.001    5.001 {time.sleep}
    

    Note the first line shows the time spent in the funciton wait while the second line shows the time spent in the module.