Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
PHP Function Speed Test
<?php
ini_set('memory_limit' ,'-1');
class SpeedTest
{
private $loop;
public function SpeedTest($num) {
$this->loop = range(0, $num);
}
public function testForeach() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x = $num;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testFor() {
$x = 0;
$count = count($this->loop);
$timeStart = microtime(true);
for ($i=0; $i < $count; ++$i) {
$num = $this->loop[$i];
$x = $num;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testWhileEach() {
$x = 0;
reset($this->loop);
$timeStart = microtime(true);
while (list(, $num) = each($this->loop)) {
$x = $num;
}
$timeEnd = microtime(true);
reset($this->loop);
return $timeEnd - $timeStart;
}
public function testWhile() {
$x = 0;
$count = count($this->loop);
reset($this->loop);
$timeStart = microtime(true);
while ($x < $count) {
$num = $this->loop[$x];
++$x;
}
$timeEnd = microtime(true);
reset($this->loop);
return $timeEnd - $timeStart;
}
public function testIncrement1() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
++$x;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testIncrement2() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x++;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testIncrement3() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x = $x + 1;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testIncrement4() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x += 1;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testDecrement1() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
--$x;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testDecrement2() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x--;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testDecrement3() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x = $x - 1;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testDecrement4() {
$x = 0;
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$x -= 1;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testIfElse() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
if ($num % 2 == 0) {
$result = true;
} else {
$result = false;
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testTernaryOperator() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$result = $num % 2 == 0 ? true : false;
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testIfElseIf() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$res = $num % 3;
if ($num == 0) {
++$res;
} else if ($num == 1) {
++$res;
} else if ($num == 2) {
++$res;
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testSwitch() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$res = $num % 3;
switch ($res) {
case 0:
++$res;
break;
case 1:
++$res;
break;
case 2:
++$res;
break;
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testEqual2() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$res = $num % 2;
if ($num == 0) {
++$res;
} else if ($num == 1) {
++$res;
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testEqual3() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$res = $num % 2;
if ($num === 0) {
++$res;
} else if ($num === 1) {
++$res;
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testCount() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$list = array();
if (count($list) == 0) {
}
$list = array(0,1,2,3,4,5,6,7,8,9);
if (count($list) == 0) {
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testEmpty() {
$timeStart = microtime(true);
foreach ($this->loop as $num) {
$list = array();
if (empty($list)) {
}
$list = array(0,1,2,3,4,5,6,7,8,9);
if (empty($list)) {
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testIsSet() {
$timeStart = microtime(true);
$list = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
foreach ($this->loop as $num) {
if (isset($list['c'])) {
}
if (isset($list['x'])) {
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
public function testArrayKeyExists() {
$timeStart = microtime(true);
$list = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
foreach ($this->loop as $num) {
if (array_key_exists('c', $list)) {
}
if (array_key_exists('x', $list)) {
}
}
$timeEnd = microtime(true);
return $timeEnd - $timeStart;
}
}
$speedTest = new SpeedTest(1000000);
$result = array();
$result['foreach'] = $speedTest->testForeach();
$result['for'] = $speedTest->testFor();
$result['while_each'] = $speedTest->testWhileEach();
$result['while'] = $speedTest->testWhile();
$result['increment_front'] = $speedTest->testIncrement1();
$result['increment_back'] = $speedTest->testIncrement2();
$result['increment_add_one'] = $speedTest->testIncrement3();
$result['increment_add_one_short'] = $speedTest->testIncrement4();
$result['decrement_front'] = $speedTest->testDecrement1();
$result['decrement_back'] = $speedTest->testDecrement2();
$result['decrement_add_one'] = $speedTest->testDecrement3();
$result['decrement_add_one_short'] = $speedTest->testDecrement4();
$result['if_else'] = $speedTest->testIfElse();
$result['ternary_operator'] = $speedTest->testTernaryOperator();
$result['if_else_if'] = $speedTest->testIfElseIf();
$result['switch'] = $speedTest->testSwitch();
$result['equal2'] = $speedTest->testEqual2();
$result['equal3'] = $speedTest->testEqual3();
$result['empty'] = $speedTest->testEmpty();
$result['count'] = $speedTest->testCount();
$result['isset'] = $speedTest->testIsSet();
$result['array_key_exists'] = $speedTest->testArrayKeyExists();
echo '<html>';
echo '
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse;}
.center {text-align: center;}
.center table { margin-left: auto; margin-right: auto; text-align: left;}
.center th { text-align: center !important; }
td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.h {background-color: #9999cc; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
.vr {background-color: #cccccc; text-align: right; color: #000000;}
img {float: right; border: 0px;}
hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
</style>
';
echo '<body><div class="center"><h1>SPEED TEST</h1>';
echo '<table border="1" cellpadding="3" width="400">';
foreach ($result as $key => $value) {
echo '<tr>';
echo '<td class="e">'. $key .'</td>';
echo '<td class="v">'. $value .' (sec)</td>';
echo '</tr>';
}
echo '</table>';
echo '</div></body></html>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment