Rcpp Version 1.0.9
RcppInlineExample.r
Go to the documentation of this file.
1 #!/usr/bin/env r
2 
3 suppressMessages(library(Rcpp))
4 
5 ## NOTE: This is the old way to compile Rcpp code inline.
6 ## The code here has left as a historical artifact and tribute to the old way.
7 ## Please use the code under the "new" inline compilation section.
8 
9 suppressMessages(library(inline))
10 
11 foo <- '
12  IntegerVector vec(10000); // vec parameter viewed as vector of ints.
13  int i = 0;
14  for (int a = 0; a < 9; a++)
15  for (int b = 0; b < 9; b++)
16  for (int c = 0; c < 9; c++)
17  for (int d = 0; d < 9; d++)
18  vec(i++) = a*b - c*d;
19 
20  return vec;
21 '
22 
23 funx_old <- cxxfunction(signature(), foo, plugin = "Rcpp" )
24 
25 ## NOTE: Within this section, the new way to compile Rcpp code inline has been
26 ## written. Please use the code next as a template for your own project.
27 
28 cppFunction('IntegerVector funx(){
29  IntegerVector vec(10000); // vec parameter viewed as vector of ints.
30  int i = 0;
31  for (int a = 0; a < 9; a++)
32  for (int b = 0; b < 9; b++)
33  for (int c = 0; c < 9; c++)
34  for (int d = 0; d < 9; d++)
35  vec(i++) = a*b - c*d;
36 
37  return vec;
38 }')
39 
40 dd.inline.rcpp <- function() {
41  res <- funx()
42  tabulate(res)
43 }
44 
45 print(mean(replicate(100,system.time(dd.inline.rcpp())["elapsed"]),trim=0.05))
46 
47