tom91136
tom91136

Reputation: 8972

PHP slow on string joining?

it seems very slow for php to process mass amounts of strings, is there anyway i can improve the speed of it? the code i was trying to write will make a image into a string of RGB values for later use it would be something like this

$string = "255:255:253#12:12:23#33:34:24"/*an output of a $SIZE = 3 image*/

the problems is that when $SIZE is big like 256, it would take up to 1 second to produce the string

$r = "";
$g = "";
$b = "";

for($y = 0; $y <= $SIZE-1; $y++){
    for($x = 0; $x <= $SIZE-1; $x++){


        {$r .= $arr2[$y][$x]["R"].":";}

        {$g .= $arr2[$y][$x]["G"].":";}

        {$b .= $arr2[$y][$x]["B"].":";}

    }
}
$r = rtrim($r, ":");
$g = rtrim($g, ":");
$b = rtrim($b, ":");
$str_a .= $r."#".$g."#".$b;

Upvotes: 1

Views: 260

Answers (2)

jsalonen
jsalonen

Reputation: 30531

Based on your given code, we can reverse-engineer the structure of $arr2 to (assuming R, G and B are integer from 0 to 255):

$arr2 = array(
   0 => array(
      0 => array(
        "R" => 128,
        "G" => 64,
        "B" => 255
      ),
      1 => array(
        ...
      ) 
   )
);

Given that your $SIZE is set to 256, you will have a total of 256*256=65536 arrays further containing arrays with key-values for R, G and B, resulting in total of 256*256*3=196608 integers in 3 levels of hierarchy. No surprise your code is slow!

I think the best strategy here is to try to reduce the total number of items in your array.

Given that instead of encoding single cells as "R, G, B" triples, you could encode all values in a single integer. Such as instead of:

0 => array( "R" => $r, "G" => $g, "B" => $b )

Given that 0<=r,g,b<=255, you could encode $arr2 as:

0 => ($r<<16 + $g<<8 + $b);

Now of course you need to unpack the color value inside your loop as well. This can be achieved by:

$col = $arr2[$y][$x];
$col_b = ($col&255);
$col_g = ($col>>8)&255;
$col_r = ($col>>16)&255;
$r .= $col_r.":";
$g .= $col_g.":";
$b .= $col_b.":";

This modification alone would cut one level of hierarchy from your array completely.

While running your original code with $SIZE=256, my average execution speed in my settings was 0.30 secs. With the given refactoring, I was able to reduce this to 0.10 secs cutting your calculation time to 1/3 of the original.

You will still have a lot of work to do if you wish to improve the performance, but I hope this gives you an idea on how you could proceed.

Upvotes: 4

GordonM
GordonM

Reputation: 31760

The first thing to bear in mind is that you're doing a really large number of iterations. If your $SIZE var is 256, then you're actually doing 256 X 256 (65536) iterations. Your biggest hope of speeding you loop up is finding a way of doing what you need to achieve in fewer loops.

You could try using an array to build up the string you want to output, then implode() it when you're done. However, this would be a micro-optimization, and the speed gains you'd get from it would probably not be worth the effort. I'd suggest building a simple test loop you can benchmark to compare string concatenation versus array building and imploding.

Upvotes: 6

Related Questions