[R] Optimizing C code

Henrik Bengtsson hb at stat.berkeley.edu
Fri Jan 22 19:29:58 CET 2010


A side note: The NA vs NaN does not seem to play a role here, because:

#define both_non_NA(a,b) (!ISNAN(a) && !ISNAN(b))

So, it is the same type of test used in line 9 and in line 11.

/Henrik

On Fri, Jan 22, 2010 at 9:52 AM, Christophe Genolini
<cgenolin at u-paris10.fr> wrote:
> Thanks both of you.
>
>>
>> > Inf - Inf
>> [1] NaN
>
> So isn't the line 9 useless ? If either x[i] or y[i] are NA, then dev will
> be NA and !ISNAN(dev) will detect it...
> Sothe loop cool be
>
> 8.    for(i = 0 ; i < taille ; i++) {
> 10.        dev = (x[i] - y[i]);
> 11.        if(!ISNAN(dev)) {
> 12.          dist += dev * dev;
> 13.          count++;
> 15.      }
> 16. }
>
> No ?
>
> Christophe
>>
>> Duncan Murdoch
>>>
>>> Christophe
>>>
>>>
>>> #define both_FINITE(a,b) (R_FINITE(a) && R_FINITE(b))
>>> #define both_non_NA(a,b) (!ISNAN(a) && !ISNAN(b))
>>>
>>> 1. static double R_euclidean2(double *x, double *y, int taille)
>>> 2. {
>>> 3.    double dev, dist;
>>> 4.    int count, i;
>>> 5.
>>> 6.    count= 0;
>>> 7.    dist = 0;
>>> 8.    for(i = 0 ; i < taille ; i++) {
>>> 9.    if(both_non_NA(x[i], y[i])) {
>>> 10.        dev = (x[i] - y[i]);
>>> 11.        if(!ISNAN(dev)) {
>>> 12.        dist += dev * dev;
>>> 13.        count++;
>>> 14.        }
>>> 15.    }
>>> 16.    }
>>> 17.    if(count == 0)return NA_REAL;
>>> 18.    if(count != taille) dist /= ((double)count/taille);
>>> 19.    return sqrt(dist);
>>> 20.}
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list