[Rcpp-devel] Sugar seems slower than Rcpp.

Romain Francois romain at r-enthusiasts.com
Tue Jan 4 19:16:00 CET 2011


Le 04/01/11 19:09, Douglas Bates a écrit :
> On Tue, Jan 4, 2011 at 12:04 PM, Douglas Bates<bates at stat.wisc.edu>  wrote:
>> On Tue, Jan 4, 2011 at 11:13 AM, Romain Francois
>> <romain at r-enthusiasts.com>  wrote:
>>> Le 04/01/11 17:42, Romain Francois a écrit :
>>>>
>>>> Le 04/01/11 16:35, Andrew Redd a écrit :
>>>>>
>>>>> Cedric,
>>>>> This was addressed about 2 weeks ago on the list, please read the
>>>>> archives. Basic point, Rcpp/sugar will not be faster than basic
>>>>> functions in R. Do something more complicated to see improvements. I
>>>>> would suggest something that involves lots of copies and edits, that
>>>>> will mess with R's memory management but be much more efficient in
>>>>> compiled code.
>>>>>
>>>>> -Andrew
>>>>
>>>> Hmmm. I suspect someone might be able to speed up "sum". Essentially it
>>>> contains lots of tests for NA that might be expensive if none of the
>>>> input data is NA.
>>>>
>>>> Not sure I want to be that someone just yet.
>>>
>>> Also, you might want to consider calculating xV.size() once (out of the
>>> loop) instead of xV.size() times.
>>>
>>> So for example:
>>>
>>> Summing2<- cxxfunction(signature(x="numeric"), '
>>>       NumericVector xV(x);
>>>       double out = 0.0;
>>>       int n  = xV.size() ;
>>>       for(int i=0; i<n; i++) out += xV[i];
>>>       return wrap(out);
>>> ',plugin="Rcpp")
>
> Actually I'm impressed that your version does as well as it does,
> Romain.  I would have thought that using xV[i] would be slower than
> declaring double *xp = xV.begin(); and using xp[i] but it doesn't seem
> to be faster.  Have you done something crafty to speed up xV[i]?

Going to double* was my first move, and then I only realized that size 
was called each time.

I also thought calling [] would be slower. It really should not be as 
inlining, etc is used all over the place. I never understood why 
sometimes it makes things slower.

Maybe in this example the compiler is able to so something smart. Some 
things in this world I don't understand.

>> Another alternative is to use the std algorithms with Rcpp
>>
>> Summing3<- cxxfunction(signature(x="numeric"), '
>>      NumericVector xV(x);
>>      return wrap(std::accumulate(xV.begin(), xV.end(), double()));
>> ',plugin="Rcpp")
>>
>>
>> For me this gives
>>
>>> system.time(Summing1(x));    # Sugar
>>    user  system elapsed
>>   0.110   0.000   0.116
>>> system.time(Summing2(x));    # Rcpp
>>    user  system elapsed
>>   0.040   0.000   0.041
>>> system.time(Summing3(x));    # Rcpp + std::accumulate
>>    user  system elapsed
>>    0.04    0.00    0.04
>>> system.time(sum(x));               # R-base
>>    user  system elapsed
>>   0.030   0.000   0.035
>>
>> but I don't want to claim a great victory in saving 1/1000 th of a
>> second based on a sample of size 1.
>>
>>> For me this is faster than R:
>>>
>>>> n<- 10000000; x<- rnorm(n)
>>>
>>>> system.time(Summing1(x));
>>> utilisateur     système      écoulé
>>>       0.040       0.000       0.039
>>>
>>>> system.time(Summing2(x));
>>> utilisateur     système      écoulé
>>>       0.011       0.000       0.011
>>>
>>>> system.time(sum(x));
>>> utilisateur     système      écoulé
>>>       0.017       0.000       0.017
>>> Someone will deal with the sugar version later.
>>>
>>> Romain
>>>
>>>>> On Tue, Jan 4, 2011 at 8:14 AM, Cedric Ginestet
>>>>> <c.ginestet05 at googlemail.com<mailto:c.ginestet05 at googlemail.com>>  wrote:
>>>>>
>>>>> Happy new year to everyone,
>>>>>
>>>>> I have made a very straightforward comparison of the performance of
>>>>> standard R, Rcpp function and sugar, and found that the latter
>>>>> produces the poorest performance. Let me know what you think and how
>>>>> I could improve such performance assessment.
>>>>>
>>>>> ###################################################
>>>>> Summing1<- cxxfunction(signature(x="numeric"), '
>>>>> NumericVector xV(x);
>>>>> double out = sum(xV);
>>>>> return wrap(out);
>>>>> ',plugin="Rcpp")
>>>>> Summing2<- cxxfunction(signature(x="numeric"), '
>>>>> NumericVector xV(x);
>>>>> double out = 0.0;
>>>>> for(int i=0; i<xV.size(); i++) out += xV[i];
>>>>> return wrap(out);
>>>>> ',plugin="Rcpp")
>>>>> ###################################################
>>>>> # Results.
>>>>> n<- 1000000; x<- rnorm(n)
>>>>> Summing1(x); Summing2(x); sum(x)
>>>>> #######################
>>>>> gives:
>>>>> [1] -396.6129
>>>>> [1] -396.6129
>>>>> [1] -396.6129
>>>>>
>>>>> ###################################################
>>>>> # Time.
>>>>> system.time(Summing1(x)); # Sugar
>>>>> system.time(Summing2(x)); # Rcpp
>>>>> system.time(sum(x)); # R-base
>>>>> ###################
>>>>>> system.time(Summing1(x));
>>>>> user system elapsed
>>>>> 0.016 0.000 0.016
>>>>>> system.time(Summing2(x));
>>>>> user system elapsed
>>>>> 0.008 0.000 0.011
>>>>>> system.time(sum(x));
>>>>> user system elapsed
>>>>> 0.000 0.000 0.003
>>>>>
>>>>>
>>>>> Sugar appears to be the slowest! What about Rcpp basic loop? Why
>>>>> isn't as fast as the standard sum() in R-base?
>>>>> Cheers,
>>>>> Cedric
>>>>>
>>>>> --
>>>>> Cedric Ginestet
>>>>> Centre for Neuroimaging Sciences (L3.04)
>>>>> NIHR Biomedical Research Centre
>>>>> Institute of Psychiatry, Box P089
>>>>> Kings College London
>>>>> De Crespigny Park
>>>>> London
>>>>> SE5 8AF
>>>>
>>>>
>>>
>>>
>>> --
>>> Romain Francois
>>> Professional R Enthusiast
>>> +33(0) 6 28 91 30 30
>>> http://romainfrancois.blog.free.fr
>>> |- http://bit.ly/fT2rZM : highlight 0.2-5
>>> |- http://bit.ly/gpCSpH : Evolution of Rcpp code size
>>> `- http://bit.ly/hovakS : RcppGSL initial release
>>>
>>>
>>> _______________________________________________
>>> Rcpp-devel mailing list
>>> Rcpp-devel at lists.r-forge.r-project.org
>>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>>
>>
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/fT2rZM : highlight 0.2-5
|- http://bit.ly/gpCSpH : Evolution of Rcpp code size
`- http://bit.ly/hovakS : RcppGSL initial release




More information about the Rcpp-devel mailing list