[Tutor] Efficiency

Steven D'Aprano steve at pearwood.info
Sun Jun 26 08:25:03 CEST 2011


Hello Naheed,

You seem to have messed up your quoting. Text starting with a > is a 
quote, but in your reply, you have written text starting with > as if it 
were written by me. I have fixed the quoting in this reply, but please 
try to watch that.

See below for more comments.


naheed arafat wrote:

> On Sat, Jun 25, 2011 at 9:42 PM, Steven D'Aprano <steve at pearwood.info>wrote:
[...]
>> Define "efficiency".
>>
>> Do you mean:
>>
>> - most efficient for the programmer to write?
>> - easiest to read?
>> - fastest for the compiler to compile?
>> - uses the smallest number of characters in source code?
>> - takes up the least space on disk when compiled?
>> - runs fastest?
>> - uses least memory?
>> - easiest to maintain when you need to make changes?
>> - easiest to debug when you discover a bug?
>>
> I meant "runs fastest" .

Why? Is it a race? Do you win a prize if your code runs in 0.001 second 
instead of 0.002 second? Be careful that you have a good reason for 
caring about "fastest", not just "fast enough".

Writing the fastest code usually means that it:

- uses more memory
- is harder to write
- is harder to read
- is harder to debug
- is harder to maintain

You should be sure that you don't waste your time trying to speed up 
code that is fast enough.


>> Before trying to optimize your code, you should consider whether you are
>> wasting your time or not. Chances are good that you are. You should consider
>> these famous quotes about optimization:
>>
>>
>> "More computing sins are committed in the name of efficiency (without
>> necessarily achieving it) than for any other single reason - including blind
>> stupidity." - W.A. Wulf
>>
>>
> I don't know what is meant by computing sins.. would you please clarify in
> which cases optimization would be a waste of time?

Optimization is nearly always a waste of time. Not always, but nearly.

By "computing sins", he means "doing something you should not do". Examples:

- writing hard to maintain code
- writing code that is fragile instead of robust when it
   receives unexpected input
- writing buggy code (it is astonishing how many tens of
   thousands of hours has been wasted by programmers trying
   to make code that doesn't work correctly run fast)
- spending an hour to try to save one minute
- spending time trying to optimize trivial, unimportant
   parts of the program, while leaving important parts
   slow
- writing code that is actually SLOWER but thinking that
   it must be faster (what I call a pessimation, not an
   optimization)

These are computing sins. Don't do them.


>> "Bottlenecks occur in surprising places, so don't try to second guess and
>> put in a speed hack until you have proven that's where the bottleneck is." -
>> Rob Pike
>>
>>
> what is meant by Bottleneck?


A bottleneck is a place where progress slows because your actions are 
restricted. Consider water in a bottle: the bottle might be 7cm wide at 
the bottom, but where the water comes out, through the neck, it is only 
1.5cm wide:

]+---------------------+
]|                      \
]|                       +---------+
]|                       +---------+
]|                      /
]+---------------------+


The speed at which you can pour water from the bottle depends on the 
width of the neck, not the width of the bottle. Making the bottle wider 
at the bottom won't speed up how fast you can pour water.



[...]
>> I would solve it like this:
>> import itertools
>> a = reversed('How are you'.split(' '))
>> b = 'I am fine'.split(' ')
>> words = itertools.chain(*zip(a, b))
>> ' '.join(words)
>
> zip() takes sequences as argument.Isn't variable a an iterable object? the
> code didn't work.

zip takes any iterable as argument, not just sequences.

It works for me in every version of Python between 2.4 and 3.2. Perhaps 
you should copy and paste the exact code you are running, and the error 
message you get, or the result you get.



-- 
Steven



More information about the Tutor mailing list