PHP, Perl & systemcalls 2

in handgestrickts earlier post on Tuesday, 29. May 2007, we described the big time differences between different code. we used systemcalls, PHP and Perl to solve a problem. PHP took nearly 14 times and Perl 1.5 times more than the systemcalls. today we tried another problem. this time PHP is superior.

the task: we wanted to list all directories in a given directory. inside of these directories we wanted to get all image files sorted by last modification descending. in the end we compare all results for differences and calculate the resulting times every process consumed. here is the code:

test.php:

<pre>
<?php
list($usec,$sec)=explode(' ',microtime());
$now1 = ((float)$usec+(float)$sec);

$files1 = explode("\n",trim(`find . -iregex ".*\\.\(jpeg\|jpg\|jpe\|gif\|png\)\$" -printf "%T@ %p\n" | sort -rn | sed -n "s/^[0-9]\+\\s\\.\\/// p"`));

list($usec,$sec)=explode(' ',microtime());
$now2 = ((float)$usec+(float)$sec);
echo 'find, sort and sed took: '.($now2-$now1)." seconds\n";
list($usec,$sec)=explode(' ',microtime());
$now1 = ((float)$usec+(float)$sec);

$files2 = array();
$handle1 = opendir('./');
while($data1=readdir($handle1)) {
        if($data1 != '.' && $data1 != '..' && is_dir($data1)) {
                $handle2 = opendir($data1);
                while($data2=readdir($handle2)) {
                        if(preg_match('/\.(?:jpeg|jpg|jpe|gif|png)$/i',$data2)) $files2[$data1.'/'.$data2] = filemtime($data1.'/'.$data2);
                }
                closedir($handle2);
        }
}
closedir($handle1);
arsort($files2,SORT_NUMERIC);
$files2 = array_keys($files2);

list($usec,$sec)=explode(' ',microtime());
$now2 = ((float)$usec+(float)$sec);
echo 'real php took: '.($now2-$now1)." seconds\n";
list($usec,$sec)=explode(' ',microtime());
$now1 = ((float)$usec+(float)$sec);

$files3 = explode("\n",trim(`perl test.pl`));

list($usec,$sec)=explode(' ',microtime());
$now2 = ((float)$usec+(float)$sec);
echo 'perl took: '.($now2-$now1)." seconds\n";

print_r(array_diff($files1,$files2,$files3));
?>
</pre>

test.pl:

#/usr/bin/perl
%files;
opendir(DIR1,'./');
foreach $data1 (grep { !/^\./ && -d "./$_" } readdir(DIR1)) {
        opendir(DIR2,'./'.$data1);
        foreach $data2 (grep { /\.(?:jpeg|jpg|jpe|gif|png)$/i } readdir(DIR2)) {
                $files{$data1.'/'.$data2} = (stat('./'.$data1.'/'.$data2))[9];
        }
        closedir(DIR2);
}
closedir(DIR1);
print join("\n",sort { $files{$b} <=> $files{$a} } keys %files);

the results in the browser:

find, sort and sed took: 0.0310471057892 seconds
real php took: 0.0205879211426 seconds
perl took: 0.0328810214996 seconds
Array
(
)

another surprise. this time perl is the slowest, even the differences are not very high, and PHP is very fast.

Sunday, 03. June 2007 • trackback url

Add Comment

( to reply to a comment, click the reply link next to the comment )

Comment Title:
Your Name:
Email Address:
Make Public?
Website:
Make Public?

Comment:


Allowed XHTML tags : a, b, i, strong, code, acrynom, blockquote, abbr. Linebreaks will be converted automatically.


Captcha:

captcha image

Soundfile:


please type the content of the above image or the soundfile into the following form-field: