Hi, for http://mediabeez.ws/htmlMicroscope/ (lgpl) i need to make
large & complex but _randomly filled_ test-arrays.

The code i wrote (hastily) for this is taking over 2 hours to generate
65k array values.
I wonder if any of you see a way to improve it's speed.

global $hmGenKeys;
$hmGenKeys = 0;
function htmlMicroscope_generateRandomArray ($maxKeys, $maxDepth,
$maxDuration=-1) {
  global $hmGenKeys;
  $r = array();
  if ($maxKeys!==null) {
    $hmGenKeys = $maxKeys;
  }

      $hmGenKeys--;
  if ($hmGenKeys<=0) return false;
  if ($maxDepth==0) return false;
  srand(htmlMicroscope_randMakeSeed());
  while ($hmGenKeys > 0) {
      $range = rand(0,40);
      file_put_contents('countdown.txt', $hmGenKeys);
      for ($i=0; $i<$range && $hmGenKeys>=0; $i++) {
      $hmGenKeys--;
        if ($maxDuration!=-1 && $maxDuration < getDuration()) {
          $r = array();
        } else {
            switch (rand(1,2)) {
              case 1 :
                $next = htmlMicroscope_generateRandomArray (null,
$maxDepth-1, $maxDuration);
                if ($next!==false)
                  $r = array_merge ($r, array(
                    htmlMicroscope_randomValue(3) => $next
                  ));
                break;
              case 2 :
                $r = array_merge ($r, array(
                  htmlMicroscope_randomValue(3) => 
htmlMicroscope_randomValue(20)
                ));
                break;
            }
        }
      }
 // echo $hmGenKeys.'<br/>';
  }
  return $r;
}

function htmlMicroscope_randomValue($maxLength) {
  $r = '';
  switch (rand(0,9)) {
    case 0 : $r = rand (0,100000000); break;
    case 1 : $r = rand (0,100000000) / rand(1,100) / 3; break;
    default:
      for ($i = 0; $i < $maxLength; $i++) {
        switch (rand(0,1)) {
          case 0:
            $r.= unichr(rand(0,hexdec('ffff')));
            break;
          case 1:
            $r.=chr(rand(ord('a'),ord('z')));;
            break;
        }
      }
      break;
  }
  //echo $r.'<br/>'.$maxLength.'<br/>';
  return $r;
}

function unichr($u) {
    return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8',
'HTML-ENTITIES');
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to