note 109859 modified in function.get-magic-quotes-gpc by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 15:38:29 +0000 |
| Subject: | note 109859 modified in function.get-magic-quotes-gpc by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190313@lists.php.net to get a copy of this message | ||
Regarding the three main strip methods as found below (two using foreach, 1 using the json method),
I've done a little benchmarking using 'true' profiling (using a registered
tickhandler where declare(ticks=1)). I wondered whether or not json would not be
terribly slow. I won't discuss the profiler, but will suffice with the
following statement, followed by the used code to benchmark: The
json method was by FAR the quickest (contrary to what I'd thought), so if you need a speedy
process, use
that! <?php declare(ticks=1); function
g() { 	return
array( 		'a'=>'i\\\\\\'m', 		array('a1'
=> 'x', 'b1' =>
'x'), 		array( 'a2' =>
array('a3' => '\\\\\\'x', 'b3'
=> 'random'
) 		) 	); } function
strip_json($d) { 	$rs =
json_decode(stripslashes(json_encode($d, JSON_HEX_APOS)), true); 	return
$rs; } function
strip_deep1($d) { 	foreach ($d as
$k=>$v) 	{ 		if (is_array($v)) {
$d[$k] = strip_deep1($v); } 		else { $d[$k] = stripslashes($v);
} 	} 	return
$d; } function
strip_deep2(&$d) { 	$d = is_array($d) ?
array_map('strip_deep2', $d) : stripslashes($d); 	return
$d; } // now profile
it require_once './TickProfiler.php'; $N =
1000; $d = g(); TickProfiler::Register(); for ($i
= 0; $i < $N; $i++){ strip_json($d); } for ($i = 0; $i < $N; $i++){
strip_deep1($d); } for ($i = 0; $i < $N; $i++){ strip_deep2($d);
} TickProfiler::Unregister(); TickProfiler::Display(); ?> My
output (PHP 5.3.1, win32): [TickProfiler::tick] =>
Array ( [time] =>
0.76188707351685 [calls] => 46001
) [TickProfiler::Register] => Array
( [time] => 3.0994415283203E-5
[calls] => 1 )
[strip_json] => Array ( [time]
=> 0.025638580322266 [calls] => 1000
) [strip_deep1] => Array
( [time] => 0.40303444862366
[calls] => 36000 )
[strip_deep2] => Array ( [time]
=> 0.14928555488586 [calls] => 9000
) ) As can be seen above,
using json speeds output by at least a factor of 5 (nearly 6). Just wanted to share
this :D Do note the strip_json function has two LOC instead of a
plain return statement, otherwise it wouldn't get picked up by the tickprofiler (it would
return from the code immediately, never reaching the
profiler)! Usage of the return statement in strip_deep2 is not
needed, as the argument is passed by reference. A new test showed that the time
penalty for this is about 0.09 seconds. This actually means that
the factor between strip_deep2 vs strip_json is only about 2. Still, strip_json
would be about twice as fast as strip_deep2
--was--
Regarding the three main strip methods as found below (two using foreach, 1 using the json method),
I've done a little benchmarking using 'true' profiling (using a registered
tickhandler where declare(ticks=1)).
I wondered whether or not json would not be terribly slow.
I won't discuss the profiler, but will suffice with the following statement, followed by the
used code to benchmark:
The json method was by FAR the quickest (contrary to what I'd thought), so if you need a speedy
process, use that!
<?php
declare(ticks=1);
function g()
{
return array(
'a'=>'i\\\'m',
array('a1' => 'x', 'b1' => 'x'),
array( 'a2' => array('a3' => '\\\'x', 'b3'
=> 'random' )
)
);
}
function strip_json($d)
{
$rs = json_decode(stripslashes(json_encode($d, JSON_HEX_APOS)), true);
return $rs;
}
function strip_deep1($d)
{
foreach ($d as $k=>$v)
{
if (is_array($v)) { $d[$k] = strip_deep1($v); }
else { $d[$k] = stripslashes($v); }
}
return $d;
}
function strip_deep2(&$d)
{
$d = is_array($d) ? array_map('strip_deep2', $d) : stripslashes($d);
return $d;
}
// now profile it
require_once './TickProfiler.php';
$N = 1000;
$d = g();
TickProfiler::Register();
for ($i = 0; $i < $N; $i++){ strip_json($d); }
for ($i = 0; $i < $N; $i++){ strip_deep1($d); }
for ($i = 0; $i < $N; $i++){ strip_deep2($d); }
TickProfiler::Unregister();
TickProfiler::Display();
?>
My output (PHP 5.3.1, win32):
[TickProfiler::tick] => Array
(
[time] => 0.76188707351685
[calls] => 46001
)
[TickProfiler::Register] => Array
(
[time] => 3.0994415283203E-5
[calls] => 1
)
[strip_json] => Array
(
[time] => 0.025638580322266
[calls] => 1000
)
[strip_deep1] => Array
(
[time] => 0.40303444862366
[calls] => 36000
)
[strip_deep2] => Array
(
[time] => 0.14928555488586
[calls] => 9000
)
)
As can be seen above, using json speeds output by at least a factor of 5 (nearly 6).
Just wanted to share this :D
Do note the strip_json function has two LOC instead of a plain return statement, otherwise it
wouldn't get picked up by the tickprofiler (it would return from the code immediately, never
reaching the profiler)!
http://php.net/manual/en/function.get-magic-quotes-gpc.php