Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:30:32+00:00 2026-05-27T02:30:32+00:00

it seems very slow for php to process mass amounts of strings, is there

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:30:33+00:00Added an answer on May 27, 2026 at 2:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code which seems very slow and I wanted to see
The simplest script: <?php echo 'hello'; Takes about 3 seconds to execute. There seems
Is there any logical reason why a stored procedure would run very slow (>60
I have a Stored Procedure that seems to be very slow. Executing it in
Nant seems very compiler-centric - which is guess is because it's considered a .NET
This seems very noisy to me. Five lines of overhead is just too much.
This seems very basic and I must be missing something, but here goes anyways...
ASP.NET tracing seems very erratic. Sometimes it traces and sometimes it doesn't. I trace
The problem seems very strange. I have a AJAX helper function within a same
The installation for clearance seems very straight forward ( https://github.com/thoughtbot/clearance ). I'm following in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.