public
Created

proftable(), and improved summaryRprof() for code profiling in R

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
proftable <- function(file, lines=20) {
require(plyr)
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"))
invisible(stacktable)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.