[R] Odp: Programming: loop versus vector oriented

Petr PIKAL petr.pikal at precheza.cz
Mon Sep 20 12:15:13 CEST 2010


Hi Jan

Jan private <jrheinlaender at gmx.de> napsal dne 18.09.2010 12:12:29:

> Hello Petr,
> 
> thank you for your ideas. The split() looks most realistic. 
> 
> What about this idea:
> 
> 1. Define three functions Refun1, Refun2, Refun3 for the three different
> sections of the calculations (same as you suggested)
> 2. lambda = (Re <= 2320) * Refun1(Re)  + ((Re > 2320) && (Re < 65 * dk))
> * Refun2(Re) etc.
> 
> But my thought is that probably the values of RefunXYZ will be
> calculated for every value of Re, even if the condition (Re <= 2320) is
> FALSE (= 0). So that would give a lot of unnecessary function
> evaluations.

Probably yes but it could be only small overhead and you can check speed 
by

system.time(result1 <- your function1)
system.time(result2 <- your function2)

all.equal(result1, result2)

If you do not perceive differences you can wrap function into some loop 
and let it perform several times inside system.time call

Regards
Petr

> 
> Regards,
>    Jan
> 
>



More information about the R-help mailing list