A new PHP Challenge: Facing Memory Issues

Let’s say that we have a PC with unlimited processing power, BUT with very little RAM. We need to optimize its memory, by using a script that creates 2 functions: one for archiving an array and another one to search through this array.

There are also a set of rules that you need to respect:
1: You will use only Php Coding
2: You can modify only these 2 functions
3: You will not use apps like: Memcached, apc, files, alt thread…
4: Don’t cheat.
5: You can reply to this article with all new records that you achieve.

You are given the following:

function getMem() {

$mem = memory_get_usage(true) / 1024;
return $mem;
}
$array = range(1,1000000);
shuffle($array);
$mem = getMem();
$string = compress($array);
echo 'You used : ',getMem() - $mem,' Kb',PHP_EOL;
function compress($array){
/** TODO create a compress function**/
return implode(',',$array);
}


function get($nr,$string) {
/** TODO create get function**/
$x = explode(',',$string);
return $x[$nr];
}



for($i = 0;$i <100; $i++) {
$test = rand(0,1000000-1);
if($array[$test] != get($test,$string)) {
try {
throw New Exception('You failed!!!');
} catch(Exception $e) {
echo $e->getMessage();die;
}
}
}
echo 'You Win',PHP_EOL;
echo date('Y-m-d H:i:s'),PHP_EOL;

 

So… What’s your best memory consumption result?

This test was proposed by Alin, Team Leader @ INNOBYTE.

8 thoughts on “A new PHP Challenge: Facing Memory Issues”

  1. Interesting problem but:
    – since you didn’t published the source code of the solution, what’s the peak memory consumption delta for the compression and decompression part?
    – when are you going to publish the source code of your solution?

    Kind regards,
    Otto

    1. andreea.paraschiv

      Sure thing, Ray!

      Our winning code was:
      $each) {
      $r = ($each >> 16) & 0xFF;
      $g = ($each >> 8) & 0xFF;
      $b = $each & 0xFF;
      $color = imagecolorallocate($gd, $r, $g ,$b);
      imagesetpixel($gd, floor($key / 1000) + 1, $key % 1000, $color);
      }
      echo ‘You used After Image : ‘,getMem() – $mem,’ Kb’,PHP_EOL;
      return $gd;//return $array;
      }

      function get($nr,$string) {
      global $mem;

      $x = floor($nr / 1000) + 1;
      $y = $nr % 1000;
      $rgb = imagecolorat($string, $x, $y);
      echo ‘You used decom : ‘,getMem() – $mem,’ Kb’,PHP_EOL;
      return $rgb;

      }

      for($i = 0;$i getMessage();die;
      }
      }
      }
      echo ‘You Win’,PHP_EOL;
      echo date(‘Y-m-d H:i:s’),PHP_EOL;

      Also, use this code to show that you used 0 kb:

      function compress($array){
      /** TODO create a compress function**/
      class Archiver
      {
      protected $meh=array();
      public function __construct($array)
      {
      $this->meh = $array;
      }
      public function get($test)
      {
      return $this->meh[$test];
      }
      }
      return new Archiver($array);
      }

      function get($nr,$archiver) {
      /** TODO create get function**/
      return $archiver->get($nr);

Leave a Comment

Your email address will not be published.

Scroll to Top