Skip to content

Instantly share code, notes, and snippets.

@unique1984
Created September 3, 2019 21:39
Show Gist options
  • Save unique1984/41edf705869ea935f4398a92653fb5c5 to your computer and use it in GitHub Desktop.
Save unique1984/41edf705869ea935f4398a92653fb5c5 to your computer and use it in GitHub Desktop.
Create 500 folder and each one nested 1000 files and destroy them with php
#!/usr/bin/env bash
mkdir hugeNumberOfFiles
for i in `seq 500`; do mkdir hugeNumberOfFiles/$i; for j in `seq 1000`; do touch hugeNumberOfFiles/$i/$j.txt; done; done
for i in `seq 4`; do cp -r hugeNumberOfFiles hugeNumberOfFiles$i; done
# hugeNumberOfFiles -> 500 folders and nested total 500000 files
<?php
$total_start = microtime(true);
function rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") {
rrmdir($dir . "/" . $object);
} else {
unlink($dir . "/" . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
function rrmdirDI($path) {
// Remove a dir (all files and folders in it)
$i = new DirectoryIterator($path);
foreach($i as $f) {
if($f->isFile()) {
unlink($f->getRealPath());
}
else if(!$f->isDot() && $f->isDir()) {
rrmdirDI($f->getRealPath());
rmdir($f->getRealPath());
}
}
}
$start = microtime(true);
exec('rm -rf hugeNumberOfFiles4');
echo "exec " . (microtime(true) - $start) . " sn\n";
$start = microtime(true);
shell_exec('rm -rf hugeNumberOfFiles3');
echo "shell_exec " . (microtime(true) - $start) . " sn\n";
$start = microtime(true);
rrmdir('hugeNumberOfFiles2');
echo "rrmdir [scandir] " . (microtime(true) - $start) . " sn\n";
$start = microtime(true);
rrmdirDI('hugeNumberOfFiles1');
echo "rrmdirDI [DirectoryIterator] " . (microtime(true) - $start) . " sn\n\n";
echo "Toplam süre: " . (microtime(true) - $total_start) . " sn\n";
@unique1984
Copy link
Author

unique1984 commented Sep 3, 2019

Son olarak:

# php
exec 4.9801368713379 sn
shell_exec 6.2770390510559 sn
rrmdir [scandir] 8.6372098922729 sn
rrmdirDI [DirectoryIterator] 11.923294782639 sn

Toplam süre: 31.823668956757 sn
# Konsol
start=$(date +"%s.%N"); rm -rf hugeNumberOfFiles; end=$(date +"%s.%N");
echo "$end - $start" | bc
7.607972800 sn

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment