Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <?php
  2.  
  3. function generateCats($n) {
  4.     $categories = [];
  5.  
  6.     for ($i = 0; $i < $n; $i++) {
  7.         $categories[] = array(
  8.             'name' => 'categoriy-'.uniqid(),
  9.             'id'   => $i
  10.         );
  11.     }
  12.     return $categories;
  13. }
  14.  
  15. function generateProducts($n, $cats) {
  16.  
  17.     $products = [];
  18.  
  19.     for ($i = 0; $i < $n; $i++) {
  20.         $products[] = [
  21.             'name'     => uniqid(),
  22.             'catId'    => $cats[rand(0, count($cats)-1)]['id'],
  23.             'id'       => $i,
  24.             'quantity' => rand(0, 1000)
  25.         ];
  26.     }
  27.  
  28.     return $products;
  29. }
  30.  
  31. $categories = generateCats(100);
  32. $products   = generateProducts(100000, $categories);
  33.  
  34. xhprof_enable(XHPROF_FLAGS_CPU+XHPROF_FLAGS_MEMORY);
  35.  
  36. $sorted   = [];
  37. $quantity = 0;
  38. foreach ($products as $product) {
  39.     if ($product['quantity'] > 0) {
  40.         if (empty($sorted[$product['catId']])) {
  41.             $sorted[$product['catId']] = [];
  42.         }
  43.  
  44.         $sorted[$product['catId']][] = $product;
  45.     }
  46. }
  47.  
  48. foreach ($sorted as $i => $value) {
  49.     usort($sorted[$i], function ($a, $b) {
  50.             if ($b['quantity'] == $a['quantity']) {
  51.                 return ord(strtolower($a['name']))-ord(strtolower($b['name']));// Oo lol
  52.             } else {
  53.                 return $b['quantity']-$a['quantity'];
  54.             }
  55.         });
  56. }
  57.  
  58. usort($categories, function ($a, $b) {
  59.         return ord(strtolower($a['name']))-ord(strtolower($b['name']));// Oo lol
  60.     });
  61.  
  62. $result = [];
  63.  
  64. foreach ($categories as $category) {
  65.     $result[$category['id']] = [
  66.         'name'     => $category['name'],
  67.         'products' => $sorted[$category['id']]
  68.     ];
  69. }
  70.  
  71. $xhprof_data = xhprof_disable();
  72.  
  73. $XHPROF_ROOT = "/usr/local/Cellar/php55-xhprof/254eb24";
  74. include_once $XHPROF_ROOT."/xhprof_lib/utils/xhprof_lib.php";
  75. include_once $XHPROF_ROOT."/xhprof_lib/utils/xhprof_runs.php";
  76.  
  77. $xhprof_runs = new XHProfRuns_Default();
  78. $run_id      = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement