forked from RcppCore/Rcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperformance.R
More file actions
38 lines (31 loc) · 831 Bytes
/
Copy pathperformance.R
File metadata and controls
38 lines (31 loc) · 831 Bytes
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
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]
#