Install packages first
install.packages("warbleR")
install.packages("microbenchmark")
install.packages("compiler")
install.packages("ggplot2")
FOR LOOPS
system.time(a<-rnorm(100000))
## user system elapsed
## 0.011 0.000 0.012
system.time(a<-rnorm(1000000))
## user system elapsed
## 0.121 0.032 0.153
system.time(a<-rnorm(10000000))
## user system elapsed
## 0.986 0.112 1.103
a<-rnorm(10000)
head(a,20)
## [1] -0.305881316 -0.919043741 -0.023916784 0.533555555 -0.211820199
## [6] 1.024610923 1.572647125 0.395739499 0.008910267 -0.221611290
## [11] -0.404365487 -0.509418571 -1.295032464 0.834658238 0.491249070
## [16] -0.006450636 -1.239992144 -2.520380447 1.477662705 1.346082102
b<-NULL
system.time(for(i in 1:length(a))
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
b<-append(b, c)
})
## user system elapsed
## 0.326 0.032 0.363
b<-NULL
system.time(for(i in 1:length(a))
{if(a[i] > 0) c <- "pos" else c <- "neg"
b<-append(b, c)
}
)
## user system elapsed
## 0.311 0.019 0.333
b<-vector(length = length(a))
system.time(for(i in 1:length(a))
{if(a[i] > 0) c <- "pos" else c <- "neg"
b[i]<-c
}
)
## user system elapsed
## 0.018 0.001 0.019
APPLY LOOPS
a<-rnorm(10000)
b<-NULL
system.time(for(i in 1:length(a))
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
b<-append(b, c)
})
## user system elapsed
## 0.338 0.003 0.344
system.time(b<-unlist(lapply(1:length(a), function(i)
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
return(c)
}))
)
## user system elapsed
## 0.018 0.000 0.018
system.time(b<-sapply(1:length(a), function(i)
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
return(c)
}, USE.NAMES = F)
)
## user system elapsed
## 0.020 0.000 0.019
b<-NULL
system.time(for(i in 1:length(a))
{if(a[i] > 0) c <- "pos" else c <- "neg"
b<-append(b, c)
}
)
## user system elapsed
## 0.352 0.000 0.354
system.time(b<-sapply(1:length(a), function(i)
{if(a[i] > 0) c <- "pos" else c <- "neg"
return(c)
}, USE.NAMES = F)
)
## user system elapsed
## 0.014 0.000 0.014
FUNCTIONS
a<-rnorm(10000)
sign1<-function(a)
{b<-NULL
system.time(for(i in 1:length(a))
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
b<-append(b, c)
})
return(b)}
system.time(sign1(a))
## user system elapsed
## 0.381 0.000 0.381
system.time(b<-sapply(1:length(a), function(i)
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
return(c)
}, USE.NAMES = F)
)
## user system elapsed
## 0.018 0.000 0.018
sign2<-function(a) {b<-sapply(1:length(a), function(i)
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
return(c)
}, USE.NAMES = F)
return(b)
}
system.time(sign2(a))
## user system elapsed
## 0.019 0.000 0.018
system.time(b<-sapply(1:length(a), function(i)
{if(a[i] >= 0) c <- "pos"
if(a[i] < 0) c <- "neg"
return(c)
}, USE.NAMES = F)
)
## user system elapsed
## 0.018 0.000 0.017
sign3<-function(a) {b<-sapply(1:length(a), function(i)
{
if(a[i] > 0) c <- "pos" else c <- "neg"
return(c)
}, USE.NAMES = F)
return(b)
}
system.time(b<-sapply(1:length(a), function(i)
{
if(a[i] > 0) c <- "pos" else c <- "neg"
return(c)
}, USE.NAMES = F)
)
## user system elapsed
## 0.014 0.000 0.014
system.time(sign3(a))
## user system elapsed
## 0.014 0.000 0.014
BENCHMARKING & COMPILING
library(microbenchmark)
library(ggplot2)
library(compiler)
a<-rnorm(10)
compare <- microbenchmark(sign1(a), sign3(a), times = 10)
compare
## Unit: microseconds
## expr min lq mean median uq max
## sign1(a) 36341.254 36559.273 36905.9557 36943.6925 37188.73 37520.603
## sign3(a) 33.381 44.413 57.5728 59.9505 62.63 94.987
## neval cld
## 10 b
## 10 a
autoplot(compare)

#compile functions
cmpsign1 <- cmpfun(sign1)
cmpsign3 <- cmpfun(sign3)
compare <- microbenchmark(sign1(a), cmpsign1(a), times = 10)
compare
## Unit: milliseconds
## expr min lq mean median uq max neval
## sign1(a) 49.29575 49.76160 52.27990 51.11066 52.21563 63.60253 10
## cmpsign1(a) 49.01154 49.81009 50.24981 49.95914 51.22354 51.39959 10
## cld
## a
## a
autoplot(compare)

compare <- microbenchmark(sign3(a), cmpsign3(a), times = 1000)
compare
## Unit: microseconds
## expr min lq mean median uq max neval cld
## sign3(a) 25.802 28.7235 35.64076 30.4905 40.1535 1237.494 1000 b
## cmpsign3(a) 19.643 23.5505 27.63959 24.7630 26.7455 131.701 1000 a
autoplot(compare)

PARALLEL COMPUTING
library(parallel)
sign3<-function(a) {b<-sapply(1:length(a), function(i)
{
if(a[i] > 0) c <- "pos" else c <- "neg"
return(c)
}, USE.NAMES = F)
return(b)
}
sign4<-function(a) {b<-mclapply(1:length(a), function(i)
{if(a[i] > 0) c <- "pos" else c <- "neg"
return(c)
}, mc.cores = 3)
return(b)
}
compare <- microbenchmark(sign4(a), sign3(a), times = 10)
compare
## Unit: microseconds
## expr min lq mean median uq max
## sign4(a) 10153.437 11976.695 15035.3556 15016.5280 17945.520 19905.241
## sign3(a) 64.783 106.081 119.3791 114.2075 135.177 183.721
## neval cld
## 10 b
## 10 a
autoplot(compare)

library(warbleR)
## Loading required package: maps
##
## # ATTENTION: maps v3.0 has an updated 'world' map. #
## # Many country borders and names have changed since 1990. #
## # Type '?world' or 'news(package="maps")'. See README_v3. #
##
##
## Loading required package: seewave
## Loading required package: tuneR
## tuneR >= 1.0 has changed its Wave class definition.
## Use updateWave(object) to convert Wave objects saved with previous versions of tuneR.
##
## NOTE: install ggplot2 to run coor.graph()
data(coor.sing)
compare <- microbenchmark(coor.test(coor.sing, iterations = 100, parallel = 1),coor.test(coor.sing, iterations = 100, parallel = 2),coor.test(coor.sing, iterations = 100, parallel = 3), coor.test(coor.sing, iterations = 100, parallel = 4), times = 10)
compare
## Unit: seconds
## expr min lq
## coor.test(coor.sing, iterations = 100, parallel = 1) 1.543050 1.555008
## coor.test(coor.sing, iterations = 100, parallel = 2) 1.541999 1.587333
## coor.test(coor.sing, iterations = 100, parallel = 3) 1.537766 1.567157
## coor.test(coor.sing, iterations = 100, parallel = 4) 1.536639 1.567414
## mean median uq max neval cld
## 1.676857 1.568808 1.697083 2.310337 10 a
## 1.614467 1.608026 1.648680 1.704658 10 a
## 1.729671 1.650988 1.764038 2.133786 10 a
## 1.744388 1.679362 1.844091 2.169849 10 a
autoplot(compare)

PROFILING
a<-rnorm(1000)
Rprof("out.out")
for (i in 1:100) pos = sign2(a)
Rprof(NULL)
summaryRprof("out.out")
## $by.self
## self.time self.pct total.time total.pct
## "FUN" 0.08 44.44 0.10 55.56
## "lapply" 0.06 33.33 0.16 88.89
## "sapply" 0.02 11.11 0.18 100.00
## "<" 0.02 11.11 0.02 11.11
##
## $by.total
## total.time total.pct self.time self.pct
## "sapply" 0.18 100.00 0.02 11.11
## "<Anonymous>" 0.18 100.00 0.00 0.00
## "block_exec" 0.18 100.00 0.00 0.00
## "call_block" 0.18 100.00 0.00 0.00
## "eval" 0.18 100.00 0.00 0.00
## "evaluate_call" 0.18 100.00 0.00 0.00
## "handle" 0.18 100.00 0.00 0.00
## "in_dir" 0.18 100.00 0.00 0.00
## "process_file" 0.18 100.00 0.00 0.00
## "process_group" 0.18 100.00 0.00 0.00
## "process_group.block" 0.18 100.00 0.00 0.00
## "sign2" 0.18 100.00 0.00 0.00
## "withCallingHandlers" 0.18 100.00 0.00 0.00
## "withVisible" 0.18 100.00 0.00 0.00
## "lapply" 0.16 88.89 0.06 33.33
## "FUN" 0.10 55.56 0.08 44.44
## "<" 0.02 11.11 0.02 11.11
##
## $sample.interval
## [1] 0.02
##
## $sampling.time
## [1] 0.18
rw2s1 <- function(n) {
xpos = ypos = numeric(n)
xdir = c(TRUE, FALSE)
pm1 = c(1, -1)
for (i in 2:n) if (sample(xdir, 1)) {
xpos[i] = xpos[i - 1] + sample(pm1, 1)
ypos[i] = ypos[i - 1]
} else {
xpos[i] = xpos[i - 1]
ypos[i] = ypos[i - 1] + sample(pm1, 1)
}
list(x = xpos, y = ypos)
}
Rprof("out.out")
for (i in 1:100) pos = rw2s1(1000)
Rprof(NULL)
summaryRprof("out.out")
## $by.self
## self.time self.pct total.time total.pct
## "sample.int" 0.58 46.77 0.58 46.77
## "rw2s1" 0.42 33.87 1.24 100.00
## "sample" 0.22 17.74 0.82 66.13
## "length" 0.02 1.61 0.02 1.61
##
## $by.total
## total.time total.pct self.time self.pct
## "rw2s1" 1.24 100.00 0.42 33.87
## "<Anonymous>" 1.24 100.00 0.00 0.00
## "block_exec" 1.24 100.00 0.00 0.00
## "call_block" 1.24 100.00 0.00 0.00
## "eval" 1.24 100.00 0.00 0.00
## "evaluate_call" 1.24 100.00 0.00 0.00
## "handle" 1.24 100.00 0.00 0.00
## "in_dir" 1.24 100.00 0.00 0.00
## "process_file" 1.24 100.00 0.00 0.00
## "process_group" 1.24 100.00 0.00 0.00
## "process_group.block" 1.24 100.00 0.00 0.00
## "withCallingHandlers" 1.24 100.00 0.00 0.00
## "withVisible" 1.24 100.00 0.00 0.00
## "sample" 0.82 66.13 0.22 17.74
## "sample.int" 0.58 46.77 0.58 46.77
## "length" 0.02 1.61 0.02 1.61
##
## $sample.interval
## [1] 0.02
##
## $sampling.time
## [1] 1.24
Rprof("out.out")
coor.test(coor.sing, iterations = 100, less.than.chance = T, parallel = F)
## sing.event obs.ovlps mean.random.ovlps p.value
## 1 ovlp 62 39.21 1.00
## 2 no.sync 43 40.36 0.85
## 3 no.ovlp 23 40.71 0.00
Rprof(NULL)
summaryRprof("out.out")
## $by.self
## self.time self.pct total.time total.pct
## "[[.data.frame" 0.42 17.36 1.22 50.41
## "FUN" 0.34 14.05 2.42 100.00
## "match" 0.26 10.74 0.44 18.18
## "[[" 0.22 9.09 1.44 59.50
## "%in%" 0.22 9.09 0.64 26.45
## "$.data.frame" 0.20 8.26 1.62 66.94
## "sys.call" 0.14 5.79 0.14 5.79
## "<Anonymous>" 0.12 4.96 2.42 100.00
## "$" 0.12 4.96 1.74 71.90
## "all" 0.06 2.48 0.06 2.48
## "data.frame" 0.04 1.65 0.16 6.61
## ":" 0.04 1.65 0.04 1.65
## "lapply" 0.02 0.83 2.42 100.00
## "as.data.frame.numeric" 0.02 0.83 0.08 3.31
## "[.data.frame" 0.02 0.83 0.06 2.48
## "deparse" 0.02 0.83 0.06 2.48
## "sample" 0.02 0.83 0.04 1.65
## "-" 0.02 0.83 0.02 0.83
## "is.na" 0.02 0.83 0.02 0.83
## "make.unique" 0.02 0.83 0.02 0.83
## "names" 0.02 0.83 0.02 0.83
## ".subset2" 0.02 0.83 0.02 0.83
## "unique" 0.02 0.83 0.02 0.83
## "unlist" 0.02 0.83 0.02 0.83
##
## $by.total
## total.time total.pct self.time self.pct
## "FUN" 2.42 100.00 0.34 14.05
## "<Anonymous>" 2.42 100.00 0.12 4.96
## "lapply" 2.42 100.00 0.02 0.83
## "block_exec" 2.42 100.00 0.00 0.00
## "call_block" 2.42 100.00 0.00 0.00
## "coor.test" 2.42 100.00 0.00 0.00
## "eval" 2.42 100.00 0.00 0.00
## "evaluate_call" 2.42 100.00 0.00 0.00
## "handle" 2.42 100.00 0.00 0.00
## "in_dir" 2.42 100.00 0.00 0.00
## "lapp" 2.42 100.00 0.00 0.00
## "process_file" 2.42 100.00 0.00 0.00
## "process_group" 2.42 100.00 0.00 0.00
## "process_group.block" 2.42 100.00 0.00 0.00
## "sapply" 2.42 100.00 0.00 0.00
## "withCallingHandlers" 2.42 100.00 0.00 0.00
## "withVisible" 2.42 100.00 0.00 0.00
## "$" 1.74 71.90 0.12 4.96
## "$.data.frame" 1.62 66.94 0.20 8.26
## "[[" 1.44 59.50 0.22 9.09
## "[[.data.frame" 1.22 50.41 0.42 17.36
## "%in%" 0.64 26.45 0.22 9.09
## "match" 0.44 18.18 0.26 10.74
## "data.frame" 0.16 6.61 0.04 1.65
## "sys.call" 0.14 5.79 0.14 5.79
## "as.data.frame.numeric" 0.08 3.31 0.02 0.83
## "as.data.frame" 0.08 3.31 0.00 0.00
## "all" 0.06 2.48 0.06 2.48
## "[.data.frame" 0.06 2.48 0.02 0.83
## "deparse" 0.06 2.48 0.02 0.83
## "[" 0.06 2.48 0.00 0.00
## "force" 0.06 2.48 0.00 0.00
## "paste" 0.06 2.48 0.00 0.00
## ":" 0.04 1.65 0.04 1.65
## "sample" 0.04 1.65 0.02 0.83
## "make.names" 0.04 1.65 0.00 0.00
## "order" 0.04 1.65 0.00 0.00
## "-" 0.02 0.83 0.02 0.83
## "is.na" 0.02 0.83 0.02 0.83
## "make.unique" 0.02 0.83 0.02 0.83
## "names" 0.02 0.83 0.02 0.83
## ".subset2" 0.02 0.83 0.02 0.83
## "unique" 0.02 0.83 0.02 0.83
## "unlist" 0.02 0.83 0.02 0.83
## "mode" 0.02 0.83 0.00 0.00
## "simplify2array" 0.02 0.83 0.00 0.00
##
## $sample.interval
## [1] 0.02
##
## $sampling.time
## [1] 2.42