Created
January 28, 2019 16:07
-
-
Save kobus1998/bac9e3ed0f5e2b60380086831fe4b18c to your computer and use it in GitHub Desktop.
Performance test static vs non-static methods
This file contains hidden or 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 | |
| /* | |
| 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"; | |
This file contains hidden or 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 | |
| class NonStaticTest | |
| { | |
| public function execute() | |
| { | |
| array_fill(0, 100, 'a'); | |
| } | |
| } |
This file contains hidden or 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 | |
| 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