You are here: Re: speed of php Echo using Benchmark_Timer « All PHP « IT news, forums, messages
Re: speed of php Echo using Benchmark_Timer

Posted by Senator Jay Billington Bulworth on 10/13/00 11:26

sumeet <sumeet72@gmail.com> wrote in news:dg0kvi$m4a$1
@domitilla.aioe.org:

> dear friends,
>
> i have a simple code as follows. I would like to know why a single
'echo
> $str' is much slower than a echo 'echo $str[$i]'. The results are also
> shown below. it shows that ex time for a single 'echo $str' is 0.000142
> and for the forloop 'echo $str[$i]' is 0.000047.
>
> include_once('Benchmark/Profiler.php');
> include_once('Benchmark/Timer.php');
>
>
> $str = 'Sumeet Shroff';
>
> $benchmark_timer = new Benchmark_Timer(false);
>
> $benchmark_timer->start();
> echo $str;
> $benchmark_timer->stop();
>
> echo '<pre>';
> echo $benchmark_timer->display();
> echo '</pre>';
>
> $benchmark_timer->start();
> for( $i = 0; $i < strlen($str); $i++ ) {
> echo $str[$i];
> }
> $benchmark_timer->stop();
>
> echo '<pre>';
> echo $benchmark_timer->display();
> echo '</pre>';
>
> ----------------
>
> time index ex time %
> Start 1126422109.93350400 - 0.00%
> Stop 1126422109.93364600 0.000142 100.00%
> total - 0.000142 100.00%
>
> Sumeet Shroff
> time index ex time %
> Start 1126422109.93391200 - 0.00%
> Stop 1126422109.93395900 0.000047 100.00%
> total - 0.000047 100.00%

Hi,

Testing things on a single iteration is not reliable. A script might run
in a tenth of a second one time, and take three seconds to run the next
time, depending upon system load, amount of free RAM, etc. Benchmarking
should involve running a command sequence for some large number of times,
and timing the response.

I'm not familiar with the Benchmark class you seem to be using, so I ran
a local test using both examples.

Script 1:

<?php
$str = 'Sumeet Shroff';
for($i=0; $i<100000; $i++){
echo $str;
}
?>

[shaun@winfosec temp]$ time php echotime1.php >outfile

real 0m0.508s
user 0m0.016s
sys 0m0.492s

Script 2:

<?php
$str = 'Sumeet Shroff';
for($i=0; $i<100000; $i++){
for($j=0; $j<strlen($str); $j++){
echo $str[$j];
}
}
?>

[shaun@winfosec temp]$ time php echotime2.php >outfile

real 0m13.222s
user 0m1.273s
sys 0m11.941s

As you can see, the first script printed out "Sumeet Shroff" 100,000
times in approximately half a second. The second script, which iterated
through each character in the string "Sumeet Shroff," took 13 seconds to
print out your name 100,000 times.

Don't rely on a single iteration to optimize your loops. Try running them
several hundreds of thousands of times, and see which one comes out on
top.

hth

--

Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация