[Tutor] How inefficient is this code?

Alan Gauld alan.gauld at btinternet.com
Thu May 8 03:07:48 CEST 2014


On 08/05/14 01:42, C Smith wrote:
> I guess intuiting efficiency doesn't work in Python because it is such
> high-level? Or is there much more going on there?

Efficiency is usually language independent because its the algorithm, or 
design, that determines efficiency in most cases.

Your example is a good case in point. The best efficiency improvement is 
to eliminate the second loop by doing the sum inside the first loop. 
That would be true in any language.

If you find yourself doing language specific tweaks to improve 
efficiency then you are probably doing the wrong thing. And
you should definitely be using the profiler to prove that
what you are doing is the correct thing. But mostly you will
want to change the algorithm before attempting to tweak the
inner workings of the code.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list