如何正确评估脚本的性能?

时间:2018-08-06 09:10:26

标签: php

我有以下两个脚本版本的示例:

It
is
test
string
for
performance
Total Execution Time: 1.5099843343099E-7 Mins
It
is
test
string
for
performance
Total Execution Time 2: 6.3578287760417E-8 Mins 

我想衡量哪个版本的脚本更有效,但是得到的结果却很奇怪。即使我交换了两个脚本,第二个脚本的执行时间也总是更长。例如:

library(ggplot2)
library(png)
library(grid)
library(egg)

i <- readPNG(system.file("img", "Rlogo.png", package="png"))
img <- data.frame(x = 6, y = 3)
img$g <-  list(rasterGrob(i, width=unit(24,"pt")))

ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point() +
  geom_custom(data=img, aes(x,y,data=g), grob_fun=identity) +
  coord_fixed()

如何最好地衡量此类脚本的性能?

1 个答案:

答案 0 :(得分:0)

一点也不奇怪。输出的时间是科学的。

1.5099843343099E-7 Mins

相同
0.00000015099843343099 minutes

这听起来很正确。
输出该字符串应该非常快,这似乎也是如此。

在同一代码上获得略有不同的结果并不罕见。
它取决于计算机的CPU和内存,如果后台任务的运行与其中一种代码的运行一样,则会有所不同。


回答您的评论:

$string = "It is test string for performance";

for($i=0;$i<10;$i++){
    $time_start = microtime(true); 
    $explode = explode(' ', $string);
    foreach ($explode as $s) {
        echo $s . "<br />";
    }

    $time_end = microtime(true);

    $execution_time[] = ($time_end - $time_start)/60;


    $time_start2 = microtime(true); 

    foreach (explode(' ', $string) as $s) {
        echo $s . "<br />";
    }

    $time_end2 = microtime(true);

    $execution_time2[] = ($time_end2 - $time_start2)/60;
}

var_dump($execution_time, $execution_time2);

这应该告诉您什么是最好/最快的。但是又有这么小的差异,我怀疑您会注意到它。
https://3v4l.org/6LcIg

相关问题