准确地测量PHP和c#中类似函数的运行时


Accurately measuring the runtime of a function in PHP and a similar one in C#

一个问题的混合物,我在PHP中得到了以下代码,它只是旋转和图像,并乘以旋转该图像所需的时间:

<?php
ini_set("memory_limit", -1);
$im = imagecreatefromjpeg("test.jpg");
$time_start = microtime(true);
$rotate = imagerotate($im,90,0);
$time_end = microtime(true);
$execution_time = "Took ". ($time_end - $time_start) * 1000 ." to generate image!";
$white = imagecolorallocate($im,255,255,255);
$font = $_SERVER['DOCUMENT_ROOT']."/ppa/images/fonts/Calibri Bold.ttf";
imagettftext($rotate,15,0,20,20,$white,$font,$execution_time);
header("Content-Type: image/jpeg");
imagejpeg($rotate);
imagedestroy($im);
?>

如果我运行该代码,我得到大约404.000显示在我的图像上,现在我也有一些代码在c#中旋转相同的图像,我担心我使用的方法来时间虽然可能不匹配,我可能会得到错误的比较。

下面是我的c#代码:
private void rotatec_Click(object sender, EventArgs e)
    {
        Image image1 = pictureBox1.Image;
        if (image1 != null)
        {
            System.Diagnostics.Stopwatch s = System.Diagnostics.Stopwatch.StartNew();
            image1.RotateFlip(RotateFlipType.Rotate90FlipY);
            s.Stop();
            string text = "Time took: {0} ms" + s.ElapsedMilliseconds;
            pictureBox1.BackgroundImage = image1;
            image1.Save(@".'test.jpg");
            //Write to text file
            System.IO.StreamWriter file = new System.IO.StreamWriter(@".'timetook.txt");
            file.WriteLine(text);
            file.Close();
        } 
        else
        {
            MessageBox.Show("Please load an image first!");
        }
    }

我担心我使用的方法来计时,虽然可能不匹配,我可能会得到错误的比较,是这样吗?…

------------------------------------------------------

更新了我的代码,现在我得到:

402.023毫秒从PHP(如果我计算正确)c#

148毫秒

对于旋转图像来说似乎是现实的?

所以。你试着在不同的语言中测量操作旋转,并进行比较。但首先你必须确保你的操作能起到同样的作用。

在您的示例中,这并不是因为输入数据和输出数据完全不同。它是图像资源的内部表示。操作旋转做不同的事情

但是如果你在你的测量中包含打开和书写文件,你可以比较它。

注:不要忘记JPG有压缩级别和一组不同的压缩算法。确保输出文件相等(至少相似)。