Two random and non-plural php Algorithms

Source: Internet
Author: User
Tags shuffle
First introduce someone else's stopwatch timing class (counttime. class. php): 1. first look at the first example (a minority in a large number): 1? 2*3 @ Description: Stopwatch this type of user get script execution time 4 @ Author: KlestiHoxhaklesti@gmail.com5 * 67 classStopwatch {8 private $ start; 9 private $ en

First introduce someone else's stopwatch timing class (counttime. class. php): 1. first look at the first example (a minority in a large number): 1? 2/* 3 @ Description: Stopwatch this class of users get script execution time 4 @ Author: Klesti Hoxha klesti@gmail.com 5 */6 7 class Stopwatch {8 private $ start; 9 private $ en

First introduce someone else's stopwatch timing class (counttime. class. php ):

1. First, let's look at the first example (a minority in a large number ):

1
 5 */6 7 class Stopwatch {8 private $ start; 9 private $ end; 10 private $ markup_start = array (); 11 private $ markup_end = array (); 12 13 function _ construct ($ markup = false) {14 $ this-> start ($ markup); 15} 16 17 public function start ($ markup = false) {18 if (! $ Markup) {19 $ this-> start = $ this-> getmicrotime (); 20} else {21 $ this-> markup_start [$ markup] = $ this-> getmicrotime (); 22} 23} 24 25 public function stop ($ markup = false) {26 if (! $ Markup) {27 $ this-> end = $ this-> getmicrotime (); 28} else {29 $ this-> markup_end [$ markup] = $ this-> getmicrotime (); 30} 31 return $ this-> getDuration ($ markup ); 32} 33 34 public function getDuration ($ markup = false) {35 if (! $ Markup) 36 {37 return number_format ($ this-> end-$ this-> start, 4 ); 38} else {39 return number_format ($ this-> markup_end [$ markup]-$ this-> markup_start [$ markup], 4 ); 40} 41} 42 43 public function reset ($ markup) {44 if (! $ Markup) {45 $ this-> start = 0; 46 $ this-> end = 0; 47 $ this-> markup_start = array (); 48 $ this-> markup_end = array (); 49} else {50 $ this-> markup_start [$ markup] = 0; 51 $ this-> markup_end [$ markup] = 0; 52} 53} 54 55 private function getmicrotime () {56 list ($ usec, $ sec) = explode ("", microtime (); 57 return (float) $ usec + (float) $ sec); 58} 59 60} 61?>

The two methods for testing are as follows:

1
 2
 Start ("p1"); 7 // range is to convert 1 to 100 columns into an array 8 $ numbers = range ); 9 // shuffle will immediately disrupt the array order by 10 shuffle ($ numbers); 11 // array_slice takes a segment of this array 12 $ no = 30; 13 $ result = array_slice ($ numbers, 0, $ no); 14 for ($ I = 0; $ I <$ no; $ I ++) {15 echo $ result [$ I]."
"; 16} 17 echo" first quiz method ended

"; 18 $ s-> stop (" p1 "); 19 20 $ s-> start (" p2 "); 21 function unique_rand ($ min, $ max, $ num) 22 {23 $ count = 0; 24 $ return = array (); 25 while ($ count <$ num) {26 $ return [] = mt_rand ($ min, $ max); 27 $ return = array_flip ($ return); 28 $ count = count ($ return); 29} 30 shuffle ($ return ); 31 return $ return; 32} 33 34 $ arr = unique_rand (1, 100000000000, 30); 35 $ result = ''; 36 for ($ I = 0; $ I <count ($ arr); $ I ++) 37 {38 $ result = $ arr [$ I]; 39 echo $ result."
"; 40} 41 $ s-> stop (" p2 "); 42 43 44 // output time 45 echo" p 1 Duration :". $ s-> getDuration ("p1 "). "seconds.
"; 46 echo" p 2 Duration: ". $ s-> getDuration (" p2 ")." seconds.
"; 47?>

Result:

1 413537 2 198587 3 60611 4 599544 5 946241 6 462853 7 570943 8 980260 71741610 96932411 29240312 61444313 76946914 15195115 16788316 30872717 34270618 95565919 11231120 32900821 18266822 33060023 92196624 99314325 36922726 74706627 79318528 34787229 43901630 42860031 first end of each test method 32 33 64958972734 45082381335 32471718136 4455376237 37566965738 3712861239 2513532740 46845418541 7339535742 75396000643 21445579744 536761445 115326396146 1780344147 66905719848 5536632449 111734395050 20353048551 116093006452 32648791453 56547123654 706844355 84695500156 83203748957 106781303458 21923959559 106979596460 104989884661 92630126662 19734153963 p 1 Duration: 0.5800 seconds.64 p 2 Duration: 0.0000 seconds.

Conclusion: The second method is faster for retrieving a small amount of data from a large amount of data.

2. Let's take a large number from the large number:

1
 2
 Start ("p1"); 7 // range is to convert 1 to 100 columns into an array 8 $ numbers = range ); 9 // shuffle will immediately disrupt the array order by 10 shuffle ($ numbers); 11 // array_slice takes a segment of this array 12 $ no = 10000; 13 $ result = array_slice ($ numbers, 0, $ no); 14 for ($ I = 0; $ I <$ no; $ I ++) {15 echo $ result [$ I]."
"; 16} 17 echo" first quiz method ended

"; 18 $ s-> stop (" p1 "); 19 20 $ s-> start (" p2 "); 21 function unique_rand ($ min, $ max, $ num) 22 {23 $ count = 0; 24 $ return = array (); 25 while ($ count <$ num) {26 $ return [] = mt_rand ($ min, $ max); 27 $ return = array_flip ($ return); 28 $ count = count ($ return); 29} 30 shuffle ($ return ); 31 return $ return; 32} 33 34 $ arr = unique_rand (1, 1000000,100 00); 35 $ result = ''; 36 for ($ I = 0; $ I <count ($ arr); $ I ++) 37 {38 $ result = $ arr [$ I]; 39 echo $ result."
"; 40} 41 $ s-> stop (" p2 "); 42 43 44 // output time 45 echo" p 1 Duration :". $ s-> getDuration ("p1 "). "seconds.
"; 46 echo" p 2 Duration: ". $ s-> getDuration (" p2 ")." seconds.
"; 47?>

Output result:

1. Omit .................. 2 p 1 Duration: 0.6050 seconds.3 p 2 Duration: 17.4260 seconds.

Conclusion: The time for outputting a data in the first method is similar to that of a large number. However, the second time is obviously far-fetched. 17.5 seconds.

Summary:

1. The flip method is easy to use for selecting a small amount of data, and it will be difficult to select a large amount of data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.