PHP的字符串连接缓慢? [英] PHP slow on string joining?

查看:197
本文介绍了PHP的字符串连接缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来很慢的PHP处理大规模量字符串,反正我有可以提高它的速度是多少?
在code我试图写将使图像分成RGB值供以后使用字符串
它会是这样的。

  $字符串=255:255:253#12:12:23#33:34:24/ * $一个SIZE = 3 *图像的输出/

问题是,当$ SIZE大像256,它会需要1秒钟产生字符串

  $ R =;
$ G =;
$ B =;为($ Y = 0; $ Y< = $ SIZE-1; $ Y ++){
    为($ 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。;


解决方案

根据你定的code,我们可以反向工程的$ ARR2的结构(假设R,G和B是整数,从0到255 ):

  $ ARR2 =阵列(
   0 =>阵列(
      0 =>阵列(
        R=> 128,
        G=> 64,
        的B=> 255
      )
      1 =>阵列(
        ...
      )
   )
);

由于你的 $ SIZE 设置为 256 ,你将有一个总的 256 * 256 = 65536 阵列还含有与键值数组为研究 B ,导致合计 256 * 256 * 3 = 196608整数 3个级别层次。难怪你的code是慢!

我觉得这里最好的策略就是尽量减少你的数组中的项目总数。

鉴于代替编码单细胞作为R,G,B三元组,则可以连接code中一个整数的所有值。例如,而不是:

  0 =>阵列(R=> $ R,G=> $克,B=> $ B)

由于 0℃; = R,G,B< = 255 ,你可以带code $ ARR2 为:

  0 => ($ R<< 16 + $ G<< 8 + $ B);

现在当然你需要解压的循环内的颜色值,以及。这可以通过以下方式实现:

  $山口= $ 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:。

仅此修改将削减层次一个层次,从阵列完全。

在运行你原来的code $与SIZE = 256,在我的设置我的平均运行速度为0.30秒。 在给定的重构,我能够将这一数字减少到0.10秒削减你的计算时间到原来的1/3。

您仍然有大量的工作,如果你想提高性能做的,但我希望这给你的,你怎么可能进行的想法。

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;

解决方案

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.

这篇关于PHP的字符串连接缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆