Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>for</code> will run <code>sqrt</code> B times, presumably discarding the answer each time. <code>foreach</code>, however, returns a list containing the result of each execution of the loop body. This would contribute considerable extra overhead, regardless of whether it's running in parallel or sequential mode (<code>%dopar%</code> or <code>%do%</code>).</p> <p>I based my answer by running the following code, which appears to be confirmed by the <a href="http://cran.r-project.org/web/packages/foreach/vignettes/nested.pdf">foreach vignette</a>, which states "foreach differs from a for loop in that its return is a list of values, whereas a for loop has no value and uses side effects to convey its result."</p> <pre><code>&gt; print(for(i in 1:10) sqrt(i)) NULL &gt; print(foreach(i = 1:10) %do% sqrt(i)) [[1]] [1] 1 [[2]] [1] 1.414214 [[3]] ... etc </code></pre> <p>UPDATE: I see from your updated question that the above answer isn't nearly sufficient to account for the performance difference. So I looked at the <a href="http://cran.r-project.org/web/packages/foreach/index.html">source code</a> for <code>foreach</code> and can see that there is a LOT going on! I haven't tried to understand exactly how it works, however <code>do.R</code> and <code>foreach.R</code> show that even when <code>%do%</code> is run, large parts of the <code>foreach</code> configuration is still run, which would make sense if perhaps the <code>%do%</code> option is largely provided to allow you to test <code>foreach</code> code without having to have a parallel backend configured and loaded. It also needs to support the more advanced nesting and iteration facilities that <code>foreach</code> provides.</p> <p>There are references in the code to results caching, error checking, debugging and the creation of local environment variables for the arguments of each iteration (see the function <code>doSEQ</code> in <code>do.R</code> for example). I'd imagine this is what creates the difference that you've observed. Of course, if you were running much more complicated code inside your loop (that would actually benefit from a parallelisation framework like <code>foreach</code>), this overhead would become irrelevant compared with the benefits it provides.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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