This code executes in 0.8seconds and takes up 22Mb of memory on my machine.
$x=500;
$y=500;
$im = imagecreatetruecolor($x,$y);
$ia=array();
for ($i = 0; $i < $x; $i++) {
for ($j = 0; $j < $y; $j++) {
$r=rand(0,96);
$g=rand(0,128);
$b=rand(0,255);
$ia[$i][$j]=ImageColorAllocate($im,$r,$g,$b);
}
}
What can be done to speed it up, but more importantly lower the memory footprint it eats at any given time.
Try the following code (from http://php.net/manual/en/function.imagecolorallocate.php) instead of your direct colour assignment:
Also, try making the function call in 1 line:
Fiddle with the hardcoded value 1000, and see how it changes memory usage.