Skip to content

Commit 6811631

Browse files
committed
👌 IMPROVE: Updated benchmarks for PHP 7.4 compatibility
1 parent af0ac8b commit 6811631

3 files changed

Lines changed: 76 additions & 62 deletions

File tree

benchpress.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function benchpress_create_snapshots_table() {
7474
require_once BENCHPRESS_PLUGIN_DIR . 'classes/BenchPress_Table.php';
7575
require_once BENCHPRESS_PLUGIN_DIR . 'classes/BenchPress_Snapshots_Table.php';
7676
require_once BENCHPRESS_PLUGIN_DIR . 'includes/helper-functions.php';
77+
// Include PHP 8-specific functions if the server's PHP version is 8.0 or above.
78+
if ( version_compare( PHP_VERSION, '8.0', '>=' ) ) {
79+
require_once BENCHPRESS_PLUGIN_DIR . 'includes/php-8.0-functions.php';
80+
}
7781

7882
/**
7983
* Register BenchPress admin menu.
@@ -233,13 +237,16 @@ function benchpress_render_settings_page() {
233237
echo '<table class="form-table">';
234238
echo '<tr><th>' . esc_html__( 'Loop Count for Benchmarks', 'benchpress' ) . '</th>';
235239
echo '<td><input type="number" name="benchpress_loop_count" value="' . esc_attr( $loop_count ) . '" /></td></tr>';
236-
echo '<tr><th>' . esc_html__( 'Enable Switch vs Match Benchmark', 'benchpress' ) . '</th>';
237-
echo '<td><input type="checkbox" name="benchpress_enable_switch_vs_match" ' . checked( 1, $enable_switch_vs_match, false ) . ' /></td></tr>';
240+
// Only add this setting when PHP 8.0+ is installed.
241+
if ( version_compare( PHP_VERSION, '8.0', '>=' ) ) {
242+
echo '<tr><th>' . esc_html__( 'Enable Switch vs Match Benchmark', 'benchpress' ) . '</th>';
243+
echo '<td><input type="checkbox" name="benchpress_enable_switch_vs_match" ' . checked( 1, $enable_switch_vs_match, false ) . ' /></td></tr>';
244+
}
238245
echo '<tr><th>' . esc_html__( 'Enable Transient vs Direct Query Benchmark', 'benchpress' ) . '</th>';
239246
echo '<td><input type="checkbox" name="benchpress_enable_transient_vs_query" ' . checked( 1, get_option( 'benchpress_enable_transient_vs_query', 1 ), false ) . ' /></td></tr>';
240247
echo '<tr><th>' . esc_html__( 'Enable Post Meta Access Benchmark', 'benchpress' ) . '</th>';
241248
echo '<td><input type="checkbox" name="benchpress_enable_meta_query_test" ' . checked( 1, get_option( 'benchpress_enable_meta_query_test', 1 ), false ) . ' /></td></tr>';
242-
249+
243250
echo '</table>';
244251

245252
// WP_Query Customization Section.

includes/helper-functions.php

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,6 @@
55
* @package BenchPress
66
*/
77

8-
/**
9-
* Benchmark Switch vs Match performance.
10-
*
11-
* @since 1.0.0
12-
* @return void|array Benchmark data showing performance difference between switch and match.
13-
*/
14-
function benchpress_benchmark_switch_vs_match() {
15-
if ( ! get_option( 'benchpress_enable_switch_vs_match', 1 ) ) {
16-
return;
17-
}
18-
19-
$loop_count = get_option( 'benchpress_loop_count', 1000000 );
20-
21-
// Measure execution time for switch statement.
22-
$start_switch = microtime( true );
23-
for ( $i = 0; $i < $loop_count; $i++ ) {
24-
switch ( $i % 3 ) {
25-
case 0:
26-
$result = 'zero';
27-
break;
28-
case 1:
29-
$result = 'one';
30-
break;
31-
case 2:
32-
$result = 'two';
33-
break;
34-
}
35-
}
36-
$end_switch = microtime( true );
37-
$switch_time = $end_switch - $start_switch;
38-
39-
// Measure execution time for match expression.
40-
$start_match = microtime( true );
41-
for ( $i = 0; $i < $loop_count; $i++ ) {
42-
$result = match ( $i % 3 ) {
43-
0 => 'zero',
44-
1 => 'one',
45-
2 => 'two',
46-
};
47-
}
48-
$end_match = microtime( true );
49-
$match_time = $end_match - $start_match;
50-
51-
// Calculate the difference.
52-
$difference = $switch_time - $match_time;
53-
$faster_or_slower = $difference > 0 ? 'slower' : 'faster';
54-
55-
return [
56-
'name' => esc_html__( 'Switch vs Match', 'benchpress' ),
57-
'execution_time'=> round( abs( $difference ), 5 ),
58-
'description' => sprintf(
59-
esc_html__( 'The switch statement is %s by %s seconds compared to match.', 'benchpress' ),
60-
$faster_or_slower,
61-
round( abs( $difference ), 5 )
62-
),
63-
];
64-
}
65-
668
/**
679
* Benchmark WP_Query by ID with customizable options.
6810
*
@@ -243,7 +185,8 @@ function benchpress_benchmark_string_concatenation() {
243185
function benchpress_run_all_benchmarks() {
244186
$benchmarks = [];
245187

246-
if ( $switch_vs_match = benchpress_benchmark_switch_vs_match() ) {
188+
// Only add Switch vs Match benchmark if PHP 8.0+.
189+
if ( function_exists( 'benchpress_benchmark_switch_vs_match' ) && $switch_vs_match = benchpress_benchmark_switch_vs_match() ) {
247190
$benchmarks[] = $switch_vs_match;
248191
}
249192

includes/php-8.0-functions.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* PHP 8-specific benchmark functions for BenchPress plugin.
4+
*
5+
* @package BenchPress
6+
*/
7+
8+
/**
9+
* Benchmark Switch vs Match performance.
10+
*
11+
* @since 1.0.0
12+
* @return void|array Benchmark data showing performance difference between switch and match.
13+
*/
14+
function benchpress_benchmark_switch_vs_match() {
15+
if ( ! get_option( 'benchpress_enable_switch_vs_match', 1 ) ) {
16+
return;
17+
}
18+
19+
$loop_count = get_option( 'benchpress_loop_count', 1000000 );
20+
21+
// Measure execution time for switch statement.
22+
$start_switch = microtime( true );
23+
for ( $i = 0; $i < $loop_count; $i++ ) {
24+
switch ( $i % 3 ) {
25+
case 0:
26+
$result = 'zero';
27+
break;
28+
case 1:
29+
$result = 'one';
30+
break;
31+
case 2:
32+
$result = 'two';
33+
break;
34+
}
35+
}
36+
$end_switch = microtime( true );
37+
$switch_time = $end_switch - $start_switch;
38+
39+
// Measure execution time for match expression.
40+
$start_match = microtime( true );
41+
for ( $i = 0; $i < $loop_count; $i++ ) {
42+
$result = match ( $i % 3 ) {
43+
0 => 'zero',
44+
1 => 'one',
45+
2 => 'two',
46+
};
47+
}
48+
$end_match = microtime( true );
49+
$match_time = $end_match - $start_match;
50+
51+
// Calculate the difference.
52+
$difference = $switch_time - $match_time;
53+
$faster_or_slower = $difference > 0 ? 'slower' : 'faster';
54+
55+
return [
56+
'name' => esc_html__( 'Switch vs Match', 'benchpress' ),
57+
'execution_time'=> round( abs( $difference ), 5 ),
58+
'description' => sprintf(
59+
esc_html__( 'The switch statement is %s by %s seconds compared to match.', 'benchpress' ),
60+
$faster_or_slower,
61+
round( abs( $difference ), 5 )
62+
),
63+
];
64+
}

0 commit comments

Comments
 (0)