Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 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 $cat => $value) {
  49.     usort($sorted[$cat], function ($a, $b) {
  50.             return $b['quantity']-$a['quantity'];
  51.         });
  52. }
  53.  
  54. $result = [];
  55.  
  56. ksort($categories);
  57.  
  58. foreach ($categories as $category) {
  59.     $result[$category['id']] = [
  60.         'name'     => $category['name'],
  61.         'products' => $sorted[$category['id']]
  62.     ];
  63. }
  64.  
  65. $xhprof_data = xhprof_disable();
  66.  
  67. $XHPROF_ROOT = "/usr/local/Cellar/php55-xhprof/254eb24";
  68. include_once $XHPROF_ROOT."/xhprof_lib/utils/xhprof_lib.php";
  69. include_once $XHPROF_ROOT."/xhprof_lib/utils/xhprof_runs.php";
  70.  
  71. $xhprof_runs = new XHProfRuns_Default();
  72. $run_id      = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement