[Tutor] Run Time Analysis With Python

Mats Wichmann mats at wichmann.us
Wed Oct 2 22:21:47 EDT 2019


On 10/2/19 9:37 AM, Ricardo Grant wrote:
> I am trying to learn more about performance analysis using python. I 
> made a small script to extract titles and uri's from Firefox's bookmark 
> JSON dump. Here is the script:
> 
> #!/usr/bin/python3
> 
> import json
> 
> bookmarks = json.load(open('/home/ricardo/bookmarks-2019-08-20.json'))
> 
> def descend(dict):
>      if 'children' in dict:
>          for child in dict['children']:
>              if 'uri' in child:
>                  print('{} {}'.format(child['title'], child['uri']))
>              else:
>                  descend(child)
> 
> and the profiling information
> 
>           103 function calls (92 primitive calls) in 0.000 seconds
> 
>     Ordered by: standard name
> 
>     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>          1    0.000    0.000    0.000    0.000 <string>:1(<module>)
>       12/1    0.000    0.000    0.000    0.000 bookmarks.py:8(descend)
>          1    0.000    0.000    0.000    0.000 {built-in method 
> builtins.exec}
>         44    0.000    0.000    0.000    0.000 {built-in method 
> builtins.print}
>          1    0.000    0.000    0.000    0.000 {method 'disable' of 
> '_lsprof.Profiler' objects}
>         44    0.000    0.000    0.000    0.000 {method 'format' of 'str' 
> objects}
> 
> Due to it's recursive nature and my limited understanding of big O 
> analysis, I can't really find the steps to define a function that 
> describes the performance of this script. I would appreciate some hints 
> or even an explanation. Can you explain the profiling information as well?

what's to describe?  as a result of running this (and note you have not 
sent us a complete program) the builtins print and format have been 
called 44 times. probably that's right, but you could have gleaned the 
same information by counting keys in bookmarks. exciting.

what are you trying to determing from your analysis? how are you calling 
the profiling?

> I am also curious about my implementation. Does this code suffice, or is 
> there other ways to recurse into dictionaries?

there's a joke cartoon making the rounds of the internet just now which 
describes how when computer science students are about to graduate with 
an advanced degree they're taken into a dark room and told to never ever 
use recursion again, it was only a trick to torment them during their 
learning (I'm paraphrasing, but that's the gist).  you are recursing. 
are you sure you need to?  while it has its uses, iteration is usually a 
sufficient (and better) way to walk through a collection like a dictionary.

This is not meant to be critical, just some questions...



More information about the Tutor mailing list