title: How can you get the call tree with Python profilers?
body: <p>I used to use a nice Apple profiler that is built into the System Monitor application. As long as your C++ code was compiled with debug information, you could sample your running application and it would print out an indented tree telling you what percent of the parent function's time was spent in this function (and the body vs. other function calls).</p>
  
  <p>For instance, if main called <code>function_1</code> and <code>function_2</code>, <code>function_2</code> calls <code>function_3</code>, and then main calls <code>function_3</code>:</p>
  
  <pre><code>main (100%, 1% in function body):
      function_1 (9%, 9% in function body):
      function_2 (90%, 85% in function body):
          function_3 (100%, 100% in function body)
      function_3 (1%, 1% in function body)
  </code></pre>
  
  <p>I would see this and think, "Something is taking a long time in the code in the body of <code>function_2</code>. If I want my program to be faster, that's where I should start."</p>
  
  <p><strong>How can I most easily get this exact profiling output for a Python program?</strong></p>
  
  <p>I've seen people say to do this:</p>
  
  <pre><code>import cProfile, pstats
  prof = cProfile.Profile()
  prof = prof.runctx("real_main(argv)", globals(), locals())
  stats = pstats.Stats(prof)
  stats.sort_stats("time")  # Or cumulative
  stats.print_stats(80)  # 80 = how many to print
  </code></pre>
  
  <p>But it's quite messy compared to that elegant call tree. Please let me know if you can easily do this, it would help quite a bit.</p>