fork download
  1. <?php
  2.  
  3. $str = "23456789ABCDEGHJKMNPQRSTUVXYZabcdeghjkmnpqrstuvxyz";
  4. $excludes = ['cp','cb','ck','c6','c9','rn','rm','mm','co','do','cl','db','qp','qb','dp','ww'];
  5.  
  6. $indexedExcludes = array_combine($excludes, $excludes);
  7. $dictionaryArray = str_split($str);
  8.  
  9. function fastCaptcha(array $dictionary, array $excludedSyllables, int $length): string
  10. {
  11. $captcha = '';
  12. $generatedLength = 0;
  13.  
  14. while ($generatedLength < $length) {
  15. $randomCharacter = $dictionary[array_rand($dictionary)];
  16. $captcha .= $randomCharacter;
  17. $generatedLength++;
  18. }
  19.  
  20. return $captcha;
  21. }
  22.  
  23. $excludesRegex = '/' . implode('|', $excludes) . '/';
  24.  
  25. $shortCaptchas = [];
  26. $mediumCaptchas = [];
  27. $longCaptchas = [];
  28. for ($i = 0; $i < 100000; $i++)
  29. $shortCaptchas[] = fastCaptcha($dictionaryArray, $indexedExcludes, 2);
  30. $mediumCaptchas[] = fastCaptcha($dictionaryArray, $indexedExcludes, 5);
  31. $longCaptchas[] = fastCaptcha($dictionaryArray, $indexedExcludes, 10);
  32.  
  33.  
  34. $t0 = microtime(true);
  35.  
  36. foreach($shortCaptchas as $captcha) {
  37. preg_match($excludesRegex, $captcha);
  38. }
  39.  
  40. $t1 = microtime(true);
  41.  
  42. foreach($longCaptchas as $captcha) {
  43. preg_match($excludesRegex, $captcha);
  44. }
  45.  
  46. $t2 = microtime(true);
  47.  
  48. foreach($mediumCaptchas as $captcha) {
  49. preg_match($excludesRegex, $captcha);
  50. }
  51.  
  52. $t3 = microtime(true);
  53.  
  54. echo "short: " . ($t1 - $t0) . "\n";
  55. echo "long: " . ($t2 - $t1) . "\n";
  56. echo "medium:" . ($t3 - $t2) . "\n";
Success #stdin #stdout 0.06s 34108KB
stdin
Standard input is empty
stdout
    short: 0.007483959197998
long:  9.5367431640625E-7
medium:0