title: How to measure time and RAM usage for recursive function?
body: <p>I want to benchmark QuickSort, and BubbleSort. My task it's measure time, CPU usage, and memory usage for both.
  I wrote code, and in BubbleSort everything works fine. But I have a problem with QuickSort.
  Example script result:</p>
  <pre><code>&gt; How many elements in list?5                                           
  &gt; bubble_sort function took 0.000 ms                                    
  &gt; quick_sort function took 0.000 ms                                     
  &gt; quick_sort function took 0.000 ms                                     
  &gt; quick_sort function took 0.000 ms                                     
  &gt; quick_sort function took 0.000 ms                                     
  &gt; quick_sort function took 0.000 ms                                     
  &gt; quick_sort function took 1.863 ms                                     
  &gt; quick_sort function took 3.360 ms                                     
  &gt; quick_sort function took 5.353 ms                                     
  &gt; quick_sort function took 6.896 ms                                     
  &gt; None                                                                  
  &gt; Filename: main.py                                                     
  &gt; Line #    Mem usage    Increment  Occurences   Line Contents          
  &gt; ============================================================                                                                 5   26.281 MiB   26.273 MiB          10       def wrap(*args,
  &gt; **kwargs):                                                6   26.281 MiB    0.004 MiB          10           time1 = time.time()            
  &gt; 7   26.281 MiB    0.000 MiB          10           ret = f(*args,
  &gt; **kwargs)                                              8   26.281 MiB    0.000 MiB          10           time2 = time.time()                                                   9   26.281 MiB    0.004 MiB          10           print('{:s} function
  &gt; took {:.3f} ms'.format(f.__name__, (time2-time1)*1000.0))             
  &gt; 10                                                                    
  &gt; 11   26.281 MiB    0.000 MiB          10           return ret
  </code></pre>
  <p>As u can see bubble sort give me one line with time, and QS give me 2*list elements.
  Next: RAM result (table below give me result for whole program (?) not for each function.</p>
  <p>Resume: I want to one final time for each function, and memory_profiler info for each function separatly.
  How can I do that?</p>
  <p>My code looks that (it's long but i think for better explanation I need to show u whole):</p>
  <pre><code>import random, time
  
  def timing(f):
      def wrap(*args, **kwargs):
          time1 = time.time()
          ret = f(*args, **kwargs)
          time2 = time.time()
          print('{:s} function took {:.3f} ms'.format(f.__name__, (time2-time1)*1000.0))
  
          return ret
      return wrap
  
  
  def create_random_list(elements_n):
      l = [random.randint(0, 100000) for i in range(elements_n)]
      return l
  
  
  @profile
  @timing
  def bubble_sort(gen_list):
      n = len(gen_list)
  
      for i in range(n - 1):
  
          for j in range(0, n - i - 1):
  
              if gen_list[j] &gt; gen_list[j + 1]:
                  gen_list[j], gen_list[j + 1] = gen_list[j + 1], gen_list[j]
  
      return gen_list
  
  ### QS
  def partition(gen_list, start, end):
      pivot = gen_list[start]
      low = start + 1
      high = end
  
      while True:
  
          while low &lt;= high and gen_list[high] &gt;= pivot:
              high = high - 1
  
          while low &lt;= high and gen_list[low] &lt;= pivot:
              low = low + 1
          if low &lt;= high:
              gen_list[low], gen_list[high] = gen_list[high], gen_list[low]
          else:
              break
  
      gen_list[start], gen_list[high] = gen_list[high], gen_list[start]
  
      return high
  
  
  @profile
  @timing
  def quick_sort(array, start, end):
      if start &gt;= end:
          return
  
      p = partition(array, start, end)
      quick_sort(array, start, p-1)
      quick_sort(array, p+1, end)
  
  
  if __name__ == '__main__':
      num = int(input(&quot;How many elements in list?&quot;))
      test_list = create_random_list(num)
      bubble_sort(test_list)
      print(quick_sort(test_list, 0, len(test_list) - 1))
  </code></pre>