search ]

Display Database Queries, Time, and Memory Usage

The following code displays in the footer the number of database queries the page executed, the time it took to run those queries, and the memory usage of the page.

function sv_performance( $visible = false ) {

    $stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
    );

    echo $visible ? $stat : "<!-- {$stat} -->" ;
}

add_action( 'wp_footer', 'sv_performance', 20 );

The result appears as a comment in the footer. Check the page source to see it:

<!-- 33 queries in 1.532 seconds, using 48.75MB memory -->

For more performance optimization, see Improve WordPress Loading Time.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo