面向对象php中php脚本运行时间的计算

面向对象php中php脚本运行时间的计算,php,performance,oop,time,Php,Performance,Oop,Time,我正在开发这个界面,拥有大量的数据集,并且有兴趣计算我正在运行的脚本的执行时间,并在以后根据时间对其进行改进。但是我不知道如何在我的代码中计算时间以及在哪里正确使用它。 这是我的代码块,从中显示结果输出 <?php class CSVFileWriter { public $filename; protected $filehandle; protected $counter = 0; protected $logger = NULL; publ

我正在开发这个界面,拥有大量的数据集,并且有兴趣计算我正在运行的脚本的执行时间,并在以后根据时间对其进行改进。但是我不知道如何在我的代码中计算时间以及在哪里正确使用它。 这是我的代码块,从中显示结果输出

<?php
class CSVFileWriter 
{
    public $filename;
    protected $filehandle;
    protected $counter = 0;
    protected $logger = NULL;

    public function __construct (\autodo\common\Logger $logger) {
        if (is_null($logger)) {
            $this->logger = new \autodo\common\NullLogger();
        }
        else {
            $this->logger = &$logger;
        }   
    }

    public function init() {
        // open/create file
        // maybe create tmp-File
    }

    public function process($data) {

        $tmp = $data->getCSV();
        /*
        @todo open file, write data
        */
        $this->logger->log("Write: ".$tmp, 
            \autodo\common\Logger::HINWEIS,
            7);

        $this->counter++;
    }

    public function finish() {
        // close file   

        $this->logger->log($this->counter." sets written to CSV", 
            \autodo\common\Logger::HINWEIS,
            5);
    }
}

最后面是正确的,这是一个类定义,而不是一个正在运行的脚本。正如TrtG所提到的,您需要选择您想要知道的执行时间的方法。类定义没有执行时间。如果不执行某些操作,则无法计算执行时间。如果您想知道流程的执行时间,可以尝试以下方法:

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$timeStart = microtime_float();

//Execute something 
$writer = new CSVFileWriter (null);
$writer->init();
$writer->process($yourData);
$writer->finish();

$yourWriterProcessExecutionTime = microtime_float() - $timeStart; 

那么问题是什么呢?正如我在问题中提到的,我不知道如何计算这个运行脚本的时间。那不是一个运行脚本,那只是一个类定义。运行的脚本应该实例化这个类并调用它的方法。还有一个地方,你必须计算执行时间,这是一个很琐碎的任务。你想用什么方法来测量执行时间?process()?没有单独的方法,只是希望看到执行时间…这次只帮助我改进代码。。。