[Tutor] Run Time Analysis With Python

Alan Gauld alan.gauld at btinternet.com
Wed Oct 2 19:45:03 EDT 2019


On 02/10/2019 16:37, Ricardo Grant wrote:

> 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)

> 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'm not sure what you mean by that last statement.
The biggest problem in reading the profile data is that your script
is too fast for any of the timings to be meaningful. There may
be options you can use to control granularity - its years since
I used the optimiser!

> I am also curious about my implementation. Does this code suffice, or is 
> there other ways to recurse into dictionaries?
You don't ever need the recursion, although it is the cleanest
and simplest technique if you can be sure you won;t break the
recursion limit. You can always use a loop although its often
a lot messier.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list