secret
Last active

  • Download Gist
bench-map-deep.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
<?php
 
function rawurlencode_deep( $value ) {
if ( is_object( $value ) ) {
foreach( $value as $property => $property_value )
$value->{$property} = rawurlencode_deep( $property_value );
 
return $value;
} else if ( is_array( $value ) ) {
return array_map( 'rawurlencode_deep', $value );
} else {
return rawurlencode( $value );
}
}
 
function map_deep( $f, $value ) {
if ( is_object( $value ) ) {
foreach( $value as $property => $property_value )
$value->{$property} = map_deep( $f, $property_value );
 
return $value;
} else if ( is_array( $value ) ) {
return array_map( $f, $value );
} else {
return $f( $value );
}
}
 
$string = '9386258903475893052389589037 dfuhlksdfkajshfk shkl sdh#$%^&*(P)FGHJKNGVHJBNKMUYIOGHKJLGHKJL"""""asdjkagshdkjsahd';
$string = (object)array('baba' => 'dyado', 'x' => '123891731231', 'baba' => array( 'x' => 'bababa', 5 => 11), 'super' => 'duper & ? sad"',);
 
$times = 20000;
 
var_dump(rawurlencode_deep($string) == map_deep('rawurlencode', $string));
 
 
$start = microtime(true);
for($i=0; $i<$times; $i++) {
$s = map_deep('rawurlencode', $string);
}
echo (microtime(true) - $start)."\n";
 
 
$start = microtime(true);
for($i=0; $i<$times; $i++) {
$s = rawurlencode_deep($string);
}
echo (microtime(true) - $start)."\n";

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.