php算法 找不重复的数,浅谈php随机不重复数的两种算法

先引入别人的一个秒表计时类(counttime.class.php): 1.先看第一个例子(大数中取少数): 1 ? 2 /* 3 @描述: Stopwatch这个类用户获取脚本执行时间 4 @作者: Klesti Hoxha klesti@gmail.com 5 */ 6 7 class Stopwatch { 8 private $start ; 9 private $en

先引入别人的一个秒表计时类(counttime.class.php):

1.先看第一个例子(大数中取少数):

1

2 /*

3 @描述: Stopwatch这个类用户获取脚本执行时间4 @作者: Klesti Hoxha5 */

6

7 classStopwatch {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 functiongetmicrotime(){56 list($usec, $sec) = explode(" ",microtime());57 return ((float)$usec + (float)$sec);58 }59

60 }61 ?>

测试的两种方法如下:

1

2 php3 require "counttime.class.php";//把类文件引用进来,根据你的实际情况来确定路径,这里是在同级目录

4 $s = newStopwatch();5

6 $s->start("div1");7 //range 是将1到100 列成一个数组

8 $numbers = range (1,1000000);9 //shuffle 将数组顺序随即打乱

10 shuffle ($numbers);11 //array_slice 取该数组中的某一段

12 $no=30;13 $result = array_slice($numbers,0,$no);14 for ($i=0;$i

";16 }17 echo "第一个测验方法结束

";18 $s->stop("div1");19

20 $s->start("div2");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(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("div2");42

43

44 //输出时间

45 echo "div 1 Duration: " . $s->getDuration("div1") . " seconds.

";46 echo "div 2 Duration: " . $s->getDuration("div2") . " seconds.

";47 ?>

结果为:

1 413537

2 198587

3 60611

4 599544

5 946241

6 462853

7 570943

8 980260

9 717416

10 969324

11 292403

12 614443

13 769469

14 151951

15 167883

16 308727

17 342706

18 955659

19 112311

20 329008

21 182668

22 330600

23 921966

24 993143

25 369227

26 747066

27 793185

28 347872

29 439016

30 428600

31 第一个测验方法结束32

33 649589727

34 450823813

35 324717181

36 44553762

37 375669657

38 37128612

39 25135327

40 468454185

41 73395357

42 753960006

43 214455797

44 5367614

45 1153263961

46 17803441

47 669057198

48 55366324

49 1117343950

50 203530485

51 1160930064

52 326487914

53 565471236

54 7068443

55 846955001

56 832037489

57 1067813034

58 219239595

59 1069795964

60 1049898846

61 926301266

62 197341539

63 div 1 Duration: 0.5800 seconds.

64 div 2 Duration: 0.0000 seconds.

结论:对于从大量数据中取少量数据,显然第二种比较快。

2.在来看看从大数中取大数:

1

2 php3 require "counttime.class.php";//把类文件引用进来,根据你的实际情况来确定路径,这里是在同级目录

4 $s = newStopwatch();5

6 $s->start("div1");7 //range 是将1到100 列成一个数组

8 $numbers = range (1,1000000);9 //shuffle 将数组顺序随即打乱

10 shuffle ($numbers);11 //array_slice 取该数组中的某一段

12 $no=10000;13 $result = array_slice($numbers,0,$no);14 for ($i=0;$i

";16 }17 echo "第一个测验方法结束

";18 $s->stop("div1");19

20 $s->start("div2");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(array_flip($return));28 $count = count($return);29 }30 shuffle($return);31 return $return;32 }33

34 $arr = unique_rand(1, 1000000, 10000);35 $result = '';36 for($i=0; $i < count($arr);$i++)37 {38 $result = $arr[$i];39 echo $result."

";40 }41 $s->stop("div2");42

43

44 //输出时间

45 echo "div 1 Duration: " . $s->getDuration("div1") . " seconds.

";46 echo "div 2 Duration: " . $s->getDuration("div2") . " seconds.

";47 ?>

输出结果为:

1 前面省略………………2 div 1 Duration: 0.6050 seconds.

3 div 2 Duration: 17.4260 seconds.

结论:对于第一种方法输出一个数据的时间,跟大数的数字时间差不多。但是第二个时间显然耗费得要离谱。达到了17.5秒。

总结:

1.翻翻法对于选少量数据好用,大量数据就会比较吃力。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值