Tôi đã cố gắng sao chép Gnu Find ("tìm") trong PHP, nhưng có vẻ như không thể đạt được tốc độ gần như vậy. Việc triển khai PHP sử dụng ít nhất hai lần thời gian Tìm kiếm. Có cách nào nhanh hơn để làm điều này với PHP?Có thể tăng tốc độ quét tệp đệ quy trong PHP không?
EDIT: Tôi đã thêm một ví dụ mã sử dụng thực hiện SPL - hiệu quả của nó tương đương với phương pháp lặp đi lặp lại
EDIT2: Khi gọi tìm từ PHP nó đã thực sự chậm hơn so với việc thực hiện PHP mẹ đẻ. Tôi đoán tôi nên hài lòng với những gì tôi đã có :)
// measured to 317% of gnu find's speed when run directly from a shell
function list_recursive($dir) {
if ($dh = opendir($dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
$path = "$dir/$entry";
echo "$path\n";
if (is_dir($path)) list_recursive($path);
}
closedir($d);
}
}
// measured to 315% of gnu find's speed when run directly from a shell
function list_iterative($from) {
$dirs = array($from);
while (NULL !== ($dir = array_pop($dirs))) {
if ($dh = opendir($dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
$path = "$dir/$entry";
echo "$path\n";
if (is_dir($path)) $dirs[] = $path;
}
closedir($dh);
}
}
}
// measured to 315% of gnu find's speed when run directly from a shell
function list_recursivedirectoryiterator($path) {
$it = new RecursiveDirectoryIterator($path);
foreach ($it as $file) {
if ($file->isDot()) continue;
echo $file->getPathname();
}
}
// measured to 390% of gnu find's speed when run directly from a shell
function list_gnufind($dir) {
$dir = escapeshellcmd($dir);
$h = popen("/usr/bin/find $dir", "r");
while ('' != ($s = fread($h, 2048))) {
echo $s;
}
pclose($h);
}
Có thể là không. PHP là cấp cao và có thể sẽ không bao giờ khớp với thứ gì đó như Gnu Find (có thể được viết bằng C) về mặt tốc độ. Ngoài ra, nó rất nổi tiếng khi đệ quy. –
+1 ví dụ mã số – buggedcom