File: isplitIndicesExample.R

package info (click to toggle)
r-cran-itertools 0.1-3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 316 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 970 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# This example takes the sum of the square roots of the numbers from
# one to a billion.  Note that this can't be done with the expression:
#
#   sum(sqrt(1:1000000000))
#
# since that attempts to allocate a 3.7 Gb vector, which generates
# an error in versions of R including 2.10.1.

library(itertools)
library(foreach)

# Size of input vector
n <- 1000000000

# The best value for chunkSize depends on how much memory you have.
# Generally, if chunkSize is too small, the code becomes inefficient.
# If chunkSize is too big, you either run out of memory or suffer from
# virtual memory thrashing.  I think that a value of 5 million should
# avoid memory thrashing on most modern computers without being too
# inefficient, but if you have 2 Gigabytes of memory or more, you
# might want to increase this value.
chunkSize <- 5000000

r <- foreach(x=isplitIndices(n, chunkSize=chunkSize), .combine='sum') %do% {
  sqrt(x)
}

cat(sprintf('sum(sqrt(1:%d)) = %e\n', n, r))