# Measure How Long Code Takes

> Adds wpide_timer_start() and wpide_timer_stop() helpers that measure the time and memory a piece of code uses and write the result to the PHP error log, to find slow functions.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: performance, helper, profiling
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/debug-timer-helper/

## Code

```php
if ( ! function_exists( 'wpide_timer_start' ) ) {
    function wpide_timer_start( $name ) {
        $GLOBALS['wpide_timers'][ $name ] = array( microtime( true ), memory_get_usage() );
    }

    function wpide_timer_stop( $name, $log = true ) {
        if ( empty( $GLOBALS['wpide_timers'][ $name ] ) ) {
            return null;
        }

        list( $started, $memory ) = $GLOBALS['wpide_timers'][ $name ];
        unset( $GLOBALS['wpide_timers'][ $name ] );

        $ms = round( ( microtime( true ) - $started ) * 1000, 2 );

        if ( $log ) {
            error_log( sprintf( '[timer] %s: %s ms, %s memory', $name, $ms, size_format( max( 0, memory_get_usage() - $memory ), 1 ) ) );
        }

        return $ms;
    }
}
```

## Notes

Example wpide_timer_start( 'menu' ); ... wpide_timer_stop( 'menu' ); the stop call also returns the milliseconds.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Measure How Long Code Takes" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
