A little benchmarking for all
It used to be that if an application was too slow you can just throw more hardware at it to scale. With the ever increasing speeds of CPUs and price drops of RAM this is still viable, but is obviously not an ideal solution in most cases. Like most problems, I think the answer to this is a little mix of both. While giving the application more hardware to work with will certainly help, you're going to get the most out of the new hardware if the application's code has some optimizations.
I've included a small micro benchmarking class for some simple script benchmarks. It allows for multiple profiles so you can track the time of more than one section of code at once. It's simple, all you do is create an instance and call the start/stop methods as needed. It records the micro time values in an array for later retrieval. You now have a small, simple and easy to use class to help you work out those bottlenecks in your code. Go forth young programmers, write the fastest code you can!
<?php
class benchmark
{
private $profiles;
public function __construct()
{
}
public function start($profile = 'default')
{
$this->profiles[$profile]['start'] = microtime(true);
}
public function stop($profile = 'default')
{
$this->profiles[$profile]['stop'] = microtime(true);
$this->profiles[$profile]['elapsed'] = $this->profiles[$profile]['stop'] - $this->profiles[$profile]['start'];
}
public function getTime($profile = 'default')
{
if($profile == 'all') {
return $this->profiles;
}
if(isset($this->profiles[$profile])) {
return $this->profiles[$profile];
}
return false;
}
}
$benchmark = new benchmark();
$benchmark->start();
for($x = 0; $x != 1000; $x++) {
$benchmark->start('second');
for($i = 0; $i != 10; $i++) {
$benchmark->start('third');
for($z = 0; $z != 1; $z++) {
$tmp = ($x + $i) * 32;
}
$benchmark->stop('third');
}
$benchmark->stop('second');
}
$benchmark->stop();
print_r($benchmark->getTime('all'));
?>
I've included a small micro benchmarking class for some simple script benchmarks. It allows for multiple profiles so you can track the time of more than one section of code at once. It's simple, all you do is create an instance and call the start/stop methods as needed. It records the micro time values in an array for later retrieval. You now have a small, simple and easy to use class to help you work out those bottlenecks in your code. Go forth young programmers, write the fastest code you can!
<?php
class benchmark
{
private $profiles;
public function __construct()
{
}
public function start($profile = 'default')
{
$this->profiles[$profile]['start'] = microtime(true);
}
public function stop($profile = 'default')
{
$this->profiles[$profile]['stop'] = microtime(true);
$this->profiles[$profile]['elapsed'] = $this->profiles[$profile]['stop'] - $this->profiles[$profile]['start'];
}
public function getTime($profile = 'default')
{
if($profile == 'all') {
return $this->profiles;
}
if(isset($this->profiles[$profile])) {
return $this->profiles[$profile];
}
return false;
}
}
$benchmark = new benchmark();
$benchmark->start();
for($x = 0; $x != 1000; $x++) {
$benchmark->start('second');
for($i = 0; $i != 10; $i++) {
$benchmark->start('third');
for($z = 0; $z != 1; $z++) {
$tmp = ($x + $i) * 32;
}
$benchmark->stop('third');
}
$benchmark->stop('second');
}
$benchmark->stop();
print_r($benchmark->getTime('all'));
?>


0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home