/
ternopolskiy
/
practice_optimization
Обзор
Документация
Войти
/
ternopolskiy
/
practice_optimization
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
benchmark.php
70 строк
2 KB
ternopolskiy
first_commit
14 май 2026, 13:33
14 май 2026, 13:33
1cb9154
Код
Авторство
О чём код?
<?php /** * benchmark.php * Скрипт для замера производительности функций из index.php */ require_once 'index.php'; $PRODUCT_COUNT = 50000; $SEARCH_ID = 49999; $ITERATIONS = 1000; echo "Генерация базы данных товаров...\n"; $products = generateProducts($PRODUCT_COUNT); $indexedProducts = prepareIndexedProducts($products); echo "Сгенерировано товаров: {$PRODUCT_COUNT}\n"; echo "Ищем товар с ID: {$SEARCH_ID}\n\n"; function runBenchmark(string $funcName, array $args, int $iterations): array { $start = microtime(true); for ($i = 0; $i < $iterations; $i++) { call_user_func_array($funcName, $args); } $end = microtime(true); $totalTime = $end - $start; return [ 'total_time_sec' => $totalTime, 'avg_time_ms' => ($totalTime / $iterations) * 1000, 'iterations' => $iterations ]; } echo "=== ЗАПУСК БЕНЧМАРКА ===\n"; echo "Тестируем searchProductSlow...\n"; $resultSlow = runBenchmark('searchProductSlow', [$products, $SEARCH_ID], $ITERATIONS); echo "Тестируем searchProductFast...\n"; $resultFast = runBenchmark('searchProductFast', [$indexedProducts, $SEARCH_ID], $ITERATIONS); echo "\n=== ОТЧЕТ ===\n"; printf("%-25s | %-15s | %-15s\n", "Функция", "Всего (сек)", "Среднее (мс)"); echo str_repeat("-", 60) . "\n"; printf("%-25s | %-15.4f | %-15.4f\n", "searchProductSlow", $resultSlow['total_time_sec'], $resultSlow['avg_time_ms']); printf("%-25s | %-15.4f | %-15.4f\n", "searchProductFast", $resultFast['total_time_sec'], $resultFast['avg_time_ms']); if ($resultFast['avg_time_ms'] > 0) { $ratio = $resultSlow['avg_time_ms'] / $resultFast['avg_time_ms']; echo "\n🚀 Ускорение: в " . number_format($ratio, 2) . " раз(а)\n"; } else { echo "\n⚡ Быстрый вариант выполнился мгновенно (< 0.0001 мс)\n"; }