Jump to content
php.lv forumi

random_string()


daGrevis

Recommended Posts

Es nezkāpēc biju iedomājies, bet darbības ar masīviem ir lēnākas, bet reku šeku

function rand1(){
$chars = implode(array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')));
$l = strlen($chars)-1;
$r = '';
for($i = 0; $i < 5; $i++) $r.= $chars[mt_rand(0, $l)];
return $r;
}

function rand2(){ /* WINNER */
$chars = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
shuffle($chars);
return implode(array_slice($chars, 0, 5));
}

$t1 = microtime(true);
for($i = 0; $i <= 100000; $i++) rand1();
$t2 = microtime(true);
for($i = 0; $i <= 100000; $i++) rand2();
$t3 = microtime(true);

printf("rand1: %.5f\nrand2: %.5f", $t2-$t1, $t3-$t2);

/*
1st try ->
rand1: 0.44617
rand2: 0.29068

2nd try ->
rand1: 0.46181
rand2: 0.29596

3rd try ->
rand1: 0.46633
rand2: 0.30778
*/

Link to comment
Share on other sites

Mārci, Man Sava funkcija gan rādās, pēc Mana mērījuma, apmēram trīs reizes ātrāka par Tavu "WINNER"... O.o

 

P.S. Mans šķībais "benchmarks" - http://paste.php.lv/e212bf3a6af220ea3de0c8bf8f6bcba2?lang=php ... xD

Link to comment
Share on other sites

Mārci, stringi savā dziļā būtībā arī ir masīvs (kas ir kodējis C/C++, sapratīs). Tā, ka principā rezultāts ir paredzams.

 

Vispār, ja mēs dzenamies pēc performances, tad īstā vieta, kur izmantot chr() funkciju (optimālāk diez vai būs).

function rand1(){
   $chars = implode(array_merge(range(0, 9), range('a', 'z'), range('A', 'Z')));
   $l = strlen($chars)-1;
   $r = '';
   for($i = 0; $i < 5; $i++) $r.= $chars[mt_rand(0, $l)];
   return $r;
}

function rand2(){ 
   $chars = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
   shuffle($chars);
   return implode(array_slice($chars, 0, 5));
}

function randString($length, $str = ''){
   $rand = mt_rand(0, 61);
   return $length==0?$str:$str.randString($length-1, chr($rand>35?$rand+0x3D:($rand>9?$rand+0x37:$rand+0x30)));
}

$t1 = microtime(true);
for($i = 0; $i <= 100000; $i++) rand1();
$t2 = microtime(true);
for($i = 0; $i <= 100000; $i++) rand2();
$t3 = microtime(true);
for($i = 0; $i <= 100000; $i++) randString(5);
$t4 = microtime(true);

printf("rand1: %.5f\nrand2: %.5f\nrandString: %.5f", $t2-$t1, $t3-$t2, $t4-$t3);
/*
1st:
rand1: 3.91979 
rand2: 3.53275 
randString: 1.91287

2nd:
rand1: 3.97741 
rand2: 3.50666 
randString: 1.92325

3rd:
rand1: 4.24653 
rand2: 3.50567 
randString: 1.89667
*/

 

EDIT: Ja aizvākt perversiju ar rekursiju un ielikt while, tad būs vēl ātrāk.

Edited by Леший
Link to comment
Share on other sites

Nu nezinu, šinī gadījumā nepatīk perversijas. xD

 

Galu galā kods strādā Mums visiem. Man galvenais jau ir laba lasāmība, jo daru to mācību nolūkos un galvenais, lai pa solīšiem saprotu kas un kā. =) To esmu panācis ar Savu funckiju.

 

Paldies visiem! =)

 

Varam diskusiju "maukt ciet"! xD

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...