解決できない問題があります。小さな画像ファイルをコピーするスクリプトがあります。問題は、1 つの画像バリアントで約 1.5 秒かかることです。早く取れるものってありますか?PHP CLI を使用しており、HDD は WD VelociRaptor 10K RPM です。ソース フォルダには約 200K のファイルが含まれています
これが私がより速くしたいコードの部分です:
$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);
ありがとう。
編集: getCropsizeFileName は次のとおりです。
private function getCropSizeFileName($srcFileName, $size) {
global $sourceCropBasePath;
$ext = pathinfo($srcFileName, PATHINFO_EXTENSION);
$destFileNamePrefix = basename($srcFileName, ".".$ext);
return $sourceCropBasePath.$destFileNamePrefix."_".$size.".".$ext;
}
各コピー行のタイマーの結果は次のとおりです。
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