forked from dbdahl/rscala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks-benchmarked.Rout-FALSE-FALSE-2.11
134 lines (134 loc) · 3.15 KB
/
callbacks-benchmarked.Rout-FALSE-FALSE-2.11
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
> source("common.R",print.eval=TRUE)
# R version 3.4.0 (2017-04-21) # becker # FALSE # FALSE
> set.seed(924234)
>
>
> sleep.time <- 0
> f <- function(x) { Sys.sleep(sleep.time); mean(x) }
> g <- function(x) { Sys.sleep(sleep.time); sd(x) }
>
>
>
> # Native R code implementation
> doit0 <- function(x) {
+ y <- 2*x
+ c(f(y),g(y))
+ }
>
> doit0(rnorm(10))
[1] 0.1013462 1.4964630
>
>
>
> # Single callback in interpreted code.
> doit1 <- function(x) {
+ s$x <- x
+ s %@% 'R.set("y",x.map(2*_))'
+ c(s %~% 'R.evalD0("f(y)")',
+ s %~% 'R.evalD0("g(y)")')
+ }
>
> doit1(rnorm(10))
[1] 0.6360252 2.5355099
>
>
>
> # Multiple callbacks in interpreted code.
> doit2 <- function(x) {
+ s$x <- x
+ s %~% '
+ R.set("y",x.map(2*_))
+ Array(R.evalD0("f(y)"),
+ R.evalD0("g(y)"))
+ '
+ }
>
> doit2(rnorm(10))
[1] -0.163821 1.921544
>
>
>
>
> # Multiple callbacks in compiled code.
> doit3 <- function(x=numeric()) s %!% '
+ R.set("y",x.map(2*_))
+ Array(R.evalD0("f(y)"),
+ R.evalD0("g(y)"))
+ '
>
> doit3(rnorm(10))
[1] 0.1105979 2.3994832
>
>
>
> # Multiple callbacks in compiled code with optimization.
> doit4 <- scalaOptimize(doit3)
>
> doit4(rnorm(10))
[1] -0.05390445 2.16063425
>
>
>
> # Benchmarks
>
> library(microbenchmark)
>
> sleep.time <- 0
> microbenchmark(
+ doit0(rnorm(10)),
+ doit1(rnorm(10)),
+ doit2(rnorm(10)),
+ doit3(rnorm(10)),
+ doit4(rnorm(10)),
+ times=10
+ )
Unit: microseconds
expr min lq mean median uq
doit0(rnorm(10)) 73.857 122.093 641.7948 173.050 202.116
doit1(rnorm(10)) 457846.403 563658.686 879044.8858 843267.704 977912.344
doit2(rnorm(10)) 298712.805 322875.666 578795.8796 420842.306 801455.228
doit3(rnorm(10)) 4145.811 4252.578 8544.6984 5324.298 12097.348
doit4(rnorm(10)) 1491.931 2214.237 4502.7388 2382.717 3655.717
max neval
5061.045 10
1738631.108 10
1346182.418 10
18745.899 10
14716.480 10
> microbenchmark(
+ doit0(rnorm(10)),
+ #doit1(rnorm(10)),
+ #doit2(rnorm(10)),
+ doit3(rnorm(10)),
+ doit4(rnorm(10)),
+ times=1000
+ )
Unit: microseconds
expr min lq mean median uq max
doit0(rnorm(10)) 31.267 60.7825 78.31258 74.013 97.0725 262.227
doit3(rnorm(10)) 2001.965 2450.3750 4000.14615 3619.785 4047.2155 104315.513
doit4(rnorm(10)) 1001.292 1229.1835 2062.39697 1907.675 2188.8840 43975.221
neval
1000
1000
1000
>
>
> sleep.time <- 0.1
> microbenchmark(
+ doit0(rnorm(10)),
+ doit1(rnorm(10)),
+ doit2(rnorm(10)),
+ doit3(rnorm(10)),
+ doit4(rnorm(10)),
+ times=5
+ )
Unit: milliseconds
expr min lq mean median uq max neval
doit0(rnorm(10)) 200.4799 200.5464 201.8634 200.5696 200.6284 207.0929 5
doit1(rnorm(10)) 867.7928 963.0224 970.5733 979.3134 1014.8281 1027.9100 5
doit2(rnorm(10)) 601.4739 623.4168 683.3671 626.4302 760.4463 805.0679 5
doit3(rnorm(10)) 203.6271 204.6667 208.3895 204.7304 214.4174 214.5061 5
doit4(rnorm(10)) 202.3432 203.2041 205.4910 203.6287 203.7261 214.5528 5
>
>