Advertisement
Guest User

Looping vs Padding

a guest
Feb 21st, 2025
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.00 KB | Source Code | 0 0
  1. <?php
  2.  
  3. // Function to generate a random number using the first method
  4. function unique_keygen_meth_looping() {
  5.     $member_id = '';
  6.     for ($i = 0; $i < 10; $i++) {
  7.         $member_id .= mt_rand(0, 9);
  8.     }
  9.     return '10000' . $member_id;
  10. }
  11.  
  12. // Function to generate a random number using the second method
  13. function unique_keygen_meth_padding() {
  14.     $member_id = '10000' . str_pad(mt_rand(0, 9999999999), 10, '0', STR_PAD_LEFT);
  15.     return $member_id;
  16. }
  17.  
  18. // Measure execution time for the first method
  19. $startTime1 = microtime(true);
  20. for ($i = 0; $i < 100000; $i++) {
  21.     unique_keygen_meth_looping();
  22. }
  23. $endTime1 = microtime(true);
  24. $executionTime1 = $endTime1 - $startTime1;
  25.  
  26. // Measure execution time for the second method
  27. $startTime2 = microtime(true);
  28. for ($i = 0; $i < 100000; $i++) {
  29.     unique_keygen_meth_padding();
  30. }
  31. $endTime2 = microtime(true);
  32. $executionTime2 = $endTime2 - $startTime2;
  33.  
  34. // Output the results
  35. echo "Execution time for Method 1 (Looping): " . $executionTime1 . " seconds\n";
  36. echo "Execution time for Method 2 (Padding): " . $executionTime2 . " seconds\n";
  37.  
  38. // Compare the execution times and determine which method is faster
  39. if ($executionTime1 < $executionTime2) {
  40.     echo "Looping is faster than Padding\n";
  41. } elseif ($executionTime1 > $executionTime2) {
  42.     echo "Padding is faster than Looping\n";
  43. } else {
  44.     echo "Both methods have the same execution time\n";
  45. }
  46.  
  47. // Compare the execution times and determine which method is faster and by what percentage
  48. if ($executionTime1 < $executionTime2) {
  49.     $percentageFaster = (int) ((($executionTime2 - $executionTime1) / $executionTime2) * 100);
  50.     echo "Looping is faster than Padding by " . $percentageFaster . "%\n";
  51. } elseif ($executionTime1 > $executionTime2) {
  52.     $percentageFaster = (int) ((($executionTime1 - $executionTime2) / $executionTime1) * 100);
  53.     echo "Padding is faster than Looping by " . $percentageFaster . "%\n";
  54. } else {
  55.     echo "Both methods have the same execution time\n";
  56. }
  57.  
  58. ?>
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement