1

I'm looking into what is slowing some scripts of mine down. I have got the following scripts, essentially the same, the only difference is one is in a subfolder and uses $_SERVER['DOCUMENT_ROOT'] to get the image url.

Script 1 (C:/xampp/htdocs/ppa/test.php):

<?php
$time_start = microtime(true); 

ini_set("memory_limit", -1);
$im = imagecreatefromjpeg("data/images/20140310/4/test.jpg");

$ratio = 500 / imagesx($im);
$width = 500;
$height = imagesy($im) * $ratio;

//Create thumbnail container
$thumb = imagecreatetruecolor($width,$height);
imagecopyresampled($thumb,$im,0,0,0,0,$width,$height,imagesx($im),imagesy($im));    

$time_end = microtime(true);
$execution_time = "Took ".($time_end - $time_start)/60 ." to generate image";

$white = imagecolorallocate($im,255,255,255);
$font = "images/fonts/Calibri Bold.ttf";

imagettftext($thumb,15,0,20,20,$white,$font,$execution_time);

header("Content-Type: image/jpeg");
imagejpeg($thumb,"thumb.jpg",100);

imagedestroy($im);
?>

Here is the image it produces with the time it took on it: Root Gen

Script 2 (C:/xampp/htdocs/ppa/benchmarking/generate_thumb.php):

<?php
$time_start = microtime(true); 

ini_set("memory_limit", -1);
$im = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT']."/ppa/data/images/20140310/4/test.jpg");

$ratio = 500 / imagesx($im);
$width = 500;
$height = imagesy($im) * $ratio;

//Create thumbnail container
$thumb = imagecreatetruecolor($width,$height);
imagecopyresampled($thumb,$im,0,0,0,0,$width,$height,imagesx($im),imagesy($im));    

$time_end = microtime(true);
$execution_time = "Took ".($time_end - $time_start)/60 ." to generate image";

$white = imagecolorallocate($im,255,255,255);
$font = $_SERVER['DOCUMENT_ROOT']."/ppa/images/fonts/Calibri Bold.ttf";

imagettftext($thumb,15,0,20,20,$white,$font,$execution_time);

header("Content-Type: image/jpeg");
imagejpeg($thumb,"thumb.jpg",100);

imagedestroy($im);
?>

Image it produces:

enter image description here

The times stay similar every time, why is it quicker in the sub directory!? Downsizing from a 1.15MB image (4512x3000)

Community
  • 1
  • 1
Martyn Ball
  • 4,679
  • 8
  • 56
  • 126
  • Because PHP takes a few micro seconds to access and concatenate the var onto the string before passing to the function, that's all I can think of.. – Lawrence Cherone Mar 13 '14 at 17:50
  • Hmm yes must be, the times are averaging around 0.0091 now, both very similar! – Martyn Ball Mar 13 '14 at 17:57
  • Well its goto be that part because both the codes are the same apart from that line. Change all the `"` quotes to `'` and your see a further increase. Every little helps ;p – Lawrence Cherone Mar 13 '14 at 18:01
  • Ive never understood the difference between ' and ", what is it @LozCherone? – Martyn Ball Mar 13 '14 at 18:03
  • 1
    Well if you use `"` php will look for vars between them EG `"yada,$var"` even if there is none, so if you only use `'` for strings it wont try and parse them hence the tiny savings. Though really unnoticeable unless your testings, but its all good practice to make yourself a better coder I suppose. – Lawrence Cherone Mar 13 '14 at 18:04
  • According to your timings: you can process 78 images a second in the worst case and 105 images a second in the best case. I suggest that the user will not notice the difference. – Ryan Vincent Mar 13 '14 at 20:12
  • That's perfect, although for some reason when this runs on my mates laptop (server run locally) it is really slow, takes about 1 - 2 minutes to process 4 images... 4GB Memory.. any ideas @RyanVincent? – Martyn Ball Mar 14 '14 at 17:45
  • How much memory is allocated to PHP when it runs? see php.ini – Ryan Vincent Mar 14 '14 at 18:51
  • @RyanVincent, I use ini_set and over-ride it to "-1" which allocates all available memory to the script. – Martyn Ball Mar 17 '14 at 14:20

0 Answers0