Saturday, 10 August 2013

PHP slow copy of files

PHP slow copy of files

I have an issue that i can't resolve. I have a script which copy small
image files. The problem is that for one image variant it gets about 1.5
seconds. Is there anything that can get it faster? I'm using PHP CLI and
my HDD is WD VelociRaptor 10K RPM. The source folder contains about 200K
files
Here is the part of code i want to get faster:
$startCopyVariant = time();
$result = array('uploadedImagesUrls'=>array(), 'errMsg'=>'');
// lazyload class instances
$productOptionImages = &lloader()->getImagesByName("productOption");
// validate image
$imgSizeInfo = @getimagesize($srcImageInfo['tmp_name']);
if (empty($imgSizeInfo)) { $result['errMsg'] = 'Invalid image
'.$srcImageInfo['name'].', type '.$srcImageInfo['type']; return
$result; }
$ext = pathinfo($srcImageInfo['name'], PATHINFO_EXTENSION);
$variantFileName = 'opt_'.$optionId."_variant.".$ext;
$mainDestFileName = $variantFileName;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'],
"big");
copy($srcFileName,
$productOptionImages->getImagePath().$variantFileName);
$variantFileName = $variantFileName =
'opt_'.$optionId."_variant_sma.".$ext;;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'],
"small");
copy($srcFileName,
$productOptionImages->getImagePath().$variantFileName);
$variantFileName = 'opt_'.$optionId."_variant_thu.".$ext;;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'],
"thumbnail");
copy($srcFileName,
$productOptionImages->getImagePath().$variantFileName);
$variantFileName = 'opt_'.$optionId."_variant_tin.".$ext;;
$srcFileName = $this->getCropSizeFileName($srcImageInfo['name'],
"tiny");
copy($srcFileName,
$productOptionImages->getImagePath().$variantFileName);
$endCopyVariant = time();
$elapsedTime = $endCopyVariant - $startCopyVariant;
print_r("Variant copy time: (".$srcImageInfo['name']."):
".sprintf('%02d:%02d:%02d',
($elapsedTime/3600),($elapsedTime/60%60), $elapsedTime%60), 0);
Thanks.
EDIT: Here is the getCropsizeFileName does:
private function getCropSizeFileName($srcFileName, $size) {
global $sourceCropBasePath;
$ext = pathinfo($srcFileName, PATHINFO_EXTENSION);
$destFileNamePrefix = basename($srcFileName, ".".$ext);
return $sourceCropBasePath.$destFileNamePrefix."_".$size.".".$ext;
}
The result of timers of each copy line are:
Variant copy time1: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:02
Variant copy time2: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:01
Variant copy time3: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:02
Variant copy time4: (0a46de43f73304469a38137bf3f43c32.jpg): 00:00:01

No comments:

Post a Comment