PHP基于迭代实现文件夹复制、删除、查看大小等

前面一篇 PHP递归实现文件夹的复制、删除、查看大小操作 分析了递归操作使用技巧,这里再来分析一下迭代的操作技巧。

“既然递归能很好的解决,为什么还要用迭代呢”?主要的原因还是效率问题……

递归的概念是函数调用自身,把一个复杂的问题分解成与其相似的多个子问题来解决,可以极大的减少代码量,使得程序看起来非常优雅。

由于系统要为每次函数调用分配运行空间,并使用压栈予以记录。在函数调用结束后,系统需要释放空间,并弹栈恢复断点。所以递归的消耗还是比较大的。

即使语言设计时已经将函数调用优化的极度完美,达到可以忽略递归造成的资源浪费,但是递归的深度仍然会受到系统栈容量的限制,否则将会抛出 StackOverflowError 错误。

而迭代能很好的利用计算机适合做重复操作的特点,并且从理论上说,所有的递归函数都可以转换为迭代函数,所以尽量能不用递归就不用递归,能用迭代代替就用迭代代替。

查看文件夹大小

迭代的思路是让计算机对一组指令进行重复执行,在每次执行这组指令时,都从变量的原值推出其它的新值……重复这一过程直到达到结束条件或没有新值产生。

由于递归相当于循环加堆栈,所以可以在迭代中使用堆栈来进行递归和迭代的转换。

/** * 文件夹大小 * @param $path * @return int */ function dirsize($path) { /* 初始条件 */ $size = 0; $stack = array(); if (file_exists($path)) { $path = realpath($path) . 'https://www.jb51.net/'; array_push($stack, ''); } else { return -1; } /* 迭代条件 */ while (count($stack) !== 0) { $dir = array_pop($stack); $handle = opendir($path . $dir); /* 执行过程 */ while (($item = readdir($handle)) !== false) { if ($item == '.' || $item == '..') continue; $_path = $path . $dir . $item; if (is_file($_path)) $size += filesize($_path); /* 更新条件 */ if (is_dir($_path)) array_push($stack, $dir . $item . 'https://www.jb51.net/'); } closedir($handle); } return $size; }

复制文件夹

迭代和递归都具有初始化变量、判断结束条件、执行实际操作、产生新变量这四个步骤,只不过所在的位置不同罢了。

比如初始化变量这一步骤,在迭代中是位于函数的开始部分,而在递归中是指其他函数传递参数这一过程;

判断结束条件这一步骤,在迭代中用于判断循环是否继续,在递归中用于判断递归的结束位置;

执行实际操作在递归和迭代中都是函数的核心部分,位于产生新变量步骤之前;

产生新变量在迭代中是迭代继续的条件,在递归中是下一次递归的基础,由于产生了新变量才使得递归或迭代继续进行。

/** * 复制文件夹 * @param $source * @param $dest * @return string */ function copydir($source, $dest) { /* 初始条件 */ $stack = array(); $target = ''; if (file_exists($source)) { if (!file_exists($dest)) mkdir($dest); $source = realpath($source) . 'https://www.jb51.net/'; $dest = realpath($dest) . 'https://www.jb51.net/'; $target = realpath($dest); array_push($stack, ''); } /* 迭代条件 */ while (count($stack) !== 0) { $dir = array_pop($stack); $handle = opendir($source . $dir); if (!file_exists($dest . $dir)) mkdir($dest . $dir); /* 执行过程 */ while (($item = readdir($handle)) !== false) { if ($item == '.' || $item == '..') continue; $_source = $source . $dir . $item; $_dest = $dest . $dir . $item; if (is_file($_source)) copy($_source, $_dest); /* 更新条件 */ if (is_dir($_source)) array_push($stack, $dir . $item . 'https://www.jb51.net/'); } closedir($handle); } return $target; }

删除文件夹

抛开语言特性影响性能最多的就是冗余代码了,冗余代码通常是由于设计不到位而产生的。

多数情况下递归要比迭代冗余代码更多,这也是造成递归效率低的一大因素。

但当递归代码足够简练,冗余度足够低时,迭代的性能未必就比递归高。

比如这个用迭代实现的文件夹删除函数,速度就比递归要慢20%,主要原因是空文件夹的判断,在递归中当文件夹没有子文件夹时,函数会直接删除所有文件和当前文件夹,递归结束。

在迭代中即使文件夹为空也需要将其存入堆栈,下次迭代时再判断是否为空,之后才能删除。这就相比递归多了判断文件为空、存入堆栈、取出迭代等冗余操作,所以处理速度会比递归更慢。

/** * 删除文件夹 * @param $path * @return bool */ function rmdirs($path) { /* 初始化条件 */ $stack = array(); if (!file_exists($path)) return false; $path = realpath($path) . 'https://www.jb51.net/'; array_push($stack, ''); /* 迭代条件 */ while (count($stack) !== 0) { $dir = end($stack); $items = scandir($path . $dir); /* 执行过程 */ if (count($items) === 2) { rmdir($path . $dir); array_pop($stack); continue; } /* 执行过程 */ foreach ($items as $item) { if ($item == '.' || $item == '..') continue; $_path = $path . $dir . $item; if (is_file($_path)) unlink($_path); /* 更新条件 */ if (is_dir($_path)) array_push($stack, $dir . $item . 'https://www.jb51.net/'); } } return !(file_exists($path)); }

查看执行时间

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2b0260a4f9cc1ec2ff0ed25d0c31e20a.html