Изменения

Перейти к: навигация, поиск

R:Измерение времени выполнения выражений

86 байтов добавлено, 14:45, 28 мая 2014
м
Загрузка 2693-time-measure.wiki
<!-- R:Измерение времени выполнения выражений -->
 
system.time(mean(x))
#> user system elapsed
#> 0.017 013 0.000 003 0.016
system.time(sum(x) / length(x))
#> user system elapsed
#> 0.007 010 0.000 0.008
</nowiki>}}
{{r-code|code=<nowiki>replicate(10, system.time(mean(x))[["elapsed"]])
#> [1] 0.016 0.015 016 0.016 0.015 016 0.016 0.015 017 0.015 016 0.015 017 0.016 017 0.016017
</nowiki>}}
{{r-code|code=<nowiki>system.time(replicate(100, mean(x)))
#> user system elapsed
#> 1.547 586 0.000 027 1.550621
system.time(replicate(100, sum(x) / length(x)))
#> user system elapsed
#> 0.793 806 0.000 020 0.792832
</nowiki>}}
{{r-code|code=<nowiki>system.time(for (i in seq_len(100)) mean(x))
#> user system elapsed
#> 1.563 0.000 050 1.563620
system.time(for (i in seq_len(100)) sum(x) / length(x))
#> user system elapsed
#> 0.797 806 0.000 020 0.797830
</nowiki>}}
colMeansLoopVec(x), colMeans(x), replications = 100)
#> test replications elapsed relative user.self sys.self user.child sys.child
#> 1 colMeansApply(x) 100 14.083 974 1718.963 328 1213.793 380 1.277 540 0 0#> 4 colMeansLoopVec(x) 100 67.715 088 8.565 676 6.663 793 0.047 270 0 0#> 3 colMeansLoop(x) 100 78.514 004 9.584 797 7.423 630 0.083 340 0 0#> 2 colMeansVapply(x) 100 7.573 851 9.659 610 7.523 407 0.043 417 0 0#> 5 colMeans(x) 100 0.784 817 1.000 0.783 787 0.000 026 0 0
</nowiki>}}
columns = c("test", "replications", "elapsed", "relative"))
#> test replications elapsed relative
#> 5 colMeans(x) 100 0.786 797 1.000#> 4 colMeansLoopVec(x) 100 6.709 856 8.536602#> 3 colMeansLoop(x) 100 7.499 633 9.541577#> 2 colMeansVapply(x) 100 7.566 722 9.626689#> 1 colMeansApply(x) 100 1415.045 167 1719.869030
</nowiki>}}
#> Unit: milliseconds
#> expr min lq median uq max neval
#> colMeans(x) 7.808 805 7.824 7.834 841 7.854 865 8.326 295 100#> colMeansLoopVec(x) 65.742 429 67.976 680 7677.827 083 8179.280 107558 95.498 667 100#> colMeansVapply(x) 73.512 158 74.748 701 8183.382 481 87.439 116724 102.362 963 100#> colMeansLoop(x) 73.538 163 75.823 355 8285.490 780 8987.138 108986 110.388 389 100#> colMeansApply(x) 102103.575 124357 138.303 137125 145.139 143323 148.418 149876 180.543 600 100
</nowiki>}}
{{r-code|code=<nowiki>str(res)
#> Classes 'microbenchmark' and 'data.frame': 500 obs. of 2 variables:
#> $ expr: Factor w/ 5 levels "colMeansApply(x)",..: 3 2 4 2 2 1 5 2 3 5 5 5 2 1 4 1 ...#> $ time: num 8.49e+07 1.25e+08 7.81e+06 8.87e+07 7.83e+06 86781202 81161181 65429189 74434388 74424478 ...
</nowiki>}}
{{r-code|code=<nowiki>aggregate(time ~ expr, data = res, function(x) median(x) * 10^-6L)
#> expr time
#> 1 colMeansApply(x) 137145.139323#> 2 colMeansVapply(x) 8183.382481#> 3 colMeansLoop(x) 8285.490780#> 4 colMeansLoopVec(x) 7677.827083#> 5 colMeans(x) 7.834841
aggregate(time ~ expr, data = res, function(x) sum(x) * 10^-6L)
#> expr time
#> 1 colMeansApply(x) 1314914224.43#> 2 colMeansVapply(x) 82138193.34#> 3 colMeansLoop(x) 82948309.36#> 4 colMeansLoopVec(x) 75487483.80#> 5 colMeans(x) 786785.37
</nowiki>}}
#> colMeansApply(x) colMeansVapply(x) colMeansLoop(x) colMeansLoopVec(x)
#> colMeansVapply(x) < 2e-16 - - -
#> colMeansLoop(x) < 2e-16 0.53 31 - - #> colMeansLoopVec(x) < 2e-16 72.1e6e-07 09 36.7e9e-08 12 -
#> colMeans(x) < 2e-16 < 2e-16 < 2e-16 < 2e-16
#>

Навигация