Re: FW: [PHP] Accessing a Char in an Array

From: Date: Tue, 21 Dec 2004 05:36:56 +0000
Subject: Re: FW: [PHP] Accessing a Char in an Array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-204940@lists.php.net to get a copy of this message
* Thus wrote Justin Palmer: > Why is substr all over the board in how fast it processes the same > string? Is it the server? Benchmarking can be a funny thing. Usually benchmarks take an average of serveral thousand iterations in order to find the average time it take something to do. Benchmarking with the output of a browser can lead to different results on each request for a varity of reasons like: 1) processor load (and capacity) at the time 2) memory allocation for the paticular request (which will vary uppon each request) 3) something I overlooked. Another thing to note is that there generally is more overhead on the first few routines of a benchmark, try swapping your test so that the 'String Array' is done first. Another thing to note is your test script has a lot of things that can factor speed issues. In order to test the difference between to different methods you want to set something up like: <?php // initialize variable before time testing // so memory allocation issues are avoided. $i=0; $var = ' '; $str = 'a b c d'; //start timer for($i=0; $i <= 1000; $i++ ) { $var = $str{0}; } //end timer //start timer for($i=0; $i <= 1000; $i++ ) { $var = substr($str, 0, 1); } //end timer Then check the results, record them and swap the two benchmarks. check the results and record. All in all, you probably will still see that the $str{0} is faster, and as mentioned, its because its substr() is a function and $str{0} is handled by the Zend Engine. A time of substr result = 0.0000370000, is rather quick and is can be assumed that there is a large amount of error (based on the level of accuracy we are dealing with). On my 450hz machine I wont accept a page that takes longer than 0.100 to execute. Which is a very long time compared to the 0.000034 for substr(). HTH, Curt. Curt -- Quoth the Raven, "Nevermore."

« previous php.general (#204940) next »