<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>R-bloggers &#187; Rcpp</title>
	<atom:link href="http://www.r-bloggers.com/tag/rcpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r-bloggers.com</link>
	<description>R news and tutorials contributed by (300) R bloggers</description>
	<lastBuildDate>Tue, 07 Feb 2012 10:27:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Vectorized R vs Rcpp</title>
		<link>http://www.r-bloggers.com/vectorized-r-vs-rcpp/</link>
		<comments>http://www.r-bloggers.com/vectorized-r-vs-rcpp/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 20:03:09 +0000</pubDate>
		<dc:creator>Dzidorius Martinaitis</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[EN]]></category>
		<category><![CDATA[quant]]></category>
		<category><![CDATA[quantitative]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[Rcpp]]></category>

		<guid isPermaLink="false">http://www.investuotojas.eu/?p=730</guid>
		<description><![CDATA[In my previous post, I tried to show, that Rcpp is 1000 faster than pure R and that generated the fuss in the comments. Being lazy, I didn&#8217;t vectorize R code and at the end I was comparing apples vs oranges. To fix that problem, I built a new script, where I&#8217;m trying to compare [...]]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://www.investuotojas.eu/2012/02/01/vectorized-r-vs-rcpp/"> Quantitative thoughts » EN</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
<p><a href="http://www.investuotojas.eu/2012/01/30/the-power-of-rcpp/" ref="nofollow" target="_blank">In my previous post</a>, I tried to show, that Rcpp is 1000 faster than pure R and that generated the fuss in the comments. Being lazy, I didn&#8217;t vectorize R code and at the end I was comparing apples vs oranges.</p>
<p>To fix that problem, I built a new script, where I&#8217;m trying to compare apples against apples. First piece of code named &#8220;ifelse R&#8221; uses R &#8220;ifelse&#8221; function to vectorize code. Second piece of code is fully vectorized code written in R, third &#8211; pure C++ code and the last one is C++, where  Rcpp &#8221;ifelse&#8221; function is used.</p>
<p><a href="http://s176.photobucket.com/albums/w180/investuotojas/?action=view&amp;current=performance.png" ref="nofollow" target="_blank"><img src="http://i176.photobucket.com/albums/w180/investuotojas/performance.png" alt="Photobucket" border="0" /></a></p>
<p>&nbsp;</p>
<table border="0">
<tbody>
<tr>
<th>name</th>
<th>seconds</th>
</tr>
<tr>
<td align="right">ifelse R</td>
<td align="right">27.50</td>
</tr>
<tr>
<td align="right">vectorized R</td>
<td align="right">10.40</td>
</tr>
<tr>
<td align="right">pure C++</td>
<td align="right">0.44</td>
</tr>
<tr>
<td align="right">vectorized C++</td>
<td align="right">2.24</td>
</tr>
</tbody>
</table>
<p>Here we go &#8211; vectorization truly helps, but pure C++ code still 23 times faster. Of course you pay the price when writing it in C++.<br />
I found a bit strange, that vectorized C++ code doesn&#8217;t perform that well&#8230;</p>
<p>You can get the code from <a href="https://github.com/kafka399/Rproject/blob/master/performance/performance.R" ref="nofollow" target="_blank">github</a> or review it below:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" title="WP-CodeBox HowTo?" ref="nofollow" target="_blank"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p730code2'); return false;" ref="nofollow" target="_blank">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7302"><td class="code" id="p730code2"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#Author Dzidorius Martinaitis</span>
<span style="color: #228B22;">#Date 2012-02-01</span>
<span style="color: #228B22;">#Description http://www.investuotojas.eu/2012/02/01/vectorized-r-vs-rcpp</span>
&nbsp;
bid <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">runif</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">50000000</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">9</span><span style="color: #080;">&#41;</span>
ask <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">runif</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">50000000</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">9</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/close.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">close</span></a> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">runif</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">50000000</span>,<span style="color: #ff0000;">5</span>,<span style="color: #ff0000;">9</span><span style="color: #080;">&#41;</span>
&nbsp;
x<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>bid<span style="color: #080;">=</span>bid,ask<span style="color: #080;">=</span>ask,last_price<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/close.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">close</span></a><span style="color: #080;">&#41;</span>
rez<span style="color: #080;">=</span><span style="color: #ff0000;">0</span>
&nbsp;
<span style="color: #228B22;">###########    ifelse R  #################</span>
answ<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/system.time.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">system.<span style="">time</span></span></a><span style="color: #080;">&#40;</span>
<span style="color: #080;">&#123;</span>
rez <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>x$last_price<span style="color: #080;">&gt;</span><span style="color: #ff0000;">0</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;bid&quot;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&gt;</span> x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;last_price&quot;</span><span style="color: #080;">&#93;</span>, x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;bid&quot;</span><span style="color: #080;">&#93;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/ifelse.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">ifelse</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;ask&quot;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&amp;</span> <span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;ask&quot;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;</span> x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;last_price&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>, x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;ask&quot;</span><span style="color: #080;">&#93;</span>, x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;last_price&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">0.5</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#91;</span>, <span style="color: #ff0000;">&quot;ask&quot;</span><span style="color: #080;">&#93;</span> <span style="color: #080;">+</span> x<span style="color: #080;">&#91;</span>,<span style="color: #ff0000;">&quot;bid&quot;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">###########   end ifelse R  #################</span>
&nbsp;
<span style="color: #228B22;">###########    vectorized R  #################</span>
&nbsp;
answ<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/append.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">append</span></a><span style="color: #080;">&#40;</span>answ,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/system.time.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">system.<span style="">time</span></span></a><span style="color: #080;">&#40;</span>
<span style="color: #080;">&#123;</span>
lgt0 <span style="color: #080;">=</span> x$last_price <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">0</span>
bgtl <span style="color: #080;">=</span> x$bid <span style="color: #080;">&gt;</span> x$last_price
agt0 <span style="color: #080;">=</span> x$ask <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">0</span>
altl <span style="color: #080;">=</span> x$ask <span style="color: #080;">&gt;</span> x$last_price
rez <span style="color: #080;">=</span> x$last_price
rez<span style="color: #080;">&#91;</span>lgt0 <span style="color: #080;">&amp;</span> agt0 <span style="color: #080;">&amp;</span> altl<span style="color: #080;">&#93;</span> <span style="color: #080;">=</span> x$ask<span style="color: #080;">&#91;</span>lgt0 <span style="color: #080;">&amp;</span> agt0 <span style="color: #080;">&amp;</span> altl<span style="color: #080;">&#93;</span>
rez<span style="color: #080;">&#91;</span>lgt0 <span style="color: #080;">&amp;</span> bgtl<span style="color: #080;">&#93;</span> <span style="color: #080;">=</span> x$bid<span style="color: #080;">&#91;</span>lgt0 <span style="color: #080;">&amp;</span> bgtl<span style="color: #080;">&#93;</span>
rez<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>lgt0<span style="color: #080;">&#93;</span> <span style="color: #080;">=</span> <span style="color: #080;">&#40;</span>x$ask<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>lgt0<span style="color: #080;">&#93;</span><span style="color: #080;">+</span>x$bid<span style="color: #080;">&#91;</span><span style="color: #080;">!</span>lgt0<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
<span style="color: #080;">&#125;</span>
<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">###########   end vectorized R  #################</span>
&nbsp;
<span style="color: #228B22;">#C++ code starts here</span>
&nbsp;
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>inline<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>Rcpp<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">###########    pure C++  #################</span>
&nbsp;
code<span style="color: #080;">=</span><span style="color: #ff0000;">'
NumericVector bid(bid_);NumericVector ask(ask_);NumericVector close(close_);NumericVector ret(ask_);
int bid_size = bid.size();
for(int i =0;i&lt;bid_size;i++)
{
  if(close[i]&gt;0)
  {
    if(bid[i]&gt;close[i])
    {
      ret[i] = bid[i]; 
    }
    else if(ask[i]&gt;0 &amp;&amp; ask[i]&lt;close[i])
    {
      ret[i] = ask[i];//
    }
    else
    {
      ret[i] = close[i];//
    }
  }
  else
  {
    ret[i]=(bid[i]+ask[i])/2;
  }
&nbsp;
}
return ret;
'</span>
getLastPrice <span style="color: #080;">&lt;-</span> cxxfunction<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/base/html/Log.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">signature</span></a><span style="color: #080;">&#40;</span> bid_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>,ask_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>,close_<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;numeric&quot;</span><span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/body.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">body</span></a><span style="color: #080;">=</span>code,plugin<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Rcpp&quot;</span><span style="color: #080;">&#41;</span>
rez<span style="color: #080;">=</span><span style="color: #ff0000;">0</span>
answ<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/append.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">append</span></a><span style="color: #080;">&#40;</span>answ,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/system.time.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">system.<span style="">time</span></span></a><span style="color: #080;">&#40;</span>
  <span style="color: #080;">&#123;</span>
    rez<span style="color: #080;">=</span>getLastPrice<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$bid<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$ask<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$last_price<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">###########   end pure C++  #################</span>
&nbsp;
<span style="color: #228B22;">#summary(rez)</span>
&nbsp;
&nbsp;
<span style="color: #228B22;">###########    vectorized C++  #################</span>
code<span style="color: #080;">=</span><span style="color: #ff0000;">'
NumericVector bid(bid_);NumericVector ask(ask_);NumericVector close(close_);NumericVector ret(ask_);
int bid_size = bid.size();
ret=ifelse(close&gt;0,ifelse(bid &gt;close, bid, ifelse(ask &gt; 0,ifelse(ask &lt; close,ask, close),close)), 0.5*(ask + bid));
return ret;
'</span>
getLastPrice <span style="color: #080;">&lt;-</span> cxxfunction<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/base/html/Log.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">signature</span></a><span style="color: #080;">&#40;</span> bid_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>,ask_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>,close_<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;numeric&quot;</span><span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/body.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">body</span></a><span style="color: #080;">=</span>code,plugin<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Rcpp&quot;</span><span style="color: #080;">&#41;</span>
rez<span style="color: #080;">=</span><span style="color: #ff0000;">0</span>
answ<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/append.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">append</span></a><span style="color: #080;">&#40;</span>answ,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/system.time.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">system.<span style="">time</span></span></a><span style="color: #080;">&#40;</span>
<span style="color: #080;">&#123;</span>
  rez<span style="color: #080;">=</span>getLastPrice<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$bid<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$ask<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$last_price<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">###########   end vectorized C++  #################</span>
&nbsp;
<span style="color: #228B22;">#summary(rez)</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>answ<span style="color: #080;">&#41;</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'ifelse R'</span>,<span style="color: #ff0000;">'vectorized R'</span>,<span style="color: #ff0000;">'pure C++'</span>,<span style="color: #ff0000;">'vectorized C++'</span><span style="color: #080;">&#41;</span>
&nbsp;
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
a<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>ind<span style="color: #080;">=</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">4</span>,val<span style="color: #080;">=</span>answ<span style="color: #080;">&#41;</span>
ggplot<span style="color: #080;">&#40;</span>a,aes<span style="color: #080;">&#40;</span>ind,val<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">+</span>geom_point<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/legend.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">legend</span></a><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/F.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">F</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">+</span>geom_text<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>label<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/names.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">names</span></a><span style="color: #080;">&#40;</span>answ<span style="color: #080;">&#41;</span>,hjust<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span><span style="color: #ff0000;">0.2</span>,<span style="color: #080;">-</span><span style="color: #ff0000;">0.2</span>,<span style="color: #080;">-</span><span style="color: #ff0000;">0.2</span>,<span style="color: #ff0000;">0.8</span><span style="color: #080;">&#41;</span>,vjust<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0</span>,<span style="color: #ff0000;">0</span>,<span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,size<span style="color: #080;">=</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>


<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://www.investuotojas.eu/2012/02/01/vectorized-r-vs-rcpp/"> Quantitative thoughts » EN</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/vectorized-r-vs-rcpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The power of Rcpp</title>
		<link>http://www.r-bloggers.com/the-power-of-rcpp/</link>
		<comments>http://www.r-bloggers.com/the-power-of-rcpp/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 21:51:38 +0000</pubDate>
		<dc:creator>Dzidorius Martinaitis</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[EN]]></category>
		<category><![CDATA[quant]]></category>
		<category><![CDATA[quantitative]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[Rcpp]]></category>

		<guid isPermaLink="false">http://www.investuotojas.eu/?p=703</guid>
		<description><![CDATA[While ago I built two R scripts to track OMX Baltic Benchmark Fund against the index. One script returns the deviation of  fund from the index and it works fast enough. The second calculates the value of the fund every minute and it used to take for while. For example, it spent 2 minutes or [...]]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://www.investuotojas.eu/2012/01/30/the-power-of-rcpp/"> Quantitative thoughts » EN</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
<p>While ago I built two R scripts to track <a href="http://markets.ft.com/research/Markets/Tearsheets/Financials?s=OAMOBBF1L:VLX" ref="nofollow" target="_blank">OMX Baltic Benchmark Fund</a> against the index. One script returns the deviation of  fund from the index and it works fast enough. The second calculates the value of the fund every minute and it used to take for while. For example, it spent 2 minutes or more to get the values for one day. Here is an example of the result:</p>
<p><a href="http://s176.photobucket.com/albums/w180/investuotojas/?action=view&amp;current=ind.png" ref="nofollow" target="_blank"><img src="http://i176.photobucket.com/albums/w180/investuotojas/ind.png" alt="Photobucket" border="0" /></a></p>
<p>Following piece of code was in question:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" title="WP-CodeBox HowTo?" ref="nofollow" target="_blank"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p703code3'); return false;" ref="nofollow" target="_blank">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7033"><td class="code" id="p703code3"><pre class="rsplus" style="font-family:monospace;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/for.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">for</span></a><span style="color: #080;">&#40;</span>y <span style="color: #0000FF; font-weight: bold;">in</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/NROW.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">NROW</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
 <span style="color: #080;">&#123;</span>
    z<span style="color: #080;">=</span>x<span style="color: #080;">&#91;</span>y,<span style="color: #080;">&#93;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>z$last_price<span style="color: #080;">&amp;</span>gt<span style="color: #080;">;</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <span style="color: #080;">&#123;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>z$bid<span style="color: #080;">&amp;</span>gt<span style="color: #080;">;</span>z$last_price<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>rez<span style="color: #080;">&#91;</span>y<span style="color: #080;">&#93;</span><span style="color: #080;">=</span>z$bid
      <span style="color: #0000FF; font-weight: bold;">else</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>z$ask<span style="color: #080;">&amp;</span>gt<span style="color: #080;">;</span><span style="color: #ff0000;">0</span> <span style="color: #080;">&amp;</span>amp<span style="color: #080;">;</span> z$ask<span style="color: #080;">&amp;</span>lt<span style="color: #080;">;</span>z$last_price<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>rez<span style="color: #080;">&#91;</span>y<span style="color: #080;">&#93;</span><span style="color: #080;">=</span>z$ask
      <span style="color: #0000FF; font-weight: bold;">else</span> rez<span style="color: #080;">&#91;</span>y<span style="color: #080;">&#93;</span><span style="color: #080;">=</span>z$last_price
    <span style="color: #080;">&#125;</span>
    <span style="color: #0000FF; font-weight: bold;">else</span>
    <span style="color: #080;">&#123;</span>
      rez<span style="color: #080;">&#91;</span>y<span style="color: #080;">&#93;</span><span style="color: #080;">=</span><span style="color: #080;">&#40;</span>z$ask<span style="color: #080;">+</span>z$bid<span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
    <span style="color: #080;">&#125;</span>
 <span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>The code above loops over time series and based on set of rules tries to decide which price (bid, ask or previous one) to use for calculations. Pure R script used to take 100 seconds to derive the price.</p>
<p>During the weekend I found time to watch very interesting <a href="http://goo.gl/zzq0B" ref="nofollow" target="_blank">Rcpp presentation</a>. To my surprise, there are numerous ways to seamlessly integrate C++ into R code. So, I decided to rewrite the code above in C++ (Rcpp and inline packages were used).</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" title="WP-CodeBox HowTo?" ref="nofollow" target="_blank"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p703code4'); return false;" ref="nofollow" target="_blank">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7034"><td class="code" id="p703code4"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#c++ code embed in code value</span>
code<span style="color: #080;">=</span><span style="color: #ff0000;">'
NumericVector bid(bid_);NumericVector ask(ask_);NumericVector close(close_);NumericVector ret(ask_);
int bid_size = bid.size();
for(int i =0;i&amp;lt;bid_size;i++)
{
  if(close[i]&amp;gt;0)
  {
    if(bid[i]&amp;gt;close[i])
    {
      ret[i] = bid[i];
    }
    else if(ask[i]&amp;gt;0 &amp;amp;&amp;amp; ask[i]&amp;lt;close[i])
    {
      ret[i] = ask[i];//
    }
    else
    {
      ret[i] = close[i];//
    }
  }
  else
  {
    ret[i]=(bid[i]+ask[i])/2;
  }
&nbsp;
}
return ret;
'</span>
<span style="color: #228B22;">#a glue function between C++ and R</span>
getLastPrice <span style="color: #080;">=</span> cxxfunction<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/base/html/Log.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">signature</span></a><span style="color: #080;">&#40;</span> bid_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>,ask_ <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;numeric&quot;</span>,close_<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;numeric&quot;</span><span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/body.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">body</span></a><span style="color: #080;">=</span>code,plugin<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Rcpp&quot;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#and the call of the function</span>
getLastPrice<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$bid<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$ask<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.numeric.html" ref="nofollow" target="_blank"><span style="color: #0000FF; font-weight: bold;">as.<span style="">numeric</span></span></a><span style="color: #080;">&#40;</span>x$last_price<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>What did I get in return? Well, 0.1 of a second instead of 100 seconds!</p>

<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://www.investuotojas.eu/2012/01/30/the-power-of-rcpp/"> Quantitative thoughts » EN</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/the-power-of-rcpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Rcpp benchmarks</title>
		<link>http://www.r-bloggers.com/some-rcpp-benchmarks/</link>
		<comments>http://www.r-bloggers.com/some-rcpp-benchmarks/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 22:27:00 +0000</pubDate>
		<dc:creator>Shige</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>

		<guid isPermaLink="false">http://www.r-bloggers.com/?guid=dcc4d99330b51badca0723392b8f138e</guid>
		<description><![CDATA[I ran the Fibonacci number example from the Rcpp package on a number of computers and operating systems. Here are the results:A. On my main computer (Core 2 Extreme 3.06GHz, 8 GB memory) running Ubuntu 10.04 (g++ 4.4.3):&#160; &#160; &#160; &#160; test...]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://sgsong.blogspot.com/2012/01/some-rcpp-benchmarks.html"> Shige's Research Blog</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
I ran the <a href="http://dirk.eddelbuettel.com/code/rcpp/html/Misc_2fibonacci_8r-example.html" ref="nofollow" target="_blank">Fibonacci number example</a> from the Rcpp package on a number of computers and operating systems. Here are the results:<br /><div><br /></div><div>A. On my main computer (Core 2 Extreme 3.06GHz, 8 GB memory) running Ubuntu 10.04 (g++ 4.4.3):</div><div><div><span style="font-family: 'Courier New', Courier, monospace;">&nbsp; &nbsp; &nbsp; &nbsp; test replications elapsed relative user.self sys.self</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">3 fibRcpp(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 0.148 &nbsp; 1.0000 &nbsp; &nbsp; &nbsp;0.14 &nbsp; &nbsp; 0.01</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">1 &nbsp; &nbsp;fibR(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp;87.078 588.3649 &nbsp; &nbsp; 87.03 &nbsp; &nbsp; 0.04</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">2 &nbsp; fibRC(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp;91.209 616.2770 &nbsp; &nbsp; 91.14 &nbsp; &nbsp; 0.07</span></div></div><div><br /></div><div>B. Same computer running Windows Vista (g++ 4.5.0):<br /><br /><span style="font-family: 'Courier New', Courier, monospace;">&nbsp; &nbsp; &nbsp; &nbsp; test replications elapsed relative user.self sys.self</span><br /><span style="font-family: 'Courier New', Courier, monospace;">3 fibRcpp(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp;0.21 &nbsp; 1.0000 &nbsp; &nbsp; &nbsp;0.21 &nbsp; &nbsp; 0.00</span><br /><span style="font-family: 'Courier New', Courier, monospace;">1 &nbsp; &nbsp;fibR(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 92.08 438.4762 &nbsp; &nbsp; 90.47 &nbsp; &nbsp; 0.05</span><br /><span style="font-family: 'Courier New', Courier, monospace;">2 &nbsp; fibRC(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 94.39 449.4762 &nbsp; &nbsp; 93.13 &nbsp; &nbsp; 0.03</span><br /><br /><br /><span style="font-family: inherit;">C. On my second laptop (Core 2 Duo 2.53GHz, 4 GB memory) running Windows 7 (g++ 4.5.0):</span></div><div><br /><span style="font-family: 'Courier New', Courier, monospace;">&nbsp; &nbsp; &nbsp; &nbsp; test replications elapsed relative user.self sys.self</span><br /><span style="font-family: 'Courier New', Courier, monospace;">3 fibRcpp(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp;0.17 &nbsp; 1.0000 &nbsp; &nbsp; &nbsp;0.17 &nbsp; &nbsp; 0.00</span><br /><span style="font-family: 'Courier New', Courier, monospace;">1 &nbsp; &nbsp;fibR(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 73.62 433.0588 &nbsp; &nbsp; 73.47 &nbsp; &nbsp; 0.00</span><br /><span style="font-family: 'Courier New', Courier, monospace;">2 &nbsp; fibRC(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 74.27 436.8824 &nbsp; &nbsp; 74.20 &nbsp; &nbsp; 0.03</span></div><div><br />D. On my third laptop (Core 2 Duo 2.50GHz, 2 GB memory) running Mint Debian (g++ 4.6.2):</div><div><div><div><span style="font-family: 'Courier New', Courier, monospace;">&nbsp; &nbsp; &nbsp; &nbsp; test replications elapsed relative user.self sys.self</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">3 fibRcpp(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 0.148 &nbsp; 1.0000 &nbsp; &nbsp; 0.148 &nbsp; &nbsp; 0.00</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">1 &nbsp; &nbsp;fibR(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp;65.535 442.8041 &nbsp; &nbsp; 65.328 &nbsp; &nbsp;0.200</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">2 &nbsp; fibRC(N) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp;65.664 443.6757 &nbsp; &nbsp; 65.492 &nbsp; &nbsp;0.172</span></div></div></div><div><span style="font-family: 'Courier New', Courier, monospace;"><br /></span><br /><span style="font-family: inherit;">Why the faster computer performed worse, on both R and Rcpp versions?</span></div><div><br /></div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/35916379-4410081609614832171?l=sgsong.blogspot.com' alt='' /></div>
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://sgsong.blogspot.com/2012/01/some-rcpp-benchmarks.html"> Shige's Research Blog</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/some-rcpp-benchmarks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8230; And now for solution 17, still using Rcpp</title>
		<link>http://www.r-bloggers.com/and-now-for-solution-17-still-using-rcpp/</link>
		<comments>http://www.r-bloggers.com/and-now-for-solution-17-still-using-rcpp/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 12:35:00 +0000</pubDate>
		<dc:creator>romain francois</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>

		<guid isPermaLink="false">http://www.r-bloggers.com/?guid=da2a1ce1a53b476bd7f6892a386c90ff</guid>
		<description><![CDATA[    Here comes yet another sequel of the code optimization problem from the R wiki, still using Rcpp, but with a different strategy this time

Essentially, my previous version (15) was using stringstream although we don't really need its functionality ...]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/12/14/...-And-now-for-solution-17,-still-using-Rcpp"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
    <p>Here comes yet another sequel of the code optimization problem from the <a href="http://rwiki.sciviews.org/doku.php?id=tips:programming:code_optim2" ref="nofollow" target="_blank">R wiki</a>, still using <a href="http://dirk.eddelbuettel.com/code/rcpp.html" ref="nofollow" target="_blank">Rcpp</a>, but with a different strategy this time</p>

<p>Essentially, my <a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/10/Code-optimization,-an-Rcpp-solution" ref="nofollow" target="_blank">previous version (15)</a> was using stringstream although we don't really need its functionality and it was slowing us down</p>

<p>Also, the characters "i" and "." are always on the same position so we can assign them once and for all</p>

<p>So without further ado, here is attempt 17:</p>

<iframe src="http://romainfrancois.blog.free.fr/public/packages/Rcpp/optim/index17.html" width=500 height=400></iframe>

<p>With quite a speedup from attempt 15:</p>

<pre>
                test replications elapsed relative
2 generateIndex17(n)           20   9.363 1.000000
1 generateIndex15(n)           20  17.795 1.900566
</pre>
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/12/14/...-And-now-for-solution-17,-still-using-Rcpp"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/and-now-for-solution-17-still-using-rcpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Optimization: One R Problem, Thirteen Solutions – Now Sixteen!</title>
		<link>http://www.r-bloggers.com/code-optimization-one-r-problem-thirteen-solutions-%e2%80%93-now-sixteen/</link>
		<comments>http://www.r-bloggers.com/code-optimization-one-r-problem-thirteen-solutions-%e2%80%93-now-sixteen/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 13:41:05 +0000</pubDate>
		<dc:creator>Tony Breyal</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[optimisation]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>
		<category><![CDATA[rstats]]></category>

		<guid isPermaLink="false">http://tonybreyal.wordpress.com/?p=548</guid>
		<description><![CDATA[Introduction The old r-wiki optimisation challenge describes a string generation problem which I have bloged about previously both here and here. The Objective To code the most efficient algorithm, using R, to produce a sequence of strings based on a single integer input, e.g.: Solutions One Through Thirteen A variety of different approaches are illustrated on the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tonybreyal.wordpress.com&#38;blog=28974435&#38;post=548&#38;subd=tonybreyal&#38;ref=&#38;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://tonybreyal.wordpress.com/2011/12/08/code-optimization-one-r-problem-thirteen-solutions-now-sixteen/"> Consistently Infrequent » R</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
<p><strong>Introduction</strong></p>
<p>The old <a href="http://rwiki.sciviews.org/doku.php?id=tips:programming:code_optim2" ref="nofollow" target="_blank">r-wiki optimisation challenge</a> describes a string generation problem which I have bloged about previously both <a href="http://tonybreyal.wordpress.com/2011/11/02/code-optimization-one-r-problem-ten-solutions-%E2%80%93-now-eleven-2/" ref="nofollow" target="_blank">here</a> and <a href="http://tonybreyal.wordpress.com/2011/11/07/code-optimization-one-r-problem-eleven-solutions-now-thirteen/" ref="nofollow" target="_blank">here</a>.</p>
<p><strong>The Objective</strong></p>
<p>To code the most efficient algorithm, using R, to produce a sequence of strings based on a single integer input, e.g.:</p>
<p><pre class="brush: r;">
# n = 4
[1] &quot;i001.002&quot; &quot;i001.003&quot; &quot;i001.004&quot; &quot;i002.003&quot; &quot;i002.004&quot; &quot;i003.004&quot;
# n = 5
 [1] &quot;i001.002&quot; &quot;i001.003&quot; &quot;i001.004&quot; &quot;i001.005&quot; &quot;i002.003&quot; &quot;i002.004&quot; &quot;i002.005&quot; &quot;i003.004&quot;
 [9] &quot;i003.005&quot; &quot;i004.005&quot;
# n = 6
 [1] &quot;i001.002&quot; &quot;i001.003&quot; &quot;i001.004&quot; &quot;i001.005&quot; &quot;i001.006&quot; &quot;i002.003&quot; &quot;i002.004&quot; &quot;i002.005&quot;
 [9] &quot;i002.006&quot; &quot;i003.004&quot; &quot;i003.005&quot; &quot;i003.006&quot; &quot;i004.005&quot; &quot;i004.006&quot; &quot;i005.006&quot;
</pre></p>
<p><strong>Solutions One Through Thirteen</strong></p>
<p>A variety of different approaches are illustrated on the <a href="http://rwiki.sciviews.org/doku.php?id=tips:programming:code_optim2#sixteenth_solution" ref="nofollow" target="_blank">r-wiki</a> page which show the performance benefits of things like vectorisation, variable initialisation, linking through to a compiled programming language, reducing a problem to its component parts, etc.</p>
<p><strong>The Fourteenth Solution</strong></p>
<p>The main speed improvement here comes from replacing the function “paste” by “file.path”. This use of “file.path” with parameter fsep=”” only works correctly here because there is never a character vector of length 0 for it to deal with. I only learned about this approach when I happened to see <a href="https://twitter.com/#!/RLangTip/status/142271960867540994" ref="nofollow" target="_blank">this tweet on twitter with hashtag #rstats</a> and reading the<a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/file.path.html" ref="nofollow" target="_blank"> associated help file </a>where it says that it is faster than paste.</p>
<p><pre class="brush: r;">
generateIndex14 &lt;- function&lt;/a&gt;(n) {
  # initialise vectors
  s &lt;- (mode = &quot;character&quot;, length = n)

  # set up n unique strings
  s &lt;- sprintf(&quot;%03d&quot;, seq_len(n))

  # paste strings together
  unlist(lapply(1:(n-1), function(i) file.path(&quot;i&quot;, s[i], &quot;.&quot;, s[(i+1):n], fsep = &quot;&quot;) ), use.names = FALSE)
}
</pre></p>
<p>Timings:</p>
<p><pre class="brush: r;">
               test  elapsed    n replications
 generateIndex14(n) 27.27500 2000           50
 generateIndex13(n) 33.09300 2000           50
 generateIndex12(n) 35.31344 2000           50
 generateIndex11(n) 36.32900 2000           50
</pre></p>
<p><strong>The Fifteenth Solution: Rcpp</strong></p>
<p>This solution comes from Romain Francois and is based on the tenth solution but implemented in C++ using the R package <a href="http://cran.r-project.org/web/packages/Rcpp/index.html" ref="nofollow" target="_blank">Rcpp</a>. See his <a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/10/Code-optimization,-an-Rcpp-solution" ref="nofollow" target="_blank">blog</a> for the implementation. This is the sort of thing I would love to learn to do myself but just need to find the time to re-learn C++, though I doubt that&#8217;ll happen any time soon as I&#8217;m hoping to start my MSc in Statistics next year. This is a great solution though.</p>
<p>Timings:</p>
<p><pre class="brush: r;">
               test  elapsed    n replications
 generateIndex15(n) 23.30100 2000           50
 generateIndex14(n) 27.27500 2000           50
 generateIndex13(n) 33.09300 2000           50
 generateIndex12(n) 35.31344 2000           50
 generateIndex11(n) 36.32900 2000           50
</pre></p>
<p><strong>The Sixteenth Solution</strong></p>
<p>When I was writing up this post I thought up a sixteenth solution (as seems to be the pattern with me on this blog!). This solution gets its speed up by generating the largest set of strings which start i001.xxx first and then replacing the “001” part with “002”, “003”, “004”, etc., for each increment up to and including n-1.</p>
<p><pre class="brush: r;">

generateIndex16 &lt;- function(n) {
  # initialise vectors
  str &lt;- vector(&quot;list&quot;, length = n-1)
  s &lt;- vector(mode = &quot;character&quot;, length = n)

  # set up strings
  s &lt;- sprintf(&quot;%03d&quot;, seq_len(n))
  str[[1]] &lt;- file.path(&quot;i&quot;, s[1], &quot;.&quot;, s[-1], fsep = &quot;&quot;)

  # generate string sequences
  str[2:(n-1)] &lt;- lapply(2:(n-1), function(i) sub(&quot;001&quot;, s[i], str[[1]][i:(n-1)], fixed=TRUE))
  unlist(str)
}

</pre></p>
<p>The above requires matching the “001” part first and then replacing it. However, we know that “001” will ALWAYS be in character positions 2, 3 and 4, and so there may be a way to avoid the matching part altogether (i.e. replace a fixed position substring with another string of equal or larger length) but I could not work out how to do that outside of a regular expression. Sadface.</p>
<p>Timings:</p>
<p><pre class="brush: r;">
               test  elapsed    n replications
 generateIndex16(n) 20.77200 2000           50
 generateIndex15(n) 23.30100 2000           50
 generateIndex14(n) 27.27500 2000           50
 generateIndex13(n) 33.09300 2000           50
 generateIndex12(n) 35.31344 2000           50
 generateIndex11(n) 36.32900 2000           50
</pre></p>
<p><strong>Solutions Comparisons For Different N</strong></p>
<p>I like ggplot2 charts and so ran my computer overnight to generate data for the speed performance of the last several solutions over different N:</p>
<p><img class="aligncenter" title="r wiki sols 11-16" src="http://rwiki.sciviews.org/lib/exe/fetch.php?w=&amp;h=&amp;cache=cache&amp;media=tips:programming:optim_sols_11-16.jpeg" alt="" width="1000" height="800" /></p>
<p><strong>Final Thoughts</strong></p>
<p>I&#8217;m pretty sure that any more speed improvements will come from some or all of the follwing:</p>
<ul>
<li>doing the heavy lifting in a compiled language and interfacing with R</li>
<li>running in parallel (I actually got this to work on linux by replacing <em>lapply</em> with <em>mclapply</em> from the <em>parallel</em> R package but the downside was that one has to use much more memory for larger values of N, plus it&#8217;s only works in <em>serial</em> fashion on Windows</li>
<li>working out an efficient way of replacing a fixed positioned substring with a string of equal or great length</li>
<li>compiling the function into R bytecodes using the <em>compiler</em> package function <em>cmpfun</em></li>
</ul>
<p>It would also be interesting to profile the memory usage of each funciton.</p>
<p>This was a fun challenge &#8211; if you find some spare time why not try your hand at it, you might come up with something even better!  :)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tonybreyal.wordpress.com/548/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tonybreyal.wordpress.com/548/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tonybreyal.wordpress.com/548/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tonybreyal.wordpress.com/548/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tonybreyal.wordpress.com/548/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tonybreyal.wordpress.com/548/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tonybreyal.wordpress.com/548/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tonybreyal.wordpress.com/548/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tonybreyal.wordpress.com&amp;blog=28974435&amp;post=548&amp;subd=tonybreyal&amp;ref=&amp;feed=1" width="1" height="1" />
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://tonybreyal.wordpress.com/2011/12/08/code-optimization-one-r-problem-thirteen-solutions-now-sixteen/"> Consistently Infrequent » R</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/code-optimization-one-r-problem-thirteen-solutions-%e2%80%93-now-sixteen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://0.gravatar.com/avatar/282c9a55aed6ab2fff0c4b8b3304a9f7?s=96&amp;amp;d=identicon&amp;amp;r=PG" length="" type="" />
<enclosure url="http://rwiki.sciviews.org/lib/exe/fetch.php?w=&amp;amp;h=&amp;amp;cache=cache&amp;amp;media=tips:programming:optim_sols_11-16.jpeg" length="" type="" />
		</item>
		<item>
		<title>Installing Rcpp on Windows 7 for R and C++ integration</title>
		<link>http://www.r-bloggers.com/installing-rcpp-on-windows-7-for-r-and-c-integration/</link>
		<comments>http://www.r-bloggers.com/installing-rcpp-on-windows-7-for-r-and-c-integration/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 17:49:12 +0000</pubDate>
		<dc:creator>Tony Breyal</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>
		<category><![CDATA[rstats]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://tonybreyal.wordpress.com/?p=513</guid>
		<description><![CDATA[Introduction Romain Francois presented an Rcpp solution on his blog to an old r-wiki optimisation challenge which I had also presented R solutions for previously on my blog. The Rcpp package provides a method for integrating R and C++. This allows for faster execution of an R project by recoding the slower R parts into C+ and thus providing potential performance enhancements. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tonybreyal.wordpress.com&#38;blog=28974435&#38;post=513&#38;subd=tonybreyal&#38;ref=&#38;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://tonybreyal.wordpress.com/2011/12/07/installing-rcpp-on-windows-7-for-r-and-c-integration/"> Consistently Infrequent » R</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
<p style="text-align:center;"><img class="aligncenter" title="c++" src="http://tonybreyal.files.wordpress.com/2011/12/solarworlds_crescentmoon_1280x1024copy.jpg?w=768&#038;h=614" alt="" width="768" height="614" /></p>
<p><strong>Introduction</strong></p>
<p><a href="http://romainfrancois.blog.free.fr" ref="nofollow" target="_blank">Romain Francois</a> presented an <a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/10/Code-optimization,-an-Rcpp-solution" ref="nofollow" target="_blank">Rcpp solution</a> on his blog to an old <a href="http://rwiki.sciviews.org/doku.php?id=tips:programming:code_optim2" ref="nofollow" target="_blank">r-wiki optimisation challenge</a> which I had also presented R solutions for <a href="https://tonybreyal.wordpress.com/2011/11/07/code-optimization-one-r-problem-eleven-solutions-now-thirteen/" ref="nofollow" target="_blank">previously</a> on my blog.</p>
<p>The <a href="http://cran.r-project.org/web/packages/Rcpp/index.html" ref="nofollow" target="_blank">Rcpp package</a> provides a method for integrating R and C++. This allows for faster execution of an R project by recoding the slower R parts into C+ and thus providing potential performance enhancements.</p>
<p>The two main attractions, for me personally, of the Rcpp package are (a) help me to re-learn C++ because I&#8217;ve not used it in over 10 years and (b) write R packages which interface to existing C++ libraries for the purposes of natural language processing or some other library which does something cool.</p>
<p><strong>The objective</strong></p>
<p>Install Rcpp on Windows 7 Pro x64 to test out Romain&#8217;s Rccp solution to the old r-wiki optimisation challenge</p>
<p><strong>The Problem</strong></p>
<p>I had zero problems installing and running Rcpp code on <a class="zem_slink" title="Ubuntu (operating system)" href="http://www.ubuntu.com/" rel="homepage" ref="nofollow" target="_blank">Ubuntu Linux</a> 11.10 but just could not get the same to work on Windows 7 Pro x64, often getting the following warning and errors:</p>
<blockquote><p>Error in compileCode(f, code, language = language, verbose = verbose) :<br />
Compilation ERROR, function(s)/method(s) not created!</p>
<p>cygwin warning:<br />
MS-DOS style path detected: C:/PROGRA~1/R/R-214~1.0/etc/x64/Makeconf<br />
Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-214~1.0/etc/x64/Makeconf<br />
CYGWIN environment variable option &#8220;nodosfilewarning&#8221; turns off this warning.<br />
Consult the user&#8217;s guide for more details about POSIX paths:</p>
<p>http://cygwin.com/cygwin-ug-net/using.html#using-pathnames</p>
<p>x86_64-w64-mingw32-g++.exe: C:/Program: No such file or directory<br />
x86_64-w64-mingw32-g++.exe: Files/R/R-2.14.0/library/Rcpp/lib/x64/libRcpp.a: No such file or directory</p></blockquote>
<p><strong>The Solution</strong></p>
<p>It turns out that there&#8217;s two issues here:</p>
<p>(1) the cygwin warning which we can ignore as it has nothing to do with Rcpp and is caused by using the 64bit version of MinGW from Rtools when adding it to our PATH variable (easily solved by choosing the 32bit version instead, shown below in step 5)</p>
<p>(2) the actual Rcpp issue itself which happened because I had originally installed R on a path with a space in it. In fact (and this is something I didn&#8217;t previously know) it&#8217;s officially recommended that one does not install R on Windows with a path containing a space (see <a href="http://cran.r-project.org/bin/windows/rw-FAQ.html#How-do-I-install-R-for-Windows_003f" ref="nofollow" target="_blank">rw-FAQ 2.2</a>).</p>
<p>So armed with this knowledge, here is how I got Rcpp working from beginning to end (a lot of the sub-steps below are blindingly obvious but I included them anyway for the sake of completeness and OCD satisfaction):</p>
<ol>
<li>Install R on a path that does not contain a space.<br />
(i)  download R from: <a href="http://cran.r-project.org/bin/windows/base/" ref="nofollow" target="_blank">http://cran.r-project.org/bin/windows/base/</a><br />
(ii) double click the downloaded .exe file<br />
(iii) press Run when the security warning appears<br />
(iv) press Yes when the User Account Control message box appears<br />
(v) Choose your language (I choose English)<br />
(vi) when prompted for where to install R, choose a location without a space. I choose &#8220;C:\R\R-2.14.0&#8243;<br />
(vii) click Next on all following screens to finish installation (I left the defaults unaltered).<br />
.</li>
<li>Install Rtools for components necessary to buld R (I think this has a C++ compiler included because I&#8217;m pretty sure my system didn&#8217;t already have another previous C++ compiler installed)<br />
(i) Go to this webpage: <a href="http://www.murdoch-sutherland.com/Rtools/" ref="nofollow" target="_blank">http://www.murdoch-sutherland.com/Rtools/</a><br />
(ii) In the downloads section, choose a compatible version for your version of R. I&#8217;m using R-2.14.0 and thus chose <a href="http://www.murdoch-sutherland.com/Rtools/Rtools214.exe" ref="nofollow" target="_blank">Rtools214.exe<br />
</a>(iii) press Run when the security warning appears<br />
(iv) press Yes when the User Account Control message box appears<br />
(v) choose your language (I choose English)<br />
(vi) choose where to install (I chose &#8220;C:\R\Rtools&#8221;)<br />
(vii) click Next on all following screens to finish installation (I left the defaults unaltered).<br />
.</li>
<li>Download <a href="https://code.google.com/p/batchfiles/" ref="nofollow" target="_blank">batchfiles </a>to always point to the latest version of R on your system when running R from the command line (I have found that this saves a lot of time in the long run)<br />
(i) go to: <a href="http://cran.r-project.org/contrib/extra/batchfiles/" ref="nofollow" target="_blank">http://cran.r-project.org/contrib/extra/batchfiles/</a><br />
(ii) choose the latest version (I chose batchfiles_0.6-6.zip)<br />
(iii) when downloaded, right click on file and click Extract All.<br />
(iv) click Browse and choose where you want to put the extracted folder (I chose &#8220;C:\R\&#8221;)<br />
.</li>
<li>Download Redmond Path Utility to alter PATH variables in a very user friendly fashion:<br />
(i) go to: <a href="http://download.cnet.com/Redmond-Path/3000-2094_4-10811594.html" ref="nofollow" target="_blank">http://download.cnet.com/Redmond-Path/3000-2094_4-10811594.html</a><br />
(ii) click on &#8220;Download Now CNET Secure Download&#8221;<br />
(iii) double click the downloaded .exe file<br />
(iv) press Run when the security warning appears<br />
(iv) press Yes when the User Account Control message box appears<br />
(v) press Next<br />
(vi) press Decline (unless you want the annoying advertising extra).<br />
(vii) click open<br />
(viii) An explorer window will open with RedmondPath.zip<br />
(ix) click Extract All.<br />
(x) click Browse and choose where you want to put the extracted folder (I chose &#8220;C:\R\&#8221;)<br />
.</li>
<li>Edit PATH variable to allow system wide access to the current version of R on the computer and components of Rtools<br />
(i) double click the Redmond Path Utility from step 4 above (mine is in: &#8220;C:\R\RedmondPath\Redmond Path.exe&#8221;)<br />
(ii) click Yes when the User Account Control message box appears<br />
(iii) click the green &#8220;+&#8221; icon in the top left corner so we can add elements to the PATH variable<br />
(iv)  In the window which pops open, navigate to &#8220;C:\R\batchfiles_0.6-6&#8243; and click OK<br />
(v) click the green &#8220;+&#8221; icon in the top left corner so we can add elements to the PATH variable<br />
(vi)  In the window which pops open, navigate to &#8220;C:\R\Rtools\bin&#8221; and click OK<br />
(vii) click the green &#8220;+&#8221; icon in the top left corner so we can add elements to the PATH variable<br />
(viii)  In the window which pops open, navigate to &#8220;C:\R\Rtools\MinGW\bin&#8221; and click OK<br />
.</li>
<li>Restart your computer (this solved an issue where the edits to the PATH variable above had not taken immediate affect)<br />
.</li>
<li>Open R and run the following code<br />
<pre class="brush: r;">
# install packages
install.packages(c(&quot;Rcpp&quot;, &quot;rbenchmark&quot;, &quot;inline&quot;, &quot;Runit&quot;))

# load main two packages
library(Rcpp)
library(inline)

# do something with Rcpp to quickly check that it works
body &lt;- '
NumericVector xx(x);
return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));'

add &lt;- cxxfunction(signature(x = &quot;numeric&quot;), body, plugin = &quot;Rcpp&quot;)

x &lt;- 1
y &lt;- 2
res &lt;- add(c(x, y))
res
#[1] 3

</pre></li>
</ol>
<p>And there you have it, Rcpp working on Windows! I was able to run Romain&#8217;s Rcpp code (<a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/10/Code-optimization,-an-Rcpp-solution" ref="nofollow" target="_blank">see his blog for the code</a>) without any problems. Awesome stuff.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/tonybreyal.wordpress.com/513/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/tonybreyal.wordpress.com/513/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/tonybreyal.wordpress.com/513/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/tonybreyal.wordpress.com/513/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/tonybreyal.wordpress.com/513/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/tonybreyal.wordpress.com/513/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/tonybreyal.wordpress.com/513/" ref="nofollow" target="_blank"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/tonybreyal.wordpress.com/513/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=tonybreyal.wordpress.com&amp;blog=28974435&amp;post=513&amp;subd=tonybreyal&amp;ref=&amp;feed=1" width="1" height="1" />
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://tonybreyal.wordpress.com/2011/12/07/installing-rcpp-on-windows-7-for-r-and-c-integration/"> Consistently Infrequent » R</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/installing-rcpp-on-windows-7-for-r-and-c-integration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://0.gravatar.com/avatar/282c9a55aed6ab2fff0c4b8b3304a9f7?s=96&amp;amp;d=identicon&amp;amp;r=PG" length="" type="" />
<enclosure url="http://tonybreyal.files.wordpress.com/2011/12/solarworlds_crescentmoon_1280x1024copy.jpg?w=300" length="" type="" />
		</item>
		<item>
		<title>int64: 64 bit integer vectors for R</title>
		<link>http://www.r-bloggers.com/int64-64-bit-integer-vectors-for-r/</link>
		<comments>http://www.r-bloggers.com/int64-64-bit-integer-vectors-for-r/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 09:50:00 +0000</pubDate>
		<dc:creator>romain francois</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[cplusplus]]></category>
		<category><![CDATA[cran]]></category>
		<category><![CDATA[int64]]></category>
		<category><![CDATA[package]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[R package]]></category>
		<category><![CDATA[Rcpp]]></category>
		<category><![CDATA[RProtoBuf]]></category>

		<guid isPermaLink="false">http://www.r-bloggers.com/?guid=bbaca728180bc01e7a76facb54353ff3</guid>
		<description><![CDATA[    

The Google Open Source Programs Office sponsored me to create the new int64 package that has been released to CRAN a few days ago. The package has been mentionned in an article in the open source blog from Google. 
 
The package defines classes i...]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/26/int64:-64-bit-integer-vectors-for-R"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
    <a href="http://cran.r-project.org/web/packages/int64/index.html" ref="nofollow" target="_blank"><img src="http://romainfrancois.blog.free.fr/public/packages/int64/google-64.png" alt="google-64.png" style="margin: 0 auto; display: block;" title="google-64.png, nov. 2011" /></a>

<p>The <a href="http://code.google.com/opensource/" ref="nofollow" target="_blank">Google Open Source Programs Office</a> sponsored me to create the new <a href="http://cran.r-project.org/web/packages/int64/index.html" ref="nofollow" target="_blank">int64</a> package that has been released to CRAN a few days ago. The package has been mentionned in an article in the <a href="http://google-opensource.blogspot.com/2011/11/bringing-64-bit-data-to-r.html" ref="nofollow" target="_blank">open source blog from Google</a>. </p>
 
<p>The package defines classes int64 and uint64 that represent signed and unsigned 64 bit integer vectors. The package also allows conversion of several types (integer, numeric, character, logical) to 64 bit integer vectors, arithmetic operations as well as other standard group generic functions, and reading 64 bit integer vectors as a data.frame column using int64 or uint64 as the colClasses argument. </p>

<p>The package has a <a href="http://cran.r-project.org/web/packages/int64/vignettes/int64.pdf" ref="nofollow" target="_blank">vignette</a> that details its features, several examples are given in the usual help files. Once again, I've used <a href="http://cran.r-project.org/web/packages/RUnit/index.html" ref="nofollow" target="_blank">RUnit</a> for quality insurance about the package code</p>

<p><a href="http://cran.r-project.org/web/packages/int64/index.html" ref="nofollow" target="_blank">int64</a> has been developped so that 64 bit integer vectors are represented using only R data structures, i.e data is not represented as external pointers to some C++ object. Instead, each 64 bit integer is represented as a couple of regular 32 bit integers, each of them carrying half the bits of the underlying 64 bit integer. This was a choice by design so that 64 bit integer vectors can be serialized and used as data frame columns. </p>

<p>The package contains C++ headers that third party packages can used (via LinkingTo: int64) to use the C++ internals. This allows creation and manipulation of the objects in C++. The internals will be documented in another vignette for package developpers who wish to use the internals. For the moment, the main entry point is the C++ template class LongVector. </p>

<p>I'm particularly proud that Google trusted me to sponsor the development of <a href="http://cran.r-project.org/web/packages/int64/index.html" ref="nofollow" target="_blank">int64</a>. The next versions of packages <a href="http://dirk.eddelbuettel.com/code/rcpp.html" ref="nofollow" target="_blank">Rcpp</a> and <a href="http://dirk.eddelbuettel.com/code/rprotobuf.html" ref="nofollow" target="_blank">RProtoBuf</a> take advantage of the facilities of <a href="http://cran.r-project.org/web/packages/int64/index.html" ref="nofollow" target="_blank">int64</a>, e.g. Rcpp gains wrapping of C++ containers of 64 bit integers as R objects of classes int64 and uint64 and RProtoBuf improves handling of 64 bit integers in protobuf messages. More on this later</p>
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/26/int64:-64-bit-integer-vectors-for-R"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/int64-64-bit-integer-vectors-for-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bringing 64-bit data to R</title>
		<link>http://www.r-bloggers.com/bringing-64-bit-data-to-r/</link>
		<comments>http://www.r-bloggers.com/bringing-64-bit-data-to-r/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 18:00:00 +0000</pubDate>
		<dc:creator>Stephanie Taylor</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[dremel]]></category>
		<category><![CDATA[protocol buffers]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>
		<category><![CDATA[RProtoBuf]]></category>

		<guid isPermaLink="false">http://www.r-bloggers.com/?guid=c7877d820969b8299af7e6e768992229</guid>
		<description><![CDATA[The R programming language has become one of the standard tools for statistical data analysis and visualization, and is widely used by Google and many others. The language includes extensive support for working with vectors of integers, numerics (doubl...]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://google-opensource.blogspot.com/2011/11/bringing-64-bit-data-to-r.html"> Google Open Source Blog</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
<a href="http://4.bp.blogspot.com/-PeQIcQv2DaY/Ts3STVSXpzI/AAAAAAAAAZs/1svRJBHgjrQ/s1600/index.jpeg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" ref="nofollow" target="_blank"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 100px; height: 76px;" src="http://4.bp.blogspot.com/-PeQIcQv2DaY/Ts3STVSXpzI/AAAAAAAAAZs/1svRJBHgjrQ/s400/index.jpeg" border="0" alt="" id="BLOGGER_PHOTO_ID_5678425934757603122" /></a><br />The <a href="http://www.r-project.org/" ref="nofollow" target="_blank">R programming language</a> has become one of the standard tools for statistical data analysis and visualization, and is widely <a href="http://google-opensource.blogspot.com/search/label/r" ref="nofollow" target="_blank">used by Google</a> and many others. The language includes extensive support for working with vectors of integers, numerics (doubles), and many other types, but has lacked support for 64-bit integers.  <a href="http://romainfrancois.blog.free.fr/" ref="nofollow" target="_blank">Romain Francois</a> has recently uploaded the <a href="http://cran.r-project.org/web/packages/int64/index.html" ref="nofollow" target="_blank">int64</a> package to <a href="http://cran.r-project.org/" ref="nofollow" target="_blank">CRAN</a> as well as updated versions of the <a href="http://google-opensource.blogspot.com/2010/10/integrating-r-with-c-rcpp-rinside-and.html" ref="nofollow" target="_blank">Rcpp</a> and <a href="http://cran.r-project.org/web/packages/RProtoBuf/index.html" ref="nofollow" target="_blank">RProtobuf</a> packages to make use of this package. Inside Google, this is important when interacting with other engineering systems such as <a href="http://research.google.com/pubs/pub36632.html" ref="nofollow" target="_blank">Dremel</a> and <a href="http://code.google.com/p/protobuf/" ref="nofollow" target="_blank">Protocol Buffers</a>, where our engineers and quantitative analysts often need to read in 64-bit quantities from a datastore and perform statistical analysis inside R.<br /><br />Romain has taken the approach of storing int64 vectors as S4 objects with a pair of R’s default 32-bit integers to store the high and low-order bits. Almost all of the standard arithmetic operations built into the R language have been extended to work with this new class. The design is such that the necessary bit-artihmetic is done behind the scenes in high-performance C++ code, but the higher-level R functions work transparently. This means, for example, that you can:<div><br />• Perform arithmetic operations between 64-bit operands or between int64 objects and integer or numeric types in R.<br />• Read and write CSV files including 64-bit values by specifying int64 as a colClasses argument to read.csv and write.csv (with int64 version 1.1).<br />• Load and save 64-bit types with the built-in serialization methods of R.<br />• Compute summary statistics of int64 vectors, such as max, min, range, sum, and the other standard R functions in the Summary <a href="http://stat.ethz.ch/R-manual/R-patched/library/methods/html/S4groupGeneric.html" ref="nofollow" target="_blank">Group Generic</a>.<br /><br />For even higher levels of precision, there is also the venerable and powerful <a href="http://gmplib.org/" ref="nofollow" target="_blank">GNU Multiple Precision Arithmetic Library</a> and the <a href="http://cran.r-project.org/web/packages/gmp/index.html" ref="nofollow" target="_blank">R GMP package</a> on CRAN, although Romain’s new int64 package is a better fit for the 64-bit case.<br /><br />We’ve had to work around the lack of 64-bit integers in R for several years at Google. And after several discussions with Romain, we were very happy to be able to fund his development of this package to solve the problem not just for us, but for the broader open-source community as well. Enjoy!<br /><br /><i>By Murray Stokely, Software Engineer, Infrastructure Quantitative Team</i><br /></div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8698702854482141883-3309361090026544882?l=google-opensource.blogspot.com' alt='' /></div>
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://google-opensource.blogspot.com/2011/11/bringing-64-bit-data-to-r.html"> Google Open Source Blog</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/bringing-64-bit-data-to-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="" length="" type="" />
		</item>
		<item>
		<title>Code optimization, an Rcpp solution</title>
		<link>http://www.r-bloggers.com/code-optimization-an-rcpp-solution/</link>
		<comments>http://www.r-bloggers.com/code-optimization-an-rcpp-solution/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 17:32:00 +0000</pubDate>
		<dc:creator>romain francois</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>

		<guid isPermaLink="false">http://www.r-bloggers.com/?guid=a957082157b4a5ce5f0c4e9bab46c53a</guid>
		<description><![CDATA[    Tony Breyal woke up an old code optimization problem in this blog post, so I figured it was time for an Rcpp based solution

This solutions moves down Henrik Bengtsson's idea (which was at the basis of attempt 10) down to C++. The idea was to call ...]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/10/Code-optimization,-an-Rcpp-solution"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
    <p><a href="http://tonybreyal.wordpress.com/" ref="nofollow" target="_blank">Tony Breyal</a> woke up an old code optimization problem in this <a href="http://tonybreyal.wordpress.com/2011/11/02/code-optimization-one-r-problem-ten-solutions-%E2%80%93-now-eleven-2/" ref="nofollow" target="_blank">blog post</a>, so I figured it was time for an <a href="http://dirk.eddelbuettel.com/code/rcpp.html" ref="nofollow" target="_blank">Rcpp</a> based solution</p>

<p>This solutions moves down Henrik Bengtsson's idea (which was at the basis of attempt 10) down to C++. The idea was to call sprintf less than the other solutions to generate the strings "001", "002", "003", ...</p>

<iframe src="http://romainfrancois.blog.free.fr/public/packages/Rcpp/generateIndex14.html" height=400 width=500></iframe>

<p>We can benchmark this version using the rbenchmark package:</p>

<pre>
> library(rbenchmark)
> n <- 2000
> benchmark(
+     generateIndex10(n), 
+     generateIndex11(n),
+     generateIndex12(n), 
+     generateIndex13(n),
+     generateIndex14(n),
+     columns = 
+        c("test", "replications", "elapsed", "relative"),
+     order = "relative",
+     replications = 20
+ )
                test replications elapsed relative
5 generateIndex14(n)           20  21.015 1.000000
3 generateIndex12(n)           20  22.034 1.048489
4 generateIndex13(n)           20  23.436 1.115203
2 generateIndex11(n)           20  23.829 1.133904
1 generateIndex10(n)           20  30.580 1.455151
>    

</pre>
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/11/10/Code-optimization,-an-Rcpp-solution"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/code-optimization-an-rcpp-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rcpp reverse dependency graph</title>
		<link>http://www.r-bloggers.com/rcpp-reverse-dependency-graph/</link>
		<comments>http://www.r-bloggers.com/rcpp-reverse-dependency-graph/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 09:05:00 +0000</pubDate>
		<dc:creator>romain francois</dc:creator>
				<category><![CDATA[R bloggers]]></category>
		<category><![CDATA[cran]]></category>
		<category><![CDATA[graphviz]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Rcpp]]></category>

		<guid isPermaLink="false">http://www.r-bloggers.com/?guid=56d3d45526f27dc35f00a956a0812ab9</guid>
		<description><![CDATA[    I played around with reverse dependencies of Rcpp. At the moment, 44 packages depend on Rcpp and the number goes up to 53 when counting recusive reverse dependencies.

I've used graphviz for the representation of the directed graph



Here is the c...]]></description>
			<content:encoded><![CDATA[<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 12px;">
(This article was first published on  <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/10/30/Rcpp-reverse-dependency-graph"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>, and kindly contributed to <a href="http://www.r-bloggers.com/">R-bloggers)</a>      
</div></p>
    <p>I played around with reverse dependencies of <a href="http://dirk.eddelbuettel.com/code/rcpp.html" ref="nofollow" target="_blank">Rcpp</a>. At the moment, 44 packages depend on <a href="http://dirk.eddelbuettel.com/code/rcpp.html" ref="nofollow" target="_blank">Rcpp</a> and the number goes up to 53 when counting recusive reverse dependencies.</p>

<p>I've used <a href="http://www.graphviz.org/" ref="nofollow" target="_blank">graphviz</a> for the representation of the directed graph</p>

<img src="http://romainfrancois.blog.free.fr/public/packages/Rcpp/dep.png" alt="dep.png" style="margin: 0 auto; display: block;" title="dep.png, oct. 2011" />

<p>Here is the code I've used to generate the dot file:</p>

<iframe src="http://romainfrancois.blog.free.fr/public/packages/Rcpp/depends.html" width=550 height=400></iframe>
<p class="syndicated-attribution"><div style="border: 1px solid; background: none repeat scroll 0 0 #EDEDED; margin: 1px; font-size: 13px;">
<div style="text-align: center;">To <strong>leave a comment</strong> for the author, please follow the link and comment on his blog: <strong><a href="http://romainfrancois.blog.free.fr/index.php?post/2011/10/30/Rcpp-reverse-dependency-graph"> Romain Francois, Professional R Enthusiast - Tag - R</a></strong>.</div>
<hr />
<a href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: visualization (<a title="ggplot and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" href="http://www.r-bloggers.com/?s=time+series">time series</a>,<a title="ecdf" href="http://www.r-bloggers.com/?s=ecdf">ecdf</a>, <a title="finance trading" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
</div></p>]]></content:encoded>
			<wfw:commentRss>http://www.r-bloggers.com/rcpp-reverse-dependency-graph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

