could use some suggestions to speed this short piece of code up
could use some suggestions to speed this short piece of code up
am 20.01.2010 12:42:09 von Rene Veerman
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.'
';
}
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.'
'.$maxLength.'
';
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
Re: could use some suggestions to speed this short piece ofcode up
am 20.01.2010 15:33:28 von TedD
At 12:42 PM +0100 1/20/10, Rene Veerman wrote:
>Hi, for http://mediabeez.ws/htmlMicroscope/ (lgpl) i need to make
>large & complex but _randomly filled_ test-arrays.
Sometimes it's good to look at some of php's built in functions, like
shuffle() and array_rand().
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: could use some suggestions to speed this short piece of
am 20.01.2010 18:46:37 von Rene Veerman
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.'
'.$maxLength.'
';
return $r;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: could use some suggestions to speed this short piece of
am 21.01.2010 12:37:47 von Richard Quadling
2010/1/20 Rene Veerman :
> this is at least 1000% faster
>
> the crux is that array()+=3Darray() is way faster than any array_merge()
> operations.
>
> global $hmGenKeys;
> $hmGenKeys =3D 0;
> function htmlMicroscope_generateRandomArray ($maxKeys, $maxDepth,
> $maxDuration=3D-1) {
> Â global $hmGenKeys;
> Â if ($maxKeys!==null) {
> Â Â $hmGenKeys =3D $maxKeys;
> Â }
>
> Â $hmGenKeys--;
> Â if ($hmGenKeys<=3D0) return false;
> Â if ($maxDepth==0) return false;
> Â srand(htmlMicroscope_randMakeSeed());
> Â while ($hmGenKeys > 0) {
> Â Â Â $range =3D rand(0,40);
> Â Â Â file_put_contents('countdown.txt', $hmGenKeys);
> Â Â Â for ($i=3D0; $i<$range && $hmGenKeys>=3D0; $i++) {
> Â Â Â Â $hmGenKeys--;
> Â Â Â Â if ($maxDuration!=3D-1 && $maxDuration < getDu=
ration()) {
> Â Â Â Â Â return false;
> Â Â Â Â } else {
> Â Â Â Â Â Â $r =3D array();
> Â Â Â Â Â Â switch (rand(1,2)) {
> Â Â Â Â Â Â Â case 1 :
> Â Â Â Â Â Â Â Â $next =3D htmlMicr=
oscope_generateRandomArray (null,
> $maxDepth-1, $maxDuration);
> Â Â Â Â Â Â Â Â if ($next!==fa=
lse)
> Â Â Â Â Â Â Â Â Â $r +=3D =C2=
=A0array(
> Â Â Â Â Â Â Â Â Â Â html=
Microscope_randomValue(3) =3D> $next
> Â Â Â Â Â Â Â Â Â );
> Â Â Â Â Â Â Â Â break;
> Â Â Â Â Â Â Â case 2 :
> Â Â Â Â Â Â Â Â $r +=3D array(
> Â Â Â Â Â Â Â Â Â htmlMicrosc=
ope_randomValue(3) =3D> htmlMicroscope_randomValue(20)
> Â Â Â Â Â Â Â Â );
> Â Â Â Â Â Â Â Â break;
> Â Â Â Â Â Â }
> Â Â Â Â }
> Â Â Â }
> Â }
> Â return $r;
> }
>
> function htmlMicroscope_randomValue($maxLength) {
> Â $r =3D '';
> Â switch (rand(0,9)) {
> Â Â case 0 : $r =3D rand (0,100000000); break;
> Â Â case 1 : $r =3D rand (0,100000000) / rand(1,100) / 3; break;
> Â Â default:
> Â Â Â Â switch (rand(0,1)) {
> Â Â Â Â Â case 0:
> Â Â Â Â Â Â for ($i =3D 0; $i < $maxLength; =
$i++) {
> Â Â Â Â Â Â Â $r.=3D unichr(rand(0,hexd=
ec('ffff')));
> Â Â Â Â Â Â }
> Â Â Â Â Â Â break;
> Â Â Â Â Â case 1:
> Â Â Â Â Â Â for ($i =3D 0; $i < $maxLength; =
$i++) {
> Â Â Â Â Â Â Â $r.=3Dchr(rand(ord('a'),o=
rd('z')));;
> Â Â Â Â Â Â }
> Â Â Â Â Â Â break;
> Â Â Â Â }
> Â Â Â Â break;
> Â }
> Â //echo $r.'
'.$maxLength.'
';
> Â 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
--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php