note 109859 added to function.get-magic-quotes-gpc

From: Date: Tue, 28 Aug 2012 22:12:03 +0000
Subject: note 109859 added to function.get-magic-quotes-gpc
Groups: php.notes 
Request: Send a blank email to php-notes+get-190250@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)! ---- Server IP: 91.199.167.223 Probable Submitter: 81.205.252.247 ---- Manual Page -- http://www.php.net/manual/en/function.get-magic-quotes-gpc.php Edit -- https://master.php.net/note/edit/109859 Del: integrated -- https://master.php.net/note/delete/109859/integrated Del: useless -- https://master.php.net/note/delete/109859/useless Del: bad code -- https://master.php.net/note/delete/109859/bad+code Del: spam -- https://master.php.net/note/delete/109859/spam Del: non-english -- https://master.php.net/note/delete/109859/non-english Del: in docs -- https://master.php.net/note/delete/109859/in+docs Del: other reasons-- https://master.php.net/note/delete/109859 Reject -- https://master.php.net/note/reject/109859 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190250) next »