1 | <?php |
---|
2 | |
---|
3 | // Color via GET |
---|
4 | $color_init = ( isset($_GET['color']) ) ? $_GET['color'] : "%23123456"; |
---|
5 | $repeat = ( isset($_GET['repeat']) && is_numeric($_GET['repeat']) ) ? $_GET['repeat'] : 2000000; |
---|
6 | |
---|
7 | |
---|
8 | // For the record |
---|
9 | echo "Looping $repeat times<br /><br />"; |
---|
10 | |
---|
11 | |
---|
12 | // Time the strpos() way |
---|
13 | $time_a1 = microtime(true); |
---|
14 | echo "Start strpos(): $time_a1<br />"; |
---|
15 | |
---|
16 | for ( $i = 0; $i < $repeat; $i++ ){ |
---|
17 | if ( 0 === strpos( $color_init, '#' ) ) |
---|
18 | $color = substr( $color_init, 1 ); |
---|
19 | elseif ( 0 === strpos( $color_init, '%23' ) ) |
---|
20 | $color = substr( $color_init, 3 ); |
---|
21 | else $color = $color_init; |
---|
22 | } |
---|
23 | |
---|
24 | echo "Final strpos() color: $color<br />"; |
---|
25 | $time_a2 = microtime(true); |
---|
26 | echo "End strpos(): $time_a2<br />"; |
---|
27 | echo "Total time: " . ($time_a2 - $time_a1) . "<br /><br />"; |
---|
28 | |
---|
29 | |
---|
30 | // Time the trim() way |
---|
31 | $time_b1 = microtime(true); |
---|
32 | echo "Start trim(): $time_b1<br />"; |
---|
33 | |
---|
34 | for ( $i = 0; $i < $repeat; $i++ ){ |
---|
35 | //$color = trim( urldecode( $color_init ), "#" ); //used as 2nd trim |
---|
36 | $color = trim( urldecode( $color_init ), "# \t\r\n\0\x0B" ); //replace existing diff's trim |
---|
37 | //$color = trim( urldecode( trim( $color_init ) ), "#" ); //double trim |
---|
38 | } |
---|
39 | |
---|
40 | echo "Final trim() color: $color<br />"; |
---|
41 | $time_b2 = microtime(true); |
---|
42 | echo "End trim(): $time_b2<br />"; |
---|
43 | echo "Total time: " . ($time_b2 - $time_b1) . "<br /><br />"; |
---|