search
HomeBackend DevelopmentPHP TutorialExample of getting target function execution time in php_PHP tutorial

Written a class to test the execution time of the target function. The following is the definition code of the class:

Copy code The code is as follows:

/**
* class EfficiencyTester
* Efficiency tester, test function running time
* @version 1.0 2013.04.13
* @author Kross
*/
class EfficiencyTester {
/**
* var $testTimes
* Number of tests
*/
private $testTimes = 1000;

/**
* function getTime()
* Get the timestamp
according to the time mode * @param $timeModel time mode, default: microseconds
* @return int timestamp
*/
private function getTime($timeModel = 'MS') {
if ($timeModel == 'MS') {
return microtime();
} Else if ($ Timemodel == 's') {
Return Time ();
} Else {
Return Microtime ();
}
}
/**
* function testOnce()
* Test the target function once and return the running time
* @param $functionName Target function name
* @param $timeModel Time mode, default: microseconds
* @return double The time it takes for the target function to run once (very random)
*/
public function testOnce($functionName, $timeModel = 'MS') { $startMicroTime = $this->getTime($timeModel);
$functionName();
        $endMicroTime = $this->getTime($timeModel);

$costMicroTime = $endMicroTime - $startMicroTime;

                                                                                                                            """""""""""""" > for ($i = 1; $i testTimes; $i++) {

$totalMicroTimes += $this->testOnce($functionName);
}
return $totalMicroTimes / $this->testTimes;
}
}
?>



The following is the test code of the class:




Copy code

The code is as follows:

require_once('../class/EfficiencyTester.class .php');

$e = new EfficiencyTester();

echo $e->test('rand');

?>

At first, I directly used microtime() to get the time. Later, I thought that if I wanted to get the running time in seconds, writing this way was not polymorphic enough, so I wrote a getTime() function to get it. Timestamps in different units, but in this way, it seems that the running time of the target function has become longer, possibly because the judgment in the getTime() function takes up part of the time.


http://www.bkjia.com/PHPjc/736812.html

www.bkjia.com

true
http: //www.bkjia.com/PHPjc/736812.html

TechArticle

Written a class to test the execution time of the target function. The following is the definition code of the class: Copy the code The code is as follows: ?php /** * class EfficiencyTester * Efficiency tester, test function...

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)