先引入别人的一个秒表计时类(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.先看第一个例子(大数中取少数):
<span> 1</span> <span> 2</span> <span>/*</span> <span> 3</span> <span>@描述: Stopwatch这个类用户获取脚本执行时间 </span><span> 4</span> <span>@作者: Klesti Hoxha <klesti> </klesti></span><span> 5</span> <span>*/</span> <span> 6</span> <span> 7</span> <span>class</span><span> Stopwatch { </span><span> 8</span> <span>private</span> <span>$start</span><span>; </span><span> 9</span> <span>private</span> <span>$end</span><span>; </span><span>10</span> <span>private</span> <span>$markup_start</span> = <span>array</span><span>(); </span><span>11</span> <span>private</span> <span>$markup_end</span> = <span>array</span><span>(); </span><span>12</span> <span>13</span> <span>function</span> __construct(<span>$markup</span>=<span>false</span><span>) { </span><span>14</span> <span>$this</span>->start(<span>$markup</span><span>); </span><span>15</span> <span>} </span><span>16</span> <span>17</span> <span>public</span> <span>function</span> start(<span>$markup</span>=<span>false</span><span>) { </span><span>18</span> <span>if</span> (!<span>$markup</span><span>) { </span><span>19</span> <span>$this</span>->start = <span>$this</span>-><span>getmicrotime(); </span><span>20</span> } <span>else</span><span> { </span><span>21</span> <span>$this</span>->markup_start[<span>$markup</span>] = <span>$this</span>-><span>getmicrotime(); </span><span>22</span> <span>} </span><span>23</span> <span>} </span><span>24</span> <span>25</span> <span>public</span> <span>function</span> stop(<span>$markup</span>=<span>false</span><span>) { </span><span>26</span> <span>if</span> (!<span>$markup</span><span>) { </span><span>27</span> <span>$this</span>-><span>end</span> = <span>$this</span>-><span>getmicrotime(); </span><span>28</span> } <span>else</span><span> { </span><span>29</span> <span>$this</span>->markup_end[<span>$markup</span>] = <span>$this</span>-><span>getmicrotime(); </span><span>30</span> <span>} </span><span>31</span> <span>return</span> <span>$this</span>->getDuration(<span>$markup</span><span>); </span><span>32</span> <span>} </span><span>33</span> <span>34</span> <span>public</span> <span>function</span> getDuration(<span>$markup</span>=<span>false</span><span>) { </span><span>35</span> <span>if</span> (!<span>$markup</span><span>) </span><span>36</span> <span> { </span><span>37</span> <span>return</span> <span>number_format</span>(<span>$this</span>-><span>end</span>-<span>$this</span>->start,4<span>); </span><span>38</span> } <span>else</span><span> { </span><span>39</span> <span>return</span> <span>number_format</span>(<span>$this</span>->markup_end[<span>$markup</span>]-<span>$this</span>->markup_start[<span>$markup</span>],4<span>); </span><span>40</span> <span> } </span><span>41</span> <span>} </span><span>42</span> <span>43</span> <span>public</span> <span>function</span> <span>reset</span>(<span>$markup</span><span>) { </span><span>44</span> <span>if</span> (!<span>$markup</span><span>) { </span><span>45</span> <span>$this</span>->start = 0<span>; </span><span>46</span> <span>$this</span>-><span>end</span> = 0<span>; </span><span>47</span> <span>$this</span>->markup_start = <span>array</span><span>(); </span><span>48</span> <span>$this</span>->markup_end = <span>array</span><span>(); </span><span>49</span> } <span>else</span><span> { </span><span>50</span> <span>$this</span>->markup_start[<span>$markup</span>] = 0<span>; </span><span>51</span> <span>$this</span>->markup_end[<span>$markup</span>] = 0<span>; </span><span>52</span> <span>} </span><span>53</span> <span>} </span><span>54</span> <span>55</span> <span>private</span> <span>function</span><span> getmicrotime(){ </span><span>56</span> <span>list</span>(<span>$usec</span>, <span>$sec</span>) = <span>explode</span>(" ",<span>microtime</span><span>()); </span><span>57</span> <span>return</span> ((<span>float</span>)<span>$usec</span> + (<span>float</span>)<span>$sec</span><span>); </span><span>58</span> <span>} </span><span>59</span> <span>60</span> <span>} </span><span>61</span> ?>
测试的两种方法如下:
<span> 1</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <span> 2</span> <span>php </span><span> 3</span> <span>require</span> "counttime.class.php";<span>//</span><span>把类文件引用进来,根据你的实际情况来确定路径,这里是在同级目录</span> <span> 4</span> <span>$s</span> = <span>new</span><span> Stopwatch(); </span><span> 5</span> <span> 6</span> <span>$s</span>->start("div1"<span>); </span><span> 7</span> <span>//</span><span>range 是将1到100 列成一个数组</span> <span> 8</span> <span>$numbers</span> = <span>range</span> (1,1000000<span>); </span><span> 9</span> <span>//</span><span>shuffle 将数组顺序随即打乱</span> <span>10</span> <span>shuffle</span> (<span>$numbers</span><span>); </span><span>11</span> <span>//</span><span>array_slice 取该数组中的某一段</span> <span>12</span> <span>$no</span>=30<span>; </span><span>13</span> <span>$result</span> = <span>array_slice</span>(<span>$numbers</span>,0,<span>$no</span><span>); </span><span>14</span> <span>for</span> (<span>$i</span>=0;<span>$i</span>$no;<span>$i</span>++<span>){ </span><span>15</span> <span>echo</span> <span>$result</span>[<span>$i</span>]."<br>"<span>; </span><span>16</span> <span>} </span><span>17</span> <span>echo</span> "第一个测验方法结束<br><br>"<span>; </span><span>18</span> <span>$s</span>->stop("div1"<span>); </span><span>19</span> <span>20</span> <span>$s</span>->start("div2"<span>); </span><span>21</span> <span>function</span> unique_rand(<span>$min</span>, <span>$max</span>, <span>$num</span><span>) </span><span>22</span> <span>{ </span><span>23</span> <span>$count</span> = 0<span>; </span><span>24</span> <span>$return</span> = <span>array</span><span>(); </span><span>25</span> <span>while</span> (<span>$count</span> $num<span>) { </span><span>26</span> <span>$return</span>[] = <span>mt_rand</span>(<span>$min</span>, <span>$max</span><span>); </span><span>27</span> <span>$return</span> = <span>array_flip</span>(<span>array_flip</span>(<span>$return</span><span>)); </span><span>28</span> <span>$count</span> = <span>count</span>(<span>$return</span><span>); </span><span>29</span> <span> } </span><span>30</span> <span>shuffle</span>(<span>$return</span><span>); </span><span>31</span> <span>return</span> <span>$return</span><span>; </span><span>32</span> <span>} </span><span>33</span> <span>34</span> <span>$arr</span> = unique_rand(1, 100000000000, 30<span>); </span><span>35</span> <span>$result</span> = ''<span>; </span><span>36</span> <span>for</span>(<span>$i</span>=0; <span>$i</span> count(<span>$arr</span>);<span>$i</span>++<span>) </span><span>37</span> <span> { </span><span>38</span> <span>$result</span> = <span>$arr</span>[<span>$i</span><span>]; </span><span>39</span> <span>echo</span> <span>$result</span>."<br>"<span>; </span><span>40</span> <span> } </span><span>41</span> <span>$s</span>->stop("div2"<span>); </span><span>42</span> <span>43</span> <span>44</span> <span>//</span><span>输出时间</span> <span>45</span> <span>echo</span> "div 1 Duration: " . <span>$s</span>->getDuration("div1") . " seconds.<br>"<span>; </span><span>46</span> <span>echo</span> "div 2 Duration: " . <span>$s</span>->getDuration("div2") . " seconds.<br>"<span>; </span><span>47</span> ?>
结果为:
<span> 1</span> 413537 <span> 2</span> 198587 <span> 3</span> 60611 <span> 4</span> 599544 <span> 5</span> 946241 <span> 6</span> 462853 <span> 7</span> 570943 <span> 8</span> 980260 <span> 9</span> 717416 <span>10</span> 969324 <span>11</span> 292403 <span>12</span> 614443 <span>13</span> 769469 <span>14</span> 151951 <span>15</span> 167883 <span>16</span> 308727 <span>17</span> 342706 <span>18</span> 955659 <span>19</span> 112311 <span>20</span> 329008 <span>21</span> 182668 <span>22</span> 330600 <span>23</span> 921966 <span>24</span> 993143 <span>25</span> 369227 <span>26</span> 747066 <span>27</span> 793185 <span>28</span> 347872 <span>29</span> 439016 <span>30</span> 428600 <span>31</span> <span>第一个测验方法结束 </span><span>32</span> <span>33</span> 649589727 <span>34</span> 450823813 <span>35</span> 324717181 <span>36</span> 44553762 <span>37</span> 375669657 <span>38</span> 37128612 <span>39</span> 25135327 <span>40</span> 468454185 <span>41</span> 73395357 <span>42</span> 753960006 <span>43</span> 214455797 <span>44</span> 5367614 <span>45</span> 1153263961 <span>46</span> 17803441 <span>47</span> 669057198 <span>48</span> 55366324 <span>49</span> 1117343950 <span>50</span> 203530485 <span>51</span> 1160930064 <span>52</span> 326487914 <span>53</span> 565471236 <span>54</span> 7068443 <span>55</span> 846955001 <span>56</span> 832037489 <span>57</span> 1067813034 <span>58</span> 219239595 <span>59</span> 1069795964 <span>60</span> 1049898846 <span>61</span> 926301266 <span>62</span> 197341539 <span>63</span> div 1 Duration: 0.5800 seconds. <span>64</span> div 2 Duration: 0.0000 seconds.
结论:对于从大量数据中取少量数据,显然第二种比较快。
2.在来看看从大数中取大数:
<span> 1</span> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <span> 2</span> <span>php </span><span> 3</span> <span>require</span> "counttime.class.php";<span>//</span><span>把类文件引用进来,根据你的实际情况来确定路径,这里是在同级目录</span> <span> 4</span> <span>$s</span> = <span>new</span><span> Stopwatch(); </span><span> 5</span> <span> 6</span> <span>$s</span>->start("div1"<span>); </span><span> 7</span> <span>//</span><span>range 是将1到100 列成一个数组</span> <span> 8</span> <span>$numbers</span> = <span>range</span> (1,1000000<span>); </span><span> 9</span> <span>//</span><span>shuffle 将数组顺序随即打乱</span> <span>10</span> <span>shuffle</span> (<span>$numbers</span><span>); </span><span>11</span> <span>//</span><span>array_slice 取该数组中的某一段</span> <span>12</span> <span>$no</span>=10000<span>; </span><span>13</span> <span>$result</span> = <span>array_slice</span>(<span>$numbers</span>,0,<span>$no</span><span>); </span><span>14</span> <span>for</span> (<span>$i</span>=0;<span>$i</span>$no;<span>$i</span>++<span>){ </span><span>15</span> <span>echo</span> <span>$result</span>[<span>$i</span>]."<br>"<span>; </span><span>16</span> <span>} </span><span>17</span> <span>echo</span> "第一个测验方法结束<br><br>"<span>; </span><span>18</span> <span>$s</span>->stop("div1"<span>); </span><span>19</span> <span>20</span> <span>$s</span>->start("div2"<span>); </span><span>21</span> <span>function</span> unique_rand(<span>$min</span>, <span>$max</span>, <span>$num</span><span>) </span><span>22</span> <span>{ </span><span>23</span> <span>$count</span> = 0<span>; </span><span>24</span> <span>$return</span> = <span>array</span><span>(); </span><span>25</span> <span>while</span> (<span>$count</span> $num<span>) { </span><span>26</span> <span>$return</span>[] = <span>mt_rand</span>(<span>$min</span>, <span>$max</span><span>); </span><span>27</span> <span>$return</span> = <span>array_flip</span>(<span>array_flip</span>(<span>$return</span><span>)); </span><span>28</span> <span>$count</span> = <span>count</span>(<span>$return</span><span>); </span><span>29</span> <span> } </span><span>30</span> <span>shuffle</span>(<span>$return</span><span>); </span><span>31</span> <span>return</span> <span>$return</span><span>; </span><span>32</span> <span>} </span><span>33</span> <span>34</span> <span>$arr</span> = unique_rand(1, 1000000, 10000<span>); </span><span>35</span> <span>$result</span> = ''<span>; </span><span>36</span> <span>for</span>(<span>$i</span>=0; <span>$i</span> count(<span>$arr</span>);<span>$i</span>++<span>) </span><span>37</span> <span> { </span><span>38</span> <span>$result</span> = <span>$arr</span>[<span>$i</span><span>]; </span><span>39</span> <span>echo</span> <span>$result</span>."<br>"<span>; </span><span>40</span> <span> } </span><span>41</span> <span>$s</span>->stop("div2"<span>); </span><span>42</span> <span>43</span> <span>44</span> <span>//</span><span>输出时间</span> <span>45</span> <span>echo</span> "div 1 Duration: " . <span>$s</span>->getDuration("div1") . " seconds.<br>"<span>; </span><span>46</span> <span>echo</span> "div 2 Duration: " . <span>$s</span>->getDuration("div2") . " seconds.<br>"<span>; </span><span>47</span> ?>
输出结果为:
<span>1</span> <span>前面省略……………… </span><span>2</span> div 1 Duration: 0.6050 seconds. <span>3</span> div 2 Duration: 17.4260 seconds.
结论:对于第一种方法输出一个数据的时间,跟大数的数字时间差不多。但是第二个时间显然耗费得要离谱。达到了17.5秒。
总结:
1.翻翻法对于选少量数据好用,大量数据就会比较吃力。