Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Performance test for 'Checking if an instance's class implements an interface?'
- * See: http://stackoverflow.com/q/274360/473961
- *
- * Results for 100,000 iterations:
- * class_implements + isset: 149.19 ms
- * Reflection: 294.79 ms
- * InstanceOf (with cheap __construct()): 84.65 ms
- */
- interface IInterface
- {
- }
- class TheClass implements IInterface
- {
- }
- $tmp = false;
- $interfaces = null;
- $class = null;
- $start = microtime(true);
- for ($i = 0; $i < 100000; $i++) {
- $interfaces = class_implements('TheClass');
- $tmp = isset($interfaces['IInterface']);
- }
- $end = microtime(true);
- $total = round(($end - $start) * 1000.0, 2);
- echo "class_implements + isset: " . $total . " ms\n";
- $start = microtime(true);
- for ($i = 0; $i < 100000; $i++) {
- $class = new ReflectionClass('TheClass');
- $tmp = $class->implementsInterface('IInterface');
- }
- $end = microtime(true);
- $total = round(($end - $start) * 1000.0, 2);
- echo "Reflection: " . $total . " ms\n";
- $start = microtime(true);
- for ($i = 0; $i < 100000; $i++) {
- $class = new TheClass();
- $tmp = ($class instanceof IInterface);
- }
- $end = microtime(true);
- $total = round(($end - $start) * 1000.0, 2);
- echo "InstanceOf: " . $total . " ms\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement