-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprof_utils.R
29 lines (25 loc) · 1.05 KB
/
prof_utils.R
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
# Purpose: Super friendly profiling
# Date: 2016-03-13
time <- function(func, ...)
{
ptm <- proc.time()
func(...)
diff <- proc.time() - ptm
print(paste(round(diff["elapsed"], 2), "seconds"))
}
profile <- function(func, ...) {
Rprof(tmp <- tempfile(), memory.profiling = TRUE, interval = 0.01)
func(...)
Rprof(NULL)
prof_data <- summaryRprof(tmp, memory = "both")
selected_data <- data.frame(transpose(list(prof_data$by.self$mem.total, prof_data$by.self$self.pct)))
colnames(selected_data) <- gsub("^\"|\"$", "", rownames(prof_data$by.self))
rownames(selected_data) <- c("mem_pct", "time_pct")
mem_allocs <- sum(selected_data["mem_pct",])
if(mem_allocs != 0)
selected_data["mem_pct", ] <- selected_data["mem_pct", ] * 100 / mem_allocs
color_scheme <- c("blue", "purple")
barplot(as.matrix(selected_data), beside = TRUE, horiz = TRUE,
main = "Resource Usage", xlab = "Percentage (%)", ylab = "Functions Executed:",
legend.text = c("Time", "Memory"), col = color_scheme, args.legend = list(x = "topright", fill = color_scheme))
}