0

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?

James.Wyst
  • 859
  • 3
  • 8
  • 13
  • 1
    How did you run cProfile? Does your module contain anything other than an import statement? – Peter Gibson Apr 01 '14 at 22:14
  • @PeterGibson I ran cProfile again. This time it identified the import statement as taking the most time. I'm editing the question. – James.Wyst Apr 01 '14 at 22:21
  • if this result is considered "wrong", what result did you expect or hope for instead? – Deestan Apr 01 '14 at 22:22
  • @PeterGibson And there is of course other code within the module, but cProfile says the rest takes a negligible amount of time to run. – James.Wyst Apr 01 '14 at 22:23
  • @Deestan I was hoping for the line to take less time than 290 seconds. However, I don't know if this is typical of importing date from datetime. This is the first time I've run cProfile on my code. – James.Wyst Apr 01 '14 at 22:24

1 Answers1

3

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.

Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
  • Thanks! I was going crazy trying to figure out why this wasn't working. Now to optimize the code within the module... – James.Wyst Apr 01 '14 at 22:26