View difference between Paste ID: 2xftNXip and VRbDHsEv
SHOW: | | - or go back to the newest paste.
1
<?php
2
3
/**
4
 * Performance test for 'Checking if an instance's class implements an interface?'
5
 * See: http://stackoverflow.com/q/274360/473961
6
 *
7
 * Results for 100,000 iterations:
8-
 * class_implements + in_array: 149.19 ms
8+
 * class_implements + isset: 149.19 ms
9
 * Reflection: 294.79 ms
10
 * InstanceOf (with cheap __construct()): 84.65 ms
11
 */
12
13
interface IInterface
14
{
15
}
16
17
class TheClass implements IInterface
18
{
19
}
20
21
$tmp = false;
22
$interfaces = null;
23
$class = null;
24
    
25
$start = microtime(true);
26
for ($i = 0; $i < 100000; $i++) { 
27
    $interfaces = class_implements('TheClass');
28
29
    $tmp = isset($interfaces['IInterface']);
30
}
31
$end = microtime(true);
32
33
$total = round(($end - $start) * 1000.0, 2);
34-
echo "class_implements + in_array: " . $total . " ms\n";
34+
echo "class_implements + isset: " . $total . " ms\n";
35
    
36
$start = microtime(true);
37
for ($i = 0; $i < 100000; $i++) {
38
    $class = new ReflectionClass('TheClass');
39
    $tmp = $class->implementsInterface('IInterface');
40
}
41
$end = microtime(true);
42
43
$total = round(($end - $start) * 1000.0, 2);
44
echo "Reflection: " . $total . " ms\n";
45
    
46
$start = microtime(true);
47
for ($i = 0; $i < 100000; $i++) {
48
    $class = new TheClass();
49
    $tmp = ($class instanceof IInterface);
50
}
51
$end = microtime(true);
52
53
$total = round(($end - $start) * 1000.0, 2);
54
echo "InstanceOf: " . $total . " ms\n";