<?php /** * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > * @author Tim Starling */ //error_reporting(E_ALL| E_STRICT); //ini_set("display_errors", 1); function a1() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = count( $array ) == 0; } } function b1() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = $array === array(); } } function c1() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = empty( $array ); } } function d1() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = !$array; } } function a5() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = count( $array ) == 0; } } function b5() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = $array === array(); } } function c5() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = empty( $array ); } } function d5() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = !$array; } } function a100() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = count( $array ) == 0; } } function b100() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = $array === array(); } } function c100() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = empty( $array ); } } function d100() { global $array; for ( $i = 0; $i < 10000000; $i++ ) { $a = !$array; } } xhprof_enable( XHPROF_FLAGS_NO_BUILTINS ); $size = 1; $array = array_fill( 0, $size, 0 ); $array = array_map( function() { return mt_rand(); }, $array ); //$hash = array_fill_keys( $array, true ); // profile( 'findArray' ); // // profile( 'findHash' ); a1(); b1(); c1(); d1(); $size = 5; $array = array_fill( 0, $size, 0 ); $array = array_map( function() { return mt_rand(); }, $array ); //$hash = array_fill_keys( $array, true ); // profile( 'findArray' ); // // profile( 'findHash' ); a5(); b5(); c5(); d5(); $size = 100; $array = array_fill( 0, $size, 0 ); $array = array_map( function() { return mt_rand(); }, $array ); //$hash = array_fill_keys( $array, true ); // profile( 'findArray' ); // // profile( 'findHash' ); a100(); b100(); c100(); d100(); $data = xhprof_disable(); foreach ( array( 1, 5, 100 ) as $size ) { echo "SIZE $size:\n"; $a = $data["main()==>a$size"]['wt']; $b = $data["main()==>b$size"]['wt']; $c = $data["main()==>c$size"]['wt']; $d = $data["main()==>d$size"]['wt']; printf( "a: %.3f ns\n", $a / 1000000 * 1000 ); printf( "b: %.3f ns\n", $b / 1000000 * 1000 ); printf( "c: %.3f ns\n", $c / 1000000 * 1000 ); printf( "d: %.3f ns\n", $d / 1000000 * 1000 ); } /* SIZE 1: a: 3375.365 ns b: 1282.860 ns c: 701.357 ns d: 703.902 ns SIZE 5: a: 4897.867 ns b: 1297.596 ns c: 702.082 ns d: 701.680 ns SIZE 100: a: 40561.674 ns b: 1282.999 ns c: 713.409 ns d: 703.935 ns */ ?>