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

Re: efficiency



On 04/02/17 07:05 AM, Peter Stephenson wrote:
On Fri, 03 Feb 2017 18:55:53 -0800
Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

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

Thanks Peter. Yes I understand that as a point of general programming, I think what I was really asking is if, in an interpreted language the time spent calculating is offset by the time spent parsing. That is, in C you'd be more efficient doing the calculation once (besides what you say above), but maybe in zsh the important thing is to reduce the number of lines or the number of variables or something like that. Or, on the contrary, maybe the simpler form is doubly better since the lines are shorter and thus save parsing time as well as calculation time.

Actually, I just realized that the thing can time itself:

$ . ./timer; timer start; timer start; timer; timer stop; timer stop

, so I tried both ways and:

echo "\nSTOP TIMER. REAL RUNTIME: $((now-start)) MILISECONDS
($(((now-start)/60000)):${(l:2::0:)$(((now-start)/1000%60))}) $@"

... turns out to be faster by a whopping milisecond or two, which surprises me. But as you say, the other way is better code of course. This isn't important, I'm just curious.




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