I want to run a code chunk and see how much of my CPU is being used. The equivalent of using task manager (activity monitor in mac), is there any R-way to achieve that?
Asked
Active
Viewed 187 times
3 Answers
3
Perhaps you could make a system call, e.g.
cpu_log <- c()
for (i in seq(1, 1000, 1)) {
print(i)
cpu_log[[i]] <- system("ps -A -o %cpu | awk '{ cpu += $1} END {print cpu}' ", intern = TRUE)
}
plot(y = cpu_log, x = 1:1000)

jared_mamrot
- 22,354
- 4
- 21
- 46
1
I don't know if this is the best way, but you can use proc.time(), which is part of base r.
ptm <- proc.time()
for (i in 1:1000) {rnorm(100000)} #insert your code here
CPU_usage<-proc.time() - ptm #compute the difference
CPU_usage
user system elapsed
6.407 0.385 6.896
There is more written about user, sys, and elapsed time here.

Joe Erinjeri
- 1,200
- 1
- 7
- 15
0
You can use system.time
:
system.time(for(i in 1:100) mad(runif(1000)))
# User System verstrichen
# 0.035 0.000 0.045
or using profvis
:
library(profvis)
profvis({
dat <- data.frame(
x = rnorm(5e4),
y = rnorm(5e4)
)
plot(x ~ y, data = dat)
m <- lm(x ~ y, data = dat)
abline(m, col = "red")
})

GKi
- 37,245
- 2
- 26
- 48