profiler.pdf

(111 KB) Pobierz
Profiling R Code
Roger D. Peng, Associate Professor of Biostatistics
Johns Hopkins Bloomberg School of Public Health
Why is My Code So Slow?
·
Profiling is a systematic way to examine how much time is spend in different parts of a program
·
Useful when trying to optimize your code
·
Often code runs fine once, but what if you have to put it in a loop for 1,000 iterations? Is it still fast
enough?
·
Profiling is better than guessing
2/17
On Optimizing Your Code
·
Getting biggest impact on speeding up code depends on knowing where the code spends most
of its time
·
This cannot be done without performance analysis or profiling
We should forget about small efficiencies, say about 97% of the time: premature
optimization is the root of all evil
--Donald Knuth
3/17
General Principles of Optimization
·
Design first, then optimize
·
Remember: Premature optimization is the root of all evil
·
Measure (collect data), don’t guess.
·
If you're going to be scientist, you need to apply the same principles here!
4/17
Using
system.time()
·
Takes an arbitrary R expression as input (can be wrapped in curly braces) and returns the
amount of time taken to evaluate the expression
·
Computes the time (in seconds) needed to execute an expression
-
If there’s an error, gives time until the error occurred
·
Returns an object of class
proc_time
- user time:
time charged to the CPU(s) for this expression
- elapsed time:
"wall clock" time
5/17
Zgłoś jeśli naruszono regulamin