Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Function to generate a random number using the first method
- function unique_keygen_meth_looping() {
- $member_id = '';
- for ($i = 0; $i < 10; $i++) {
- $member_id .= mt_rand(0, 9);
- }
- return '10000' . $member_id;
- }
- // Function to generate a random number using the second method
- function unique_keygen_meth_padding() {
- $member_id = '10000' . str_pad(mt_rand(0, 9999999999), 10, '0', STR_PAD_LEFT);
- return $member_id;
- }
- // Measure execution time for the first method
- $startTime1 = microtime(true);
- for ($i = 0; $i < 100000; $i++) {
- unique_keygen_meth_looping();
- }
- $endTime1 = microtime(true);
- $executionTime1 = $endTime1 - $startTime1;
- // Measure execution time for the second method
- $startTime2 = microtime(true);
- for ($i = 0; $i < 100000; $i++) {
- unique_keygen_meth_padding();
- }
- $endTime2 = microtime(true);
- $executionTime2 = $endTime2 - $startTime2;
- // Output the results
- echo "Execution time for Method 1 (Looping): " . $executionTime1 . " seconds\n";
- echo "Execution time for Method 2 (Padding): " . $executionTime2 . " seconds\n";
- // Compare the execution times and determine which method is faster
- if ($executionTime1 < $executionTime2) {
- echo "Looping is faster than Padding\n";
- } elseif ($executionTime1 > $executionTime2) {
- echo "Padding is faster than Looping\n";
- } else {
- echo "Both methods have the same execution time\n";
- }
- // Compare the execution times and determine which method is faster and by what percentage
- if ($executionTime1 < $executionTime2) {
- $percentageFaster = (int) ((($executionTime2 - $executionTime1) / $executionTime2) * 100);
- echo "Looping is faster than Padding by " . $percentageFaster . "%\n";
- } elseif ($executionTime1 > $executionTime2) {
- $percentageFaster = (int) ((($executionTime1 - $executionTime2) / $executionTime1) * 100);
- echo "Padding is faster than Looping by " . $percentageFaster . "%\n";
- } else {
- echo "Both methods have the same execution time\n";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement