welcome bien v en u e willkommen
play

Welcome , Bien v en u e , Willkommen , ?? W R ITIN G E FFIC IE N T - PowerPoint PPT Presentation

Welcome , Bien v en u e , Willkommen , ?? W R ITIN G E FFIC IE N T R C OD E Colin Gillespie J u mping Ri v ers & Ne w castle Uni v ersit y WRITING EFFICIENT R CODE WRITING EFFICIENT R CODE A t y pical R w orkflo w # Load data_set <-


  1. Welcome , Bien v en u e , Willkommen , ?? W R ITIN G E FFIC IE N T R C OD E Colin Gillespie J u mping Ri v ers & Ne w castle Uni v ersit y

  2. WRITING EFFICIENT R CODE

  3. WRITING EFFICIENT R CODE

  4. A t y pical R w orkflo w # Load data_set <- read.csv("dataset.csv") # Plot plot(data_set$x, data_set$y) # Model lm(y ~ x, data = data_set) WRITING EFFICIENT R CODE

  5. When to optimi z e Premat u re optimi z ation is the root of all e v il Pop u lari z ed b y Donald Kn u th WRITING EFFICIENT R CODE

  6. R v ersion v2.0 La zy loading ; fast Main releases e v er y April loading of data w ith e . g . 3.0, 3.1, 3.2 minimal e x pense of s y stem Smaller b u g �x es thro u gho u t memor y. the y ear v2.13 Speeding u p e . g . 3.3.0, 3.3.1, 3.3.2 f u nctions w ith the b y te compiler v3.0 S u pport for large v ectors WRITING EFFICIENT R CODE

  7. Let ' s practice ! W R ITIN G E FFIC IE N T R C OD E

  8. M y code is slo w! W R ITIN G E FFIC IE N T R C OD E Colin Gillespie J u mping Ri v ers & Ne w castle Uni v ersit y

  9. Is m y code reall y slo w? 1 second ? 1 min u te ? 1 ho u r ? WRITING EFFICIENT R CODE

  10. Is m y code reall y slo w? WRITING EFFICIENT R CODE

  11. Benchmarking 1. We constr u ct a f u nction aro u nd the feat u re w e w ish to benchmark 2. We time the f u nction u nder di � erent scenarios , e . g . data set WRITING EFFICIENT R CODE

  12. E x ample : Seq u ence of n u mbers 1,2,3,…, n Option 1 1:n Option 2 Option 3 seq(1, n) seq(1, n, by = 1) WRITING EFFICIENT R CODE

  13. F u nction w rapping colon <- function(n) 1:n colon(5) 1 2 3 4 5 seq_default <- function(n) seq(1, n) seq_by <- function(n) seq(1, n, by = 1) WRITING EFFICIENT R CODE

  14. Timing w ith s y stem . time () system.time(colon(1e8)) u ser time is the CPU time charged for the e x ec u tion of # user system elapsed u ser instr u ctions . # 0.032 0.028 0.060 s y stem time is the CPU system.time(seq_default(1e8)) time charged for e x ec u tion b y the s y stem # user system elapsed on behalf of the calling # 0.060 0.028 0.086 process . system.time(seq_by(1e8)) elapsed time is appro x imatel y the s u m of # user system elapsed # 1.088 0.520 1.600 u ser and s y stem , this is the n u mber w e t y picall y care abo u t . WRITING EFFICIENT R CODE

  15. Storing the res u lt The tro u ble w ith The <- operator performs both : system.time(colon(1e8)) Arg u ment passing Object assignment is w e ha v en ' t stored the res u lt . system.time(res <- colon(1e8)) We need to rer u n to code store the res u lt The = operator performs one res <- colon(1e8) of : Arg u ment passing object assignment # Raises an error system.time(res = colon(1e8)) WRITING EFFICIENT R CODE

  16. Relati v e time Method Absol u te time ( secs ) Relati v e time 0.060/0.060 = 1.00 colon(n) 0.060 0.086/0.060 = 1.40 seq_default(n) 0.086 1.60/0.060 = 26.7 seq_by(n) 1.607 WRITING EFFICIENT R CODE

  17. Microbenchmark package Compares f u nctions Each f u nction is r u n m u ltiple times library("microbenchmark") n <- 1e8 microbenchmark(colon(n), + seq_default(n), + seq_by(n), + times = 10) # Run each function 10 times # Unit: milliseconds # expr min lq mean median uq max neval cld # colon(n) 59 130 220 202 341 391 10 a # seq_default(n) 94 204 290 337 348 383 10 a # seq_by(n) 1945 2044 2260 2275 2359 2787 10 b WRITING EFFICIENT R CODE

  18. Let ' s practice ! W R ITIN G E FFIC IE N T R C OD E

  19. Ho w good is y o u r machine ? W R ITIN G E FFIC IE N T R C OD E Colin Gillespie J u mping Ri v ers & Ne w castle Uni v ersit y

  20. E x periments ! Cost of e x periment : E x perimental eq u ipment Researcher time Not cheap ! WRITING EFFICIENT R CODE

  21. To b uy, or not to b uy... WRITING EFFICIENT R CODE

  22. To b uy, or not to b uy... Anal y sis takes t w ent y min u tes on y o u r c u rrent machine Ten min u tes to r u n on a ne w machine Yo u r time is charged at $100 per ho u r R u n si x t y anal y ses to pa y back the cost of a $1000 machine WRITING EFFICIENT R CODE

  23. The benchmarkme package install.packages("benchmarkme") library("benchmarkme") # Run each benchmark 3 times res <- benchmark_std(runs = 3) plot(res) M y machine is ranked 75 th o u t 400 machines upload_results(res) WRITING EFFICIENT R CODE

  24. Let ' s practice ! W R ITIN G E FFIC IE N T R C OD E

Recommend


More recommend