-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprofiling
More file actions
97 lines (86 loc) · 1.66 KB
/
Copy pathprofiling
File metadata and controls
97 lines (86 loc) · 1.66 KB
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
system.time()
Rprof()
SummaryRprof()
by.self
by.total
sample.interval
sampling.time
//
Rprof(tmp <- tempfile())
example(glm)
Rprof()
summaryRprof(tmp)
//
require(profr)
require(ggplot2)
x = profr(example(glm))
ggplot(x)
//
Rprof(tmp <- tempfile())
example(glm)
Rprof()
plotProfileCallGraph(readProfileData(tmp), score = "total")
//
source("http://bioconductor.org/biocLite.R")
biocLite("Rgraphviz")
//
library(stockPortfolio)
fileName <- "Rprof_example.txt"
Rprof(fileName)
gr <- getReturns(c("GOOG", "MSFT", "IBM"), freq="week")
Rprof(NULL)
summaryRprof(fileName)$by.total[1:8,]
//
funAgg = function(x) {
# initialize res
res = NULL
n = nrow(x)
for (i in 1:n) {
if (!any(is.na(x[i,]))) res = rbind(res, x[i,])
}
res
}
funLoop = function(x) {
# Initialize res with x
res = x
n = nrow(x)
k = 1
for (i in 1:n) {
if (!any(is.na(x[i,]))) {
res[k, ] = x[i,]
k = k + 1
}
}
res[1:(k-1), ]
}
funApply = function(x) {
drop = apply(is.na(x), 1, any)
x[!drop, ]
}
funOmit = function(x) {
# The or operation is very fast, it is replacing the any function
# Also note that it doesn't require having another data frame as big as x
drop = F
n = ncol(x)
for (i in 1:n)
drop = drop | is.na(x[, i])
x[!drop, ]
}
#Make up large test case
xx = matrix(rnorm(2000000),100000,20)
xx[xx>2] = NA
x = as.data.frame(xx)
# Call the R code profiler and give it an output file to hold results
Rprof("exampleAgg.out")
# Call the function to be profiled
y = funAgg(xx)
Rprof(NULL)
Rprof("exampleLoop.out")
y = funLoop(xx)
Rprof(NULL)
Rprof("exampleApply.out")
y = funApply(xx)
Rprof(NULL)
Rprof("exampleOmit.out")
y = funOmit(xx)
Rprof(NULL)