1 | | @johnbillion . I tried putting the `judge` functions one above another and the results are pretty similar. Please have a look at the screenshot. |
2 | | |
3 | | [[Image(https://raw.githubusercontent.com/rnaby/screenshots/master/Screenshot-2018-04-29-10%3A44%3A20-001.png)]] |
4 | | |
5 | | Here is the testing script- |
6 | | |
7 | | |
8 | | {{{ |
9 | | <?php |
10 | | |
11 | | function _reset( $array ) { |
12 | | return reset($array); |
13 | | } |
14 | | |
15 | | function _foreach( $array ) { |
16 | | foreach( $array as $el ) { |
17 | | return $el; |
18 | | } |
19 | | } |
20 | | |
21 | | function judge($array, $method) { |
22 | | $loop = 10000; |
23 | | |
24 | | $time = microtime(true); |
25 | | |
26 | | for ($i = 0; $i < $loop; ++$i) { |
27 | | $temp = $method($array); |
28 | | } |
29 | | |
30 | | $time = microtime(true) - $time; |
31 | | |
32 | | echo implode(PHP_EOL, [ |
33 | | $method.':', |
34 | | " Time: {$time}s", |
35 | | " Result: {$temp}", |
36 | | '', |
37 | | '', |
38 | | ]); |
39 | | } |
40 | | |
41 | | $a = []; |
42 | | |
43 | | $index = 0; |
44 | | |
45 | | for ($i = 0; $i < 10000; ++$i) { |
46 | | $index += rand(1, 5); |
47 | | |
48 | | $a[$index] = substr(md5(rand()), 0, rand(3, 7)); |
49 | | } |
50 | | |
51 | | judge($a, '_foreach'); |
52 | | judge($a, '_reset'); |
53 | | }}} |