| 1 | <pre>
|
|---|
| 2 | <?php
|
|---|
| 3 |
|
|---|
| 4 | error_reporting( E_ALL );
|
|---|
| 5 | set_time_limit(0);
|
|---|
| 6 |
|
|---|
| 7 | $trials = 200000;
|
|---|
| 8 |
|
|---|
| 9 | $data = array(
|
|---|
| 10 | array(
|
|---|
| 11 | 'result' => null,
|
|---|
| 12 | 'inputs' => array(
|
|---|
| 13 | 'a:3:{i:174;O:8:"stdClass":3:{s:2:"ID";s:3:"174";s:9:"post_name";s:7:"level-1";s:11:"post_parent";s:1:"0";}i:173;O:8:"stdClass":3:{s:2:"ID";s:3:"173";s:9:"post_name";s:7:"level-2";s:11:"post_parent";s:3:"174";}i:172;O:8:"stdClass":3:{s:2:"ID";s:3:"172";s:9:"post_name";s:7:"level-3";s:11:"post_parent";s:3:"173";}}',
|
|---|
| 14 | 'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-4";}',
|
|---|
| 15 | ),
|
|---|
| 16 | ),
|
|---|
| 17 | array(
|
|---|
| 18 | 'result' => null,
|
|---|
| 19 | 'inputs' => array(
|
|---|
| 20 | 'a:3:{i:174;O:8:"stdClass":3:{s:2:"ID";s:3:"174";s:9:"post_name";s:7:"level-1";s:11:"post_parent";s:1:"0";}i:173;O:8:"stdClass":3:{s:2:"ID";s:3:"173";s:9:"post_name";s:7:"level-2";s:11:"post_parent";s:3:"174";}i:172;O:8:"stdClass":3:{s:2:"ID";s:3:"172";s:9:"post_name";s:7:"level-3";s:11:"post_parent";s:3:"173";}}',
|
|---|
| 21 | 'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-1";}',
|
|---|
| 22 | ),
|
|---|
| 23 | ),
|
|---|
| 24 | array(
|
|---|
| 25 | 'result' => null,
|
|---|
| 26 | 'inputs' => array(
|
|---|
| 27 | 'a:3:{i:174;O:8:"stdClass":3:{s:2:"ID";s:3:"174";s:9:"post_name";s:7:"level-1";s:11:"post_parent";s:1:"0";}i:173;O:8:"stdClass":3:{s:2:"ID";s:3:"173";s:9:"post_name";s:7:"level-2";s:11:"post_parent";s:3:"174";}i:172;O:8:"stdClass":3:{s:2:"ID";s:3:"172";s:9:"post_name";s:7:"level-3";s:11:"post_parent";s:3:"173";}}',
|
|---|
| 28 | 'a:3:{i:0;s:7:"level-5";i:1;s:7:"level-2";i:2;s:7:"level-3";}',
|
|---|
| 29 | ),
|
|---|
| 30 | ),
|
|---|
| 31 | array(
|
|---|
| 32 | 'result' => 172,
|
|---|
| 33 | 'inputs' => array(
|
|---|
| 34 | 'a:3:{i:174;O:8:"stdClass":3:{s:2:"ID";s:3:"174";s:9:"post_name";s:7:"level-1";s:11:"post_parent";s:1:"0";}i:173;O:8:"stdClass":3:{s:2:"ID";s:3:"173";s:9:"post_name";s:7:"level-2";s:11:"post_parent";s:3:"174";}i:172;O:8:"stdClass":3:{s:2:"ID";s:3:"172";s:9:"post_name";s:7:"level-3";s:11:"post_parent";s:3:"173";}}',
|
|---|
| 35 | 'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-3";}',
|
|---|
| 36 | ),
|
|---|
| 37 | ),
|
|---|
| 38 | array(
|
|---|
| 39 | 'result' => 174,
|
|---|
| 40 | 'inputs' => array(
|
|---|
| 41 | 'a:3:{i:174;O:8:"stdClass":3:{s:2:"ID";s:3:"174";s:9:"post_name";s:7:"level-1";s:11:"post_parent";s:1:"0";}i:173;O:8:"stdClass":3:{s:2:"ID";s:3:"173";s:9:"post_name";s:7:"level-2";s:11:"post_parent";s:3:"174";}i:172;O:8:"stdClass":3:{s:2:"ID";s:3:"172";s:9:"post_name";s:7:"level-3";s:11:"post_parent";s:3:"173";}}',
|
|---|
| 42 | 'a:1:{i:0;s:7:"level-1";}',
|
|---|
| 43 | ),
|
|---|
| 44 | ),
|
|---|
| 45 | array(
|
|---|
| 46 | 'result' => 600,
|
|---|
| 47 | 'inputs' => array(
|
|---|
| 48 | 'a:1:{i:600;O:8:"stdClass":3:{s:2:"ID";s:3:"600";s:9:"post_name";s:4:"blog";s:11:"post_parent";s:1:"0";}}',
|
|---|
| 49 | 'a:1:{i:0;s:4:"blog";}',
|
|---|
| 50 | ),
|
|---|
| 51 | ),
|
|---|
| 52 | array(
|
|---|
| 53 | 'result' => 568,
|
|---|
| 54 | 'inputs' => array(
|
|---|
| 55 | 'a:2:{i:12;O:8:"stdClass":3:{s:2:"ID";s:2:"12";s:9:"post_name";s:5:"about";s:11:"post_parent";s:1:"0";}i:568;O:8:"stdClass":3:{s:2:"ID";s:3:"568";s:9:"post_name";s:17:"about-da-children";s:11:"post_parent";s:2:"12";}}',
|
|---|
| 56 | 'a:2:{i:0;s:5:"about";i:1;s:17:"about-da-children";}',
|
|---|
| 57 | ),
|
|---|
| 58 | ),
|
|---|
| 59 | array(
|
|---|
| 60 | 'result' => 145,
|
|---|
| 61 | 'inputs' => array(
|
|---|
| 62 | 'a:2:{i:145;O:8:"stdClass":3:{s:2:"ID";s:3:"145";s:9:"post_name";s:12:"child-page-2";s:11:"post_parent";s:3:"953";}i:953;O:8:"stdClass":3:{s:2:"ID";s:3:"953";s:9:"post_name";s:11:"parent-page";s:11:"post_parent";s:1:"0";}}',
|
|---|
| 63 | 'a:2:{i:0;s:11:"parent-page";i:1;s:12:"child-page-2";}',
|
|---|
| 64 | ),
|
|---|
| 65 | ),
|
|---|
| 66 | );
|
|---|
| 67 |
|
|---|
| 68 | $functions = array( 'get_page_by_path', 'get_page_by_path_rewritten', 'get_page_by_path_dfix' ); //'get_page_by_path_simple_fix'
|
|---|
| 69 | //$functions = array( 'get_page_by_path_dfix' );
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | foreach ( $data as $index => $set )
|
|---|
| 73 | foreach ( $set['inputs'] as $input_index => $input )
|
|---|
| 74 | $data[$index]['inputs'][$input_index] = unserialize( $input );
|
|---|
| 75 |
|
|---|
| 76 | foreach ( $functions as $function ) {
|
|---|
| 77 | $errors = 0;
|
|---|
| 78 |
|
|---|
| 79 | foreach ( $data as $index => $set ) {
|
|---|
| 80 | $result = @call_user_func_array( $function, $set['inputs'] );
|
|---|
| 81 |
|
|---|
| 82 | if ( $result != $set['result'] ) {
|
|---|
| 83 | echo "$function failed test set $index\n";
|
|---|
| 84 | echo "\tExpected result: {$set['result']}\n";
|
|---|
| 85 | echo "\tActual result: ";
|
|---|
| 86 | var_export( $result );
|
|---|
| 87 |
|
|---|
| 88 | $errors++;
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | if ( 0 == $errors )
|
|---|
| 93 | echo "$function passed all tests.\n";
|
|---|
| 94 |
|
|---|
| 95 | echo "\n";
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | $results = array();
|
|---|
| 99 |
|
|---|
| 100 | for ( $count = 0; $count < $trials; $count++ ) {
|
|---|
| 101 | $function = $functions[ rand( 0, count( $functions ) - 1 ) ];
|
|---|
| 102 |
|
|---|
| 103 | if ( ! isset( $results[$function] ) ) {
|
|---|
| 104 | $results[$function] = array(
|
|---|
| 105 | 'trials' => 0,
|
|---|
| 106 | 'total_time' => 0,
|
|---|
| 107 | 'trial_time' => array(),
|
|---|
| 108 | 'total_count' => 0,
|
|---|
| 109 | );
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | run_trial( $function );
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | foreach ( $functions as $function ) {
|
|---|
| 116 | $result = $results[$function];
|
|---|
| 117 |
|
|---|
| 118 | echo "$function\n";
|
|---|
| 119 | echo "==========================\n";
|
|---|
| 120 | echo "Trials: {$result['trials']}\n";
|
|---|
| 121 | echo "Total Time: {$result['total_time']}\n";
|
|---|
| 122 | echo "Average Time: " . ( $result['total_time'] / $result['trials'] ) . "\n";
|
|---|
| 123 | echo "Runs per Sec: " . ( 1 / ( $result['total_time'] / $result['trials'] ) ) . "\n";
|
|---|
| 124 |
|
|---|
| 125 | if ( isset( $results['no_cast']['total_time'] ) )
|
|---|
| 126 | echo "Speed Difference: " . ( ( $result['total_time'] - $results['no_cast']['total_time'] ) / $results['no_cast']['total_time'] ) . "\n";
|
|---|
| 127 |
|
|---|
| 128 | echo "\n";
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | function run_trial( $function, $count = 10 ) {
|
|---|
| 134 | global $results, $data;
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | $time = 0;
|
|---|
| 138 | $total_count = 0;
|
|---|
| 139 |
|
|---|
| 140 | for ( $i = 0; $i < $count; $i++ ) {
|
|---|
| 141 | $args = $data[ rand( 0, count( $data ) - 1 ) ]['inputs'];
|
|---|
| 142 |
|
|---|
| 143 | $start = microtime( true );
|
|---|
| 144 | @call_user_func_array( $function, $args );
|
|---|
| 145 | $end = microtime( true );
|
|---|
| 146 |
|
|---|
| 147 | $time += $end - $start;
|
|---|
| 148 |
|
|---|
| 149 | $total_count++;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | $results[$function]['trials']++;
|
|---|
| 154 | $results[$function]['total_time'] += $time;
|
|---|
| 155 | $results[$function]['total_count'] += $total_count;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | function get_page_by_path( $pages, $parts ) {
|
|---|
| 160 | $revparts = array_reverse( $parts );
|
|---|
| 161 |
|
|---|
| 162 | $foundid = 0;
|
|---|
| 163 | foreach ( $pages as $page ) {
|
|---|
| 164 | if ( $page->post_name == $revparts[0] ) {
|
|---|
| 165 | $count = 0;
|
|---|
| 166 | if ( $page->post_parent != 0 ) {
|
|---|
| 167 | if ( null === ( $parent_page = $pages[ $page->post_parent ] ) )
|
|---|
| 168 | continue;
|
|---|
| 169 |
|
|---|
| 170 | while ( $parent_page->ID != 0 ) {
|
|---|
| 171 | $count++;
|
|---|
| 172 | if ( $parent_page->post_name != $revparts[ $count ] )
|
|---|
| 173 | break;
|
|---|
| 174 | $parent_page = $pages[ $parent_page->post_parent ];
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | if ( $parent_page->ID == 0 && $count+1 == count($revparts) ) {
|
|---|
| 178 | $foundid = $page->ID;
|
|---|
| 179 | break;
|
|---|
| 180 | }
|
|---|
| 181 | } else if ( count($revparts) == 1 ) {
|
|---|
| 182 | $foundid = $page->ID;
|
|---|
| 183 | break;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | if ( $foundid )
|
|---|
| 189 | return $foundid;
|
|---|
| 190 |
|
|---|
| 191 | return null;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | function get_page_by_path_dfix($pages, $parts) {
|
|---|
| 195 |
|
|---|
| 196 | $revparts = array_reverse( $parts );
|
|---|
| 197 | $revparts_count = count($revparts);
|
|---|
| 198 |
|
|---|
| 199 | if ( empty($revparts) )
|
|---|
| 200 | return null;
|
|---|
| 201 |
|
|---|
| 202 | if ( 1 == $revparts_count ) {
|
|---|
| 203 | foreach ( $pages as $page ) {
|
|---|
| 204 | if ( $page->post_name == $revparts[0] )
|
|---|
| 205 | return $page->ID;
|
|---|
| 206 | }
|
|---|
| 207 | return null;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | foreach ( $pages as $page ) {
|
|---|
| 211 | if ( $page->post_name == $revparts[0] && $page->post_parent != 0) {
|
|---|
| 212 | $cur_page = $page;
|
|---|
| 213 | $count = 0;
|
|---|
| 214 | while ( isset($pages[ $cur_page->post_parent ]) ) {
|
|---|
| 215 | if ( $cur_page->post_name != $revparts[ $count ] )
|
|---|
| 216 | continue 2; // Continue next foreach()
|
|---|
| 217 | $count++;
|
|---|
| 218 | $cur_page = $pages[ $cur_page->post_parent ];
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | if ( $count+1 == $revparts_count && $cur_page->post_name == $revparts[$count] )
|
|---|
| 222 | return $page->ID;
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | return null;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | function get_page_by_path_simple_fix( $pages, $parts ) {
|
|---|
| 230 | $revparts = array_reverse( $parts );
|
|---|
| 231 |
|
|---|
| 232 | $foundid = 0;
|
|---|
| 233 | foreach ( $pages as $page ) {
|
|---|
| 234 | if ( $page->post_name == $revparts[0] ) {
|
|---|
| 235 | $count = 0;
|
|---|
| 236 | if ( $page->post_parent != 0 ) {
|
|---|
| 237 | if ( ! isset( $pages[ $page->post_parent ] ) )
|
|---|
| 238 | continue;
|
|---|
| 239 |
|
|---|
| 240 | $parent_page = $pages[ $page->post_parent ];
|
|---|
| 241 |
|
|---|
| 242 | while ( ! is_null( $parent_page ) ) {
|
|---|
| 243 | $count++;
|
|---|
| 244 | if ( $parent_page->post_name != $revparts[ $count ] )
|
|---|
| 245 | break;
|
|---|
| 246 | if ( isset( $pages[ $parent_page->post_parent ] ) )
|
|---|
| 247 | $parent_page = $pages[ $parent_page->post_parent ];
|
|---|
| 248 | else
|
|---|
| 249 | $parent_page = null;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | if ( is_null( $parent_page ) && $count+1 == count($revparts) ) {
|
|---|
| 253 | $foundid = $page->ID;
|
|---|
| 254 | break;
|
|---|
| 255 | }
|
|---|
| 256 | } else if ( count($revparts) == 1 ) {
|
|---|
| 257 | $foundid = $page->ID;
|
|---|
| 258 | break;
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | if ( $foundid )
|
|---|
| 264 | return $foundid;
|
|---|
| 265 |
|
|---|
| 266 | return null;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | function get_page_by_path_rewritten( $pages, $parts ) {
|
|---|
| 270 | $revparts = array_reverse( $parts );
|
|---|
| 271 |
|
|---|
| 272 | $foundid = 0;
|
|---|
| 273 | foreach ( $pages as $page ) {
|
|---|
| 274 | if ( $page->post_name == $revparts[0] ) {
|
|---|
| 275 | if ( 1 == count( $revparts ) ) {
|
|---|
| 276 | $foundid = $page->ID;
|
|---|
| 277 | break;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | $cur_page = $page;
|
|---|
| 281 | $count = 0;
|
|---|
| 282 | while ( ! empty( $cur_page->post_parent ) && isset( $pages[ $cur_page->post_parent ] ) ) {
|
|---|
| 283 | if ( $cur_page->post_name != $revparts[ $count ] )
|
|---|
| 284 | break;
|
|---|
| 285 |
|
|---|
| 286 | $cur_page = $pages[ $cur_page->post_parent ];
|
|---|
| 287 | $count++;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | if ( empty( $cur_page->post_parent ) && $count+1 == count($revparts) ) {
|
|---|
| 291 | $foundid = $page->ID;
|
|---|
| 292 | break;
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | if ( $foundid )
|
|---|
| 298 | return $foundid;
|
|---|
| 299 |
|
|---|
| 300 | return null;
|
|---|
| 301 | }
|
|---|