Create a gist now

Instantly share code, notes, and snippets.

proftable(), and improved summaryRprof() for code profiling in R
proftable <- function(file, lines=20) {
sample.interval <- as.numeric(strsplit(readLines(file, 1), "=")[[1L]][2L])/1e+06
profdata <- as.matrix(read.table(file, header=FALSE, sep=" ", colClasses="character", skip=1, fill=TRUE, na.strings=""))
total.time <- nrow(profdata)*sample.interval
stacktable <- data.frame(table(aaply(profdata, 1, function(x) paste(rev(na.omit(x)), collapse=" > "))))
names(stacktable) <- c("Stack","PctTime")
stacktable$PctTime <- 100*stacktable$PctTime/nrow(profdata)
stacktable <- stacktable[order(stacktable$PctTime, decreasing=TRUE), c("PctTime", "Stack")]
rownames(stacktable) <- NULL
attr(stacktable, "total.time") <- total.time
print(head(stacktable, lines), row.names=FALSE, right=FALSE, digits=3)
cat(paste("\nTotal Time:", total.time, "seconds"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment