Skip to content

パフォーマンス最適化

; php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0 # 本番環境
<?php
// 大きな配列の処理
function processLargeArray($items) {
foreach ($items as $item) {
// 処理
yield $item; // ジェネレータを使用
}
}
// 使用例
foreach (processLargeArray($largeArray) as $item) {
// メモリ効率的な処理
}
<?php
// N+1問題の解決
// 悪い例
$users = User::all();
foreach ($users as $user) {
$posts = $user->posts; // 各ユーザーごとにクエリ
}
// 良い例(Eager Loading)
$users = User::with('posts')->get();
foreach ($users as $user) {
$posts = $user->posts; // 既にロード済み
}