';
$file = 'file.txt';
$iterations = array(1, 10, 100, 1000);
foreach ($iterations as $iteration) {
// Copy a file using copy().
$time1 = microtime(TRUE);
for ($i = 1; $i <= $iteration; $i++) {
$new_file = 'copy-' . md5(rand(1, 100)) . '.txt';
copy($file, $new_file);
$file = $new_file;
}
$finish1 = microtime(TRUE) - $time1;
echo "$iteration copy operations in $finish1 ms.
";
// Rename a file using rename().
$time2 = microtime(TRUE);
for ($i = 1; $i <= $iteration; $i++) {
$new_file = 'rename-' . md5(rand(1, 100)) . '.txt';
rename($file, $new_file);
$file = $new_file;
}
$finish2 = microtime(TRUE) - $time2;
echo "$iteration rename operations in $finish2 ms.
";
// Remove all redundant files.
$files = scandir('.');
foreach($files as $file) {
if (strpos($file, 'rename-') === 0 || strpos($file, 'copy-') === 0) {
unlink($file);
}
};
$file = 'file.txt';
if ($finish1 > $finish2) {
$diff = ($finish1 * 100) / $finish2;
echo "rename() is $diff times faster than copy().";
}
else {
$diff = ($finish2 * 100) / $finish1;
echo "copy() is $diff times faster than rename().";
}
echo '
';
}
?>