Advertisement
Guest User

Untitled

a guest
Aug 26th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. define('NUMBER_OF_TESTS', 5000);
  4. define('ARRAY_SIZE', 200);
  5.  
  6. function quicksort( $array )
  7. {
  8.  $cur = 1;
  9.  $stack[1]['l'] = 0;
  10.  $stack[1]['r'] = count($array)-1;
  11.  
  12.  do
  13.  {
  14.   $l = $stack[$cur]['l'];
  15.   $r = $stack[$cur]['r'];
  16.   $cur--;
  17.  
  18.   do
  19.   {
  20.    $i = $l;
  21.    $j = $r;
  22.    $tmp = $array[(int)( ($l+$r)/2 )][1];
  23.  
  24.    do
  25.    {
  26.     while ( $array[$i][1] < $tmp )
  27.      $i++;
  28.  
  29.     while ( $tmp < $array[$j][1] )
  30.      $j--;
  31.  
  32.     if ( $i <= $j )
  33.     {
  34.      $w = $array[$i];
  35.      $array[$i] = $array[$j];
  36.      $array[$j] = $w;
  37.  
  38.      $i++;
  39.      $j--;
  40.     }
  41.  
  42.    } while ( $i <= $j );
  43.  
  44.  
  45.    if ( $i < $r )
  46.    {
  47.     $cur++;
  48.     $stack[$cur]['l'] = $i;
  49.     $stack[$cur]['r'] = $r;
  50.    }
  51.    $r = $j;
  52.  
  53.   } while( $l < $r );
  54.  
  55.  } while( $cur != 0 );
  56.  
  57.  return $array;
  58. }
  59.  
  60. function microtime_float()
  61. {
  62.  return microtime(true);
  63. }
  64.  
  65. function set(&$array)
  66. {
  67.  for ($i = 0; $i<ARRAY_SIZE; $i++)
  68.  {
  69.   $array[$i][0] = "user ".$i;
  70.   $array[$i][1] = rand(10, 100);
  71.  }
  72. }
  73.  
  74. echo "<pre>";
  75.  
  76. // ----------
  77.  
  78. $time = microtime_float();
  79. set($array);
  80. $time = microtime_float()-$time;
  81. $settime = ($time*NUMBER_OF_TESTS)/2;
  82.  
  83. // ----------
  84.  
  85. function usort_function($a, $b)
  86. {
  87.  return ($a[1] == $b[1]) ? 0 : ($a[1] < $b[1]) ? -1 : 1;
  88. }
  89.  
  90. $time = microtime_float();
  91. for ($i = 1; $i<NUMBER_OF_TESTS; $i++)
  92. {
  93.  if ($i&1)
  94.   set($array);
  95.  usort($array, 'usort_function');
  96. }
  97. $time = microtime_float()-$time;
  98. echo "usort: ".$time."-".$settime."=<br>".($time-$settime)."<br>";
  99.  
  100. echo "<br>";
  101.  
  102. // ----------
  103.  
  104. $time = microtime_float();
  105. for ($i = 1; $i<NUMBER_OF_TESTS; $i++)
  106. {
  107.  if ($i&1)
  108.   set($array);
  109.  $array = quicksort($array);
  110. }
  111. $time = microtime_float()-$time;
  112. echo "quicksort: ".$time."-".$settime."=<br>".($time-$settime)."<br>";
  113.  
  114. echo "</pre>";
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement