Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is foreach() %do% sometimes slower than for?
    primarykey
    data
    text
    <p>I'm playing around with parallellization in R for the first time. As a first toy example, I tried</p> <pre><code>library(doMC) registerDoMC() B&lt;-10000 myFunc&lt;-function() { for(i in 1:B) sqrt(i) } myFunc2&lt;-function() { foreach(i = 1:B) %do% sqrt(i) } myParFunc&lt;-function() { foreach(i = 1:B) %dopar% sqrt(i) } </code></pre> <p>I know that <code>sqrt()</code> executes too fast for parallellization to matter, but what I didn't expect was that <code>foreach() %do%</code> would be slower than <code>for()</code>:</p> <pre><code>&gt; system.time(myFunc()) user system elapsed 0.004 0.000 0.005 &gt; system.time(myFunc2()) user system elapsed 6.756 0.000 6.759 &gt; system.time(myParFunc()) user system elapsed 6.140 0.524 6.096 </code></pre> <p>In most examples that I've seen, <code>foreach() %dopar%</code> is compared to <code>foreach() %do%</code> rather than <code>for()</code>. Since <code>foreach() %do%</code> was much slower than <code>for()</code> in my toy example, I'm now a bit confused. Somehow, I thought that these were equivalent ways of constructing for-loops. What is the difference? Are they ever equivalent? Is <code>foreach() %do%</code> always slower?</p> <p>UPDATE: Following @Peter Fines answer, I update <code>myFunc</code> as follows:</p> <pre><code> a&lt;-rep(NA,B) myFunc&lt;-function() { for(i in 1:B) a[i]&lt;-sqrt(i) } </code></pre> <p>This makes <code>for()</code> a bit slower, but not much:</p> <pre><code>&gt; system.time(myFunc()) user system elapsed 0.036 0.000 0.035 &gt; system.time(myFunc2()) user system elapsed 6.380 0.000 6.385 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload