note 109859 modified in function.get-magic-quotes-gpc by danbrown

From: 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&#39;ve done a little benchmarking using &#39;true&#39; profiling (using a registered tickhandler where declare(ticks=1)).&#13;&#10;I wondered whether or not json would not be terribly slow.&#13;&#10;I won&#39;t discuss the profiler, but will suffice with the following statement, followed by the used code to benchmark:&#13;&#10;&#13;&#10;The json method was by FAR the quickest (contrary to what I&#39;d thought), so if you need a speedy process, use that!&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;declare(ticks=1);&#13;&#10;function g()&#13;&#10;{&#13;&#10;&#9;return array(&#13;&#10;&#9;&#9;&#39;a&#39;=&#62;&#39;i\\\\\\&#39;m&#39;,&#13;&#10;&#9;&#9;array(&#39;a1&#39; =&#62; &#39;x&#39;, &#39;b1&#39; =&#62; &#39;x&#39;),&#13;&#10;&#9;&#9;array( &#39;a2&#39; =&#62; array(&#39;a3&#39; =&#62; &#39;\\\\\\&#39;x&#39;, &#39;b3&#39; =&#62; &#39;random&#39; )&#13;&#10;&#9;&#9;)&#13;&#10;&#9;);&#13;&#10;}&#13;&#10;&#13;&#10;function strip_json($d)&#13;&#10;{&#13;&#10;&#9;$rs = json_decode(stripslashes(json_encode($d, JSON_HEX_APOS)), true);&#13;&#10;&#9;return $rs;&#13;&#10;}&#13;&#10;&#13;&#10;function strip_deep1($d)&#13;&#10;{&#13;&#10;&#9;foreach ($d as $k=&#62;$v)&#13;&#10;&#9;{&#13;&#10;&#9;&#9;if (is_array($v)) { $d[$k] = strip_deep1($v); }&#13;&#10;&#9;&#9;else { $d[$k] = stripslashes($v); }&#13;&#10;&#9;}&#13;&#10;&#9;return $d;&#13;&#10;}&#13;&#10;&#13;&#10;function strip_deep2(&#38;$d)&#13;&#10;{&#13;&#10;&#9;$d = is_array($d) ? array_map(&#39;strip_deep2&#39;, $d) : stripslashes($d);&#13;&#10;&#9;return $d;&#13;&#10;}&#13;&#10;&#13;&#10;// now profile it&#13;&#10;require_once &#39;./TickProfiler.php&#39;;&#13;&#10;$N = 1000;&#13;&#10;$d = g();&#13;&#10;TickProfiler::Register();&#13;&#10;for ($i = 0; $i &#60; $N; $i++){ strip_json($d); }&#13;&#10;for ($i = 0; $i &#60; $N; $i++){ strip_deep1($d); }&#13;&#10;for ($i = 0; $i &#60; $N; $i++){ strip_deep2($d); }&#13;&#10;TickProfiler::Unregister();&#13;&#10;TickProfiler::Display();&#13;&#10;&#13;&#10;?&#62;&#13;&#10;&#13;&#10;My output (PHP 5.3.1, win32):&#13;&#10; [TickProfiler::tick] =&#62; Array&#13;&#10; (&#13;&#10; [time] =&#62; 0.76188707351685&#13;&#10; [calls] =&#62; 46001&#13;&#10; )&#13;&#10;&#13;&#10; [TickProfiler::Register] =&#62; Array&#13;&#10; (&#13;&#10; [time] =&#62; 3.0994415283203E-5&#13;&#10; [calls] =&#62; 1&#13;&#10; )&#13;&#10;&#13;&#10; [strip_json] =&#62; Array&#13;&#10; (&#13;&#10; [time] =&#62; 0.025638580322266&#13;&#10; [calls] =&#62; 1000&#13;&#10; )&#13;&#10;&#13;&#10; [strip_deep1] =&#62; Array&#13;&#10; (&#13;&#10; [time] =&#62; 0.40303444862366&#13;&#10; [calls] =&#62; 36000&#13;&#10; )&#13;&#10;&#13;&#10; [strip_deep2] =&#62; Array&#13;&#10; (&#13;&#10; [time] =&#62; 0.14928555488586&#13;&#10; [calls] =&#62; 9000&#13;&#10; )&#13;&#10;&#13;&#10;)&#13;&#10;&#13;&#10;As can be seen above, using json speeds output by at least a factor of 5 (nearly 6).&#13;&#10;Just wanted to share this :D&#13;&#10;&#13;&#10;Do note the strip_json function has two LOC instead of a plain return statement, otherwise it wouldn&#39;t get picked up by the tickprofiler (it would return from the code immediately, never reaching the profiler)!&#13;&#10;&#13;&#10;Usage of the return statement in strip_deep2 is not needed, as the argument is passed by reference.&#13;&#10;A new test showed that the time penalty for this is about 0.09 seconds.&#13;&#10;&#13;&#10;This actually means that the factor between strip_deep2 vs strip_json is only about 2.&#13;&#10;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

« previous php.notes (#190313) next »