Created
February 28, 2017 21:43
-
-
Save bobmagicii/fb9678b79a56855a9b25aa5f0c0b38a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*// | |
PS C:\Users\bob\Desktop> php .\test.php | |
Results For 1,000,000 Iterations | |
=> Test Concat: 0.294673s | |
=> Test Eval: 0.258425s | |
=> Test PrintF: 0.583331s | |
//*/ | |
class SpeedTest { | |
/*// | |
provides meta info and a callable test that we can perform. | |
//*/ | |
public $Name = 'Test'; | |
public $Func = NULL; | |
public $Time = 0.0; | |
public function | |
__construct(String $Name, Callable $Func) { | |
$this->Name = $Name; | |
$this->Func = $Func; | |
return; | |
} | |
}; | |
class Timer { | |
/*// | |
perform rapid execution and time measurement for a series of tests. | |
//*/ | |
protected | |
$Tests = []; | |
/*// | |
stores the SpeedTests we want to run. | |
//*/ | |
protected | |
$Iterations = 0; | |
/*// | |
@type Int | |
how many times we want to perform the tests. | |
//*/ | |
public function | |
Add(SpeedTest $Test): | |
self { | |
/*// | |
add a new test to the pool. | |
//*/ | |
$this->Tests[] = $Test; | |
return $this; | |
} | |
public function | |
Run(Int $Iterations): | |
Void { | |
/*// | |
run the tests the specified number of times. | |
//*/ | |
$this->Iterations = $Iterations; | |
$this->Perform(); | |
$this->Results(); | |
return; | |
} | |
protected function | |
Perform(): | |
Void { | |
/*// | |
run the things we want to test. | |
//*/ | |
array_walk($this->Tests,function(SpeedTest $Test) { | |
$Time = microtime(TRUE); | |
$Iter = 0; | |
for($Iter = 0; $Iter < $this->Iterations; ++$Iter) | |
($Test->Func)($Iter); | |
$Test->Time = microtime(TRUE) - $Time; | |
return; | |
}); | |
return; | |
} | |
protected function | |
Results(): | |
Void { | |
/*// | |
print results of the tests. | |
//*/ | |
printf( | |
'Results For %s Iterations%s', | |
number_format($this->Iterations), | |
PHP_EOL | |
); | |
array_walk($this->Tests,function(SpeedTest $Test) { | |
printf( | |
'=> Test %s: %.6fs%s', | |
$Test->Name, | |
$Test->Time, | |
PHP_EOL | |
); | |
return; | |
}); | |
return; | |
} | |
}; | |
$Timer = (new Timer) | |
->Add(new SpeedTest('Concat',function(Int $Iter){ | |
return 'Concat: ' . $Iter . PHP_EOL; | |
})) | |
->Add(new SpeedTest('Eval',function(Int $Iter){ | |
return "Eval: {$Iter}\n"; | |
})) | |
->Add(new SpeedTest('PrintF',function(Int $Iter){ | |
return sprintf('PrintF: %d%s',$Iter,PHP_EOL); | |
})) | |
->Run(1000000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment