Re: could use some suggestions to speed this short piece of code up
| From: | Richard Quadling | Date: | Thu, 21 Jan 2010 11:37:47 +0000 |
| Subject: | Re: could use some suggestions to speed this short piece of code up | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301307@lists.php.net to get a copy of this message | ||
2010/1/20 Rene Veerman <rene7705@gmail.com>:
> this is at least 1000% faster
>
> the crux is that array()+=array() is way faster than any array_merge()
> operations.
>
> global $hmGenKeys;
> $hmGenKeys = 0;
> function htmlMicroscope_generateRandomArray ($maxKeys, $maxDepth,
> $maxDuration=-1) {
> global $hmGenKeys;
> 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()) {
> return false;
> } else {
> $r = array();
> switch (rand(1,2)) {
> case 1 :
> $next = htmlMicroscope_generateRandomArray (null,
> $maxDepth-1, $maxDuration);
> if ($next!==false)
> $r += array(
> htmlMicroscope_randomValue(3) => $next
> );
> break;
> case 2 :
> $r += array(
> htmlMicroscope_randomValue(3) => htmlMicroscope_randomValue(20)
> );
> break;
> }
> }
> }
> }
> 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:
> switch (rand(0,1)) {
> case 0:
> for ($i = 0; $i < $maxLength; $i++) {
> $r.= unichr(rand(0,hexdec('ffff')));
> }
> break;
> case 1:
> for ($i = 0; $i < $maxLength; $i++) {
> $r.=chr(rand(ord('a'),ord('z')));;
> }
> break;
> }
> break;
> }
> //echo $r.'<br/>'.$maxLength.'<br/>';
> return $r;
> }
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Also take a watch of the php|Architect webcast Jun 26 Matthew
Turland New SPL Features in PHP 5.3.
This covered the ArrayObject and ArrayAccess class/interface. When
dealing with a LOT of data, this interface outperforms standard PHP
array.
http://www.phparch.com/webcasts
http://mtadata.s3.amazonaws.com/webcasts/20090623-spl.wmv
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling