File: performance.R

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 831 bytes parent folder | download | duplicates (9)
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
29
30
31
32
33
34
35
36
37
38

require( inline )
require( Rcpp )

expressions <- list( 
    times = "x * y" , 
    plus = "x + y"
#    , 
#    minus = "x - y", 
#    divides = "x / y", 
#    exp_ = "exp( x )"
)

signatures <- lapply( expressions, function(.) signature( x_ = "numeric", y_ = "numeric", n_ = "integer" ) )
bodies <- lapply( expressions, function(.){
   sprintf( '
    int n = as<int>( n_ ) ;
    NumericVector x(x_), y(y_), z(x.size()) ;
    for( int i=0; i<n; i++){
        z = %s ;
    }
    return z ;
', . ) 
} )

fx <- cxxfunction( signatures, bodies, plugin = "Rcpp" )

set.seed( 43231 )
x <- runif( 100000, min = 1, max = 100)
y <- runif( 100000, min = 1, max = 100)
# resolving the dyn lib once
invisible( lapply( fx, function(f){ f( x, y, 1L) } ) )

# t( sapply( fx, function(f){
#     system.time( f( x, y, 10000 ) )
# } ) )[, 1:3]
#