cu-numcomp / numcomp-class-spring19

CSCI-3656 Numerical Computation (Spring 2019)
BSD 2-Clause "Simplified" License
7 stars 21 forks source link

cProfile output read #21

Open tech3371 opened 5 years ago

tech3371 commented 5 years ago

python-version 3.6.8 Like we discussed in office hour today, I wrote cProfile to get the time it takes to run function call and it's good. original code:

        #start the cProfile timer here
        prof.enable()
        ret = benchmarkFunction(x,lambda x: 2*x,timestep) #function that does the calculation
        prof.disable()
        s      = io.StringIO()
        sortby = 'time'
        ps     = pstats.Stats(prof, stream=s).sort_stats(sortby)
        ps.print_stats(10)
        print(s.getvalue())

I got Output like below:

14809 function calls in 0.054 seconds

   Ordered by: internal time
   List reduced from 64 to 10 due to restriction <10>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       21    0.018    0.001    0.054    0.003 <ipython-input-101-80a6d7e4cb13>:3(benchmarkFunction)
      171    0.008    0.000    0.008    0.000 {built-in method posix.urandom}
      171    0.005    0.000    0.013    0.000 /anaconda/lib/python3.6/site-packages/ipykernel/iostream.py:180(schedule)
      126    0.005    0.000    0.012    0.000 /anaconda/lib/python3.6/site-packages/numpy/core/arrayprint.py:557(fillFormat)
     3008    0.002    0.000    0.002    0.000 <ipython-input-142-6dfb0bcdc2b9>:19(<lambda>)
      378    0.002    0.000    0.002    0.000 {method 'reduce' of 'numpy.ufunc' objects}
      168    0.001    0.000    0.015    0.000 /anaconda/lib/python3.6/site-packages/ipykernel/iostream.py:342(write)
      336    0.001    0.000    0.002    0.000 /anaconda/lib/python3.6/site-packages/numpy/core/numeric.py:2692(seterr)
      126    0.001    0.000    0.001    0.000 {method 'compress' of 'numpy.ndarray' objects}
      336    0.001    0.000    0.001    0.000 /anaconda/lib/python3.6/site-packages/numpy/core/numeric.py:2792(geterr)

And I need to get the tottime from this line:

 21    0.018    0.001    0.054    0.003 <ipython-input-101-80a6d7e4cb13>:3(benchmarkFunction) 

How can I do that?

jedbrown commented 5 years ago

Look at ps.stats, which is a dict containing this information.