Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: efficiency



On Fri, 03 Feb 2017 18:55:53 -0800
Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> echo "\nSTOP TIMER. REAL RUNTIME: $((now-start)) MILISECONDS 
> ($(((now-start)/60000)):${(l:2::0:)$(((now-start)/1000%60))}) $@"
> 
> integer -x base=$(((now-start)/1000))
> echo "\nSTOP TIMER. REAL RUNTIME: $((now-start)) MILISECONDS 
> ($(($base/60)):${(l:2::0:)$(($base%60))}) $@"
> 
> ... is there any real advantage to the second line?  Obviously I'm 
> trying to cut down on duplicate calculation, and in the real world it 
> hardly matters, but as a point of principal, which is better?

The point of principle here is "don't repeat yourself"; it's better to
do every calculation just once.  Probably the main reason for this is
what happens if you decide you need to change the calculation.  (For
example, you might decide to round the calculation differently.  I'm clearly
not suggesting you need to!)  Then you only need to change one place.
Getting used to that does help you avoid pitfalls in less trivial cases...

pws



Messages sorted by: Reverse Date, Date, Thread, Author