Skip to content

Instantly share code, notes, and snippets.

@kasparsd
Created January 3, 2025 21:41
Show Gist options
  • Save kasparsd/554ac6bcf8eb9e444fa3a64f6a807c57 to your computer and use it in GitHub Desktop.
Save kasparsd/554ac6bcf8eb9e444fa3a64f6a807c57 to your computer and use it in GitHub Desktop.
<?php
// See https://x.com/PoeHaH/status/1875290205894164758
function run_times( callable $callback, int $runs ) {
$start = microtime(true);
for ($i = 0; $i < $runs; $i++) {
$callback();
}
return microtime(true) - $start;
}
$dir = trailingslashit( get_template_directory() );
$tests = [
'option_non_existent' => function() {
get_option( 'this_option_does_not_exist_' . random_int( 0, PHP_INT_MAX ) );
},
'option_non_existent_same' => function() {
get_option( 'this_option_does_not_exist' );
},
'clear_stats_alone' => function() {
clearstatcache( true );
},
'is_file_exists_with_clear_stats' => function() use ( $dir ) {
clearstatcache( true );
is_file( $dir . 'style.css' );
},
'is_file_exists_without_clear_stats' => function() use ( $dir ) {
is_file( $dir . 'style.css' );
},
'file_exists_with_clear_stats' => function() use ( $dir ) {
clearstatcache( true );
file_exists( $dir . 'style.css' );
},
'file_exists_without_clear_stats' => function() use ( $dir ) {
file_exists( $dir . 'style.css' );
},
'is_file_exists_not_with_clear_stats' => function() use ( $dir ) {
clearstatcache( true );
is_file( $dir . 'style-not-exists.css' );
},
];
$runs = array_map(
function( $test ) {
return run_times( $test, 10000 );
},
$tests
);
print_r( $runs );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment