File: RcppInlineExample.r

package info (click to toggle)
rcpp 0.11.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,948 kB
  • ctags: 16,427
  • sloc: ansic: 42,692; cpp: 34,078; makefile: 32; sh: 21
file content (27 lines) | stat: -rwxr-xr-x 584 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/r

suppressMessages(library(Rcpp))
suppressMessages(library(inline))

foo <- '
    IntegerVector vec(10000);     // vec parameter viewed as vector of ints.
    int i = 0;
    for (int a = 0; a < 9; a++)
      for (int b = 0; b < 9; b++)
        for (int c = 0; c < 9; c++)
          for (int d = 0; d < 9; d++)
            vec(i++) = a*b - c*d;

    return vec;
'

funx <- cxxfunction(signature(), foo, plugin = "Rcpp" )

dd.inline.rcpp <- function() {
    res <- funx()
    tabulate(res)
}

print(mean(replicate(100,system.time(dd.inline.rcpp())["elapsed"]),trim=0.05))