What is the Benchmark class in CodeIgniter 4?
Answer
The Benchmark class in CI4 measures elapsed time between two points in your code, helping identify performance bottlenecks. The timer is automatically started when CI4 begins processing and stopped at the end, giving you the total execution time. Mark custom timing points: $benchmark = service("timer"). Start a timer: $benchmark->start("my_process"). Stop it: $benchmark->stop("my_process"). Get elapsed time: $benchmark->getElapsedTime("my_process"). In development mode, the Debug Toolbar automatically displays all timer data in the Timeline tab. Check memory usage: $benchmark->getMemoryUsage(). The Benchmark class is automatically shared via the Services class — the same instance is used throughout the request. Use it to profile specific database queries, external API calls, or complex computations to identify where time is being spent in your application.