OT) PHP is not so fast as we think?
| From: | Norihisa Washitake | Date: | Fri, 28 Jun 2002 01:00:55 +0000 |
| Subject: | OT) PHP is not so fast as we think? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-104242@lists.php.net to get a copy of this message | ||
Hi all,
PHP is usually thought to be faster than perl, but sometimes PHP
runs slower than perl in some cases.
Belows is the example program in both PHP and Perl languages.
This solves "x^2 + y^2 + z^2 = 32" and shows an array of (x, y, z)
candidates. This program runs 1000 times for timekeeping.
-- PHP version --
#!/usr/local/php/bin/php
<?php
$num = 32;
for ($t=0; $t<1000; $t++) {
for ($i=0; $i<=$num; $i++) {
for ($j=0; $j<=$i; $j++) {
for ($k=0; $k<=$j; $k++) {
if ($i*$i + $j*$j + $k*$k == $num)
echo "$i, $j, $k\n";
}
}
}
}
?>
-- Perl Version --
#!/usr/bin/perl
$num = 32;
for ($t=0; $t<1000; $t++) {
for ($i=0; $i<=$num; $i++) {
for ($j=0; $j<=$i; $j++) {
for ($k=0; $k<=$j; $k++) {
print "$i, $j, $k\n" if ($i*$i + $j*$j + $k*$k == $num);
}
}
}
}
__END__
... and, they results in:
>> wassy% time ./4-3.php > /dev/null
>> 40.480u 0.020s 0:40.59 99.7% 0+0k 0+0io 670pf+0w
>> wassy% time ./4-3.pl > /dev/null
>> 9.690u 0.010s 0:09.69 100.1% 0+0k 0+0io 282pf+0w
Hey, perl is much faster, isn't it? :-(
Is there any better way (i mean, better performance) to improve this?
...or, we have to declare Perl the winner sometimes?
ps. I made C version and FORTRAN version.
C was the fastest, and next comes FORTRAN: they are
hundreds times faster than these script languages :-P
---
Norihisa Washitake nori@washitake.com
http://www.washitake.com/