[Rcpp-devel] R.e. is std::sort function broken??

Tama Ma pingnang at phys.ethz.ch
Thu Sep 22 19:23:50 CEST 2011


Maybe I can give a slightly more elegant solution from Boost... (fingers crossed...)
In this manner, the ugly for-loop is removed...

For the toy-example, I would suggest the following:

std::vector<double> data;
data.reserve(20);     // extreamly important (maximize cache effect)
std::copy(boost::counting_iterator(0), boost::counting_iterator(20), std::back_inserter(data));
return Rcpp::wrap(data);

However, I do agree with Darren that this is not the place for pure C++ questions.


Best regards,
Tama Ma



On Sep 22, 2011, at 1:59 PM, Darren Cook wrote:

>> I think this is the current prize-winner of inappropriate use of Rcpp...
>> How about using R to sort your vector?!
> 
> There is also the fact that the array is being created ready-sorted :-).
> I assumed this was an artificially simplified example from real code. Or
> a learning toy.
> 
>> This isn't really an Rcpp question, but since you asked, .push_back()
>> is a great way to slow yourself down.
> 
> Or add data.reserve(20) [1] after creating the vector. Then push_back()
> is very quick.
> 
> BTW, apologies for replying to something off-topic. Perhaps, Noah, Stack
> Overflow would be a better place for pure-C++ questions? (Any other
> suggestions anyone?)
> 
> Darren
> 
> [1]: http://www.cplusplus.com/reference/stl/vector/reserve/
> 
>>>               std::vector<double> data;
>>> 
>>>               for(int i=0; i != 20; i++){
>>>                       data.push_back(i);
>>>               }
>>>               std::sort(data.front(), data.back());
>>>               return Rcpp::wrap(data);
> 
> 
> 
> -- 
> Darren Cook, Software Researcher/Developer
> 
> http://dcook.org/work/ (About me and my work)
> http://dcook.org/blogs.html (My blogs and articles)
> _______________________________________________
> 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
> 


Best regards,
Tama Ma 
(+41 78 640 50 15)

pingnang at phys.ethz.ch
www.phys.ethz.ch/~pingnang

HIT K 31.3
Institut für Theoretische Physik
Wolfgang-Pauli-Strasse 27
ETH Hönggerberg
8093 Zürich
Switzerland



More information about the Rcpp-devel mailing list