Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created January 28, 2019 16:07
Show Gist options
  • Select an option

  • Save kobus1998/bac9e3ed0f5e2b60380086831fe4b18c to your computer and use it in GitHub Desktop.

Select an option

Save kobus1998/bac9e3ed0f5e2b60380086831fe4b18c to your computer and use it in GitHub Desktop.
Performance test static vs non-static methods
<?php
/*
Tries: 1500000
Note: non-static only creates an instance once
Results (average):
Non-Static: 1.0577749512E-6
Static: 1.03780804429E-6
*/
require __DIR__ . '/NonStaticTest.php';
require __DIR__ . '/StaticTest.php';
/*
Start non-static
*/
$aTests = [];
$nonStatic = new NonStaticTest();
for ($i = 0; $i <= 1500000; $i++) {
$start = microtime(true);
$nonStatic->execute();
$aTests[] = microtime(true) - $start;
}
echo "Non-Static: ";
echo (array_sum($aTests) / count($aTests));
echo "\n";
/*
Start static
*/
$aTests = [];
for ($i = 0; $i <= 1500000; $i++) {
$start = microtime(true);
StaticTest::execute();
$aTests[] = microtime(true) - $start;
}
echo "Static: ";
echo (array_sum($aTests) / count($aTests));
echo "\n";
<?php
class NonStaticTest
{
public function execute()
{
array_fill(0, 100, 'a');
}
}
<?php
class StaticTest
{
public static function execute()
{
array_fill(0, 100, 'a');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment