Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function generateCats($n) {
- $categories = [];
- for ($i = 0; $i < $n; $i++) {
- $categories[] = array(
- 'name' => 'categoriy-'.uniqid(),
- 'id' => $i
- );
- }
- return $categories;
- }
- function generateProducts($n, $cats) {
- $products = [];
- for ($i = 0; $i < $n; $i++) {
- $products[] = [
- 'name' => uniqid(),
- 'catId' => $cats[rand(0, count($cats)-1)]['id'],
- 'id' => $i,
- 'quantity' => rand(0, 1000)
- ];
- }
- return $products;
- }
- $categories = generateCats(100);
- $products = generateProducts(100000, $categories);
- xhprof_enable(XHPROF_FLAGS_CPU+XHPROF_FLAGS_MEMORY);
- $sorted = [];
- $quantity = 0;
- foreach ($products as $product) {
- if ($product['quantity'] > 0) {
- if (empty($sorted[$product['catId']])) {
- $sorted[$product['catId']] = [];
- }
- $sorted[$product['catId']][] = $product;
- }
- }
- foreach ($sorted as $i => $value) {
- usort($sorted[$i], function ($a, $b) {
- if ($b['quantity'] == $a['quantity']) {
- return ord(strtolower($a['name']))-ord(strtolower($b['name']));// Oo lol
- } else {
- return $b['quantity']-$a['quantity'];
- }
- });
- }
- usort($categories, function ($a, $b) {
- return ord(strtolower($a['name']))-ord(strtolower($b['name']));// Oo lol
- });
- $result = [];
- foreach ($categories as $category) {
- $result[$category['id']] = [
- 'name' => $category['name'],
- 'products' => $sorted[$category['id']]
- ];
- }
- $xhprof_data = xhprof_disable();
- $XHPROF_ROOT = "/usr/local/Cellar/php55-xhprof/254eb24";
- include_once $XHPROF_ROOT."/xhprof_lib/utils/xhprof_lib.php";
- include_once $XHPROF_ROOT."/xhprof_lib/utils/xhprof_runs.php";
- $xhprof_runs = new XHProfRuns_Default();
- $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement