fork download
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. class SortTest {
  4. private $a = array();
  5.  
  6. public function __constructor($array) {
  7. $this->a = $array;
  8. }
  9.  
  10. public function start() {
  11. $time = -microtime(true) *1000 *1000;
  12.  
  13. for ($i=0; $i<1000; $i++) { // pętla for, w celu 'uwypuklenia' dziwnych czasów wykonywania sortowań
  14. $tmp = $this->a;
  15. $this->mySort($tmp);
  16. }
  17.  
  18. $time += microtime(true) *1000 *1000;
  19. return number_format($time, 1, '.', ''); // formatujemy ms z jednym przecinkiem (sprawdzenie, czy nie ma błędu przy zaokrąglaniu przy mniejszych liczbach)
  20. }
  21.  
  22. public function mySort($a) {
  23. sort($a);
  24. }
  25. }
  26.  
  27. ////////////////////////////////////////////////////////////////////////////////
  28. function makeArray($type, $count) {
  29. $arr = array();
  30. switch($type) { // tworzymy odpowiednią tabelę testową
  31. case 'inc':
  32. for ($i=0; $i<$count; $i++)
  33. $arr[] = $i;
  34. break;
  35. case 'rnd':
  36. for ($i=0; $i<$count; $i++)
  37. $arr[] = mt_rand(0, $count);
  38. break;
  39. case 'dec':
  40. for ($i=$count-1; $i>=0; $i--)
  41. $arr[] = $i;
  42. break;
  43. }
  44. return $arr;
  45. }
  46.  
  47. ////////////////////////////////////////////////////////////////////////////////
  48. function testSort($type, $count) {
  49. $count *= 1000;
  50. $arr = makeArray($type,$count);
  51.  
  52. $sort = new SortTest($arr);
  53. echo($sort->start().' ms, ');
  54. }
  55.  
  56. ////////////////////////////////////////////////////////////////////////////////
  57. echo ' == INC: ';
  58. testSort('inc',1);
  59. testSort('inc',10);
  60. testSort('inc',100);
  61. echo ' == RND: ';
  62. testSort('rnd',1);
  63. testSort('rnd',10);
  64. testSort('rnd',100);
  65. echo ' == DEC: ';
  66. testSort('dec',1);
  67. testSort('dec',10);
  68. testSort('dec',100);
Success #stdin #stdout 0.19s 20560KB
stdin
Standard input is empty
stdout
 == INC: 821.3 ms, 830.8 ms, 811.0 ms,  == RND: 773.0 ms, 811.0 ms, 805.0 ms,  == DEC: 783.3 ms, 778.5 ms, 806.0 ms,