cProfile with imports
Question: I'm currently in the process of learn how to use cProfile and I have a few
doubts.

I'm currently trying to profile the following script:

    
    
    import time
    
    def fast():
        print("Fast!")
    
    def slow():
        time.sleep(3)
        print("Slow!")
    
    def medium():
        time.sleep(0.5)
        print("Medium!")
    
    fast()
    slow()
    medium()
    

I execute the command `python -m cProfile test_cprofile.py` and I have the
following result:

    
    
    Fast!
    Slow!
    Medium!
         7 function calls in 3.504 seconds
    
    Ordered by: standard name
    
    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    3.504    3.504 test_cprofile.py:1(<module>)
        1    0.000    0.000    0.501    0.501 test_cprofile.py:10(medium)
        1    0.000    0.000    0.000    0.000 test_cprofile.py:3(fast)
        1    0.000    0.000    3.003    3.003 test_cprofile.py:6(slow)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        2    3.504    1.752    3.504    1.752 {time.sleep}
    

However, when I edit the script with a pylab import for example (`import pylab`) on the top, the output of cProfile is very large. I tried to limit the number of lines using `python -m cProfile test_cprofile.py | head -n 10` however I receive the following error:
    
    
    Traceback (most recent call last):
    File "/home/user/anaconda/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
    File "/home/user/anaconda/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
    File "/home/user/anaconda/lib/python2.7/cProfile.py", line 199, in <module>
    main()
    File "/home/user/anaconda/lib/python2.7/cProfile.py", line 192, in main
    runctx(code, globs, None, options.outfile, options.sort)
    File "/home/user/anaconda/lib/python2.7/cProfile.py", line 56, in runctx
    result = prof.print_stats(sort)
    File "/home/user/anaconda/lib/python2.7/cProfile.py", line 81, in print_stats
    pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
    File "/home/user/anaconda/lib/python2.7/pstats.py", line 360, in print_stats
    self.print_line(func)
    File "/home/user/anaconda/lib/python2.7/pstats.py", line 438, in print_line
    print >> self.stream, c.rjust(9),
    IOError: [Errno 32] Broken pipe
    

Can someone help what is the correct procedure to situations similar with this
one, where we have a import pylab or another module that generates such high
output information on cProfile?


Answer: I don't know of a way to do selective profiling like you want by running the
`cProfile` module directly from the command line like that.

However you can do it by modifying the your code to explicitly import the
module, but you have to do everything yourself. Here's how that might be done
to your example code:

    
    
    from cProfile import Profile
    from pstats import Stats
    prof = Profile()
    
    prof.disable()  # i.e. don't time imports
    import time
    prof.enable()  # profiling back on
    
    def fast():
        print("Fast!")
    
    def slow():
        time.sleep(3)
        print("Slow!")
    
    def medium():
        time.sleep(0.5)
        print("Medium!")
    
    fast()
    slow()
    medium()
    
    prof.disable()  # don't profile the generation of stats
    prof.dump_stats('mystats.stats')
    with open('mystats_output.txt', 'wt') as output:
        stats = Stats('mystats.stats', stream=output)
        stats.sort_stats('cumulative', 'time')
        stats.print_stats()
    

Contents of `mystats_output.txt` after running:

    
    
    Sun Aug 02 16:55:38 2015    mystats.stats
    
             6 function calls in 3.522 seconds
    
       Ordered by: cumulative time, internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            2    3.522    1.761    3.522    1.761 {time.sleep}
            1    0.000    0.000    3.007    3.007 cprofile-with-imports.py:15(slow)
            1    0.000    0.000    0.515    0.515 cprofile-with-imports.py:19(medium)
            1    0.000    0.000    0.000    0.000 cprofile-with-imports.py:12(fast)
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    

