<pre>
<?php

error_reporting( E_ALL );
set_time_limit(0);

$trials = 200000;

$data = array(
	array(
		'result' => null,
		'inputs' => array(
			'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";}}',
			'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-4";}',
		),
	),
	array(
		'result' => null,
		'inputs' => array(
			'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";}}',
			'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-1";}',
		),
	),
	array(
		'result' => null,
		'inputs' => array(
			'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";}}',
			'a:3:{i:0;s:7:"level-5";i:1;s:7:"level-2";i:2;s:7:"level-3";}',
		),
	),
	array(
		'result' => 172,
		'inputs' => array(
			'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";}}',
			'a:3:{i:0;s:7:"level-1";i:1;s:7:"level-2";i:2;s:7:"level-3";}',
		),
	),
	array(
		'result' => 174,
		'inputs' => array(
			'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";}}',
			'a:1:{i:0;s:7:"level-1";}',
		),
	),
	array(
		'result' => 600,
		'inputs' => array(
			'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";}}',
			'a:1:{i:0;s:4:"blog";}',
		),
	),
	array(
		'result' => 568,
		'inputs' => array(
			'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";}}',
			'a:2:{i:0;s:5:"about";i:1;s:17:"about-da-children";}',
		),
	),
	array(
		'result' => 145,
		'inputs' => array(
			'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";}}',
			'a:2:{i:0;s:11:"parent-page";i:1;s:12:"child-page-2";}',
		),
	),
);

$functions = array( 'get_page_by_path', 'get_page_by_path_rewritten', 'get_page_by_path_dfix' ); //'get_page_by_path_simple_fix'
//$functions = array( 'get_page_by_path_dfix' );


foreach ( $data as $index => $set )
	foreach ( $set['inputs'] as $input_index => $input )
		$data[$index]['inputs'][$input_index] = unserialize( $input );

foreach ( $functions as $function ) {
	$errors = 0;
	
	foreach ( $data as $index => $set ) {
		$result = @call_user_func_array( $function, $set['inputs'] );
		
		if ( $result != $set['result'] ) {
			echo "$function failed test set $index\n";
			echo "\tExpected result: {$set['result']}\n";
			echo "\tActual result:   ";
			var_export( $result );
			
			$errors++;
		}
	}
	
	if ( 0 == $errors )
		echo "$function passed all tests.\n";
	
	echo "\n";
}

$results = array();

for ( $count = 0; $count < $trials; $count++ ) {
	$function = $functions[ rand( 0, count( $functions ) - 1 ) ];
	
	if ( ! isset( $results[$function] ) ) {
		$results[$function] = array(
			'trials'		=> 0,
			'total_time'	=> 0,
			'trial_time'	=> array(),
			'total_count'	=> 0,
		);
	}
	
	run_trial( $function );
}

foreach ( $functions as $function ) {
	$result = $results[$function];
	
	echo "$function\n";
	echo "==========================\n";
	echo "Trials:           {$result['trials']}\n";
	echo "Total Time:       {$result['total_time']}\n";
	echo "Average Time:     " . ( $result['total_time'] / $result['trials'] ) . "\n";
	echo "Runs per Sec:     " . ( 1 / ( $result['total_time'] / $result['trials'] ) ) . "\n";
	
	if ( isset( $results['no_cast']['total_time'] ) )
		echo "Speed Difference: " . ( ( $result['total_time'] - $results['no_cast']['total_time'] ) / $results['no_cast']['total_time'] ) . "\n";
	
	echo "\n";
}



function run_trial( $function, $count = 10 ) {
	global $results, $data;
	
	
	$time = 0;
	$total_count = 0;
	
	for ( $i = 0; $i < $count; $i++ ) {
		$args = $data[ rand( 0, count( $data ) - 1 ) ]['inputs'];
		
		$start = microtime( true );
		@call_user_func_array( $function, $args );
		$end = microtime( true );
		
		$time += $end - $start;
		
		$total_count++;
	}
	
	
	$results[$function]['trials']++;
	$results[$function]['total_time'] += $time;
	$results[$function]['total_count'] += $total_count;
}


function get_page_by_path( $pages, $parts ) {
	$revparts = array_reverse( $parts );

	$foundid = 0;
	foreach ( $pages as $page ) {
		if ( $page->post_name == $revparts[0] ) {
			$count = 0;
			if ( $page->post_parent != 0 ) {
				if ( null === ( $parent_page = $pages[ $page->post_parent ] ) )
					continue;

				while ( $parent_page->ID != 0 ) {
					$count++;
					if ( $parent_page->post_name != $revparts[ $count ] )
						break;
					$parent_page = $pages[ $parent_page->post_parent ];
				}

				if ( $parent_page->ID == 0 && $count+1 == count($revparts) ) {
					$foundid = $page->ID;
					break;
				}
			} else if ( count($revparts) == 1 ) {
				$foundid = $page->ID;
				break;
			}
		}
	}

	if ( $foundid )
		return $foundid;

	return null;
}

function get_page_by_path_dfix($pages, $parts) {

	$revparts = array_reverse( $parts );
	$revparts_count = count($revparts);

	if ( empty($revparts) )
		return null;

	if ( 1 == $revparts_count ) {
		foreach ( $pages as $page ) {
			if ( $page->post_name == $revparts[0] )
				return $page->ID;
		}
		return null;
	}

	foreach ( $pages as $page ) {
		if ( $page->post_name == $revparts[0] && $page->post_parent !=  0) {
			$cur_page = $page;
			$count = 0;
			while ( isset($pages[ $cur_page->post_parent ]) ) {
				if ( $cur_page->post_name != $revparts[ $count ] )
					continue 2; // Continue next foreach()
				$count++;
				$cur_page = $pages[ $cur_page->post_parent ];
			}

			if ( $count+1 == $revparts_count && $cur_page->post_name == $revparts[$count] )
				return $page->ID;
		}
	}

	return null;
}

function get_page_by_path_simple_fix( $pages, $parts ) {
	$revparts = array_reverse( $parts );

	$foundid = 0;
	foreach ( $pages as $page ) {
		if ( $page->post_name == $revparts[0] ) {
			$count = 0;
			if ( $page->post_parent != 0 ) {
				if ( ! isset( $pages[ $page->post_parent ] ) )
					continue;

				$parent_page = $pages[ $page->post_parent ];

				while ( ! is_null( $parent_page ) ) {
					$count++;
					if ( $parent_page->post_name != $revparts[ $count ] )
						break;
					if ( isset( $pages[ $parent_page->post_parent ] ) )
						$parent_page = $pages[ $parent_page->post_parent ];
					else
						$parent_page = null;
				}

				if ( is_null( $parent_page ) && $count+1 == count($revparts) ) {
					$foundid = $page->ID;
					break;
				}
			} else if ( count($revparts) == 1 ) {
				$foundid = $page->ID;
				break;
			}
		}
	}

	if ( $foundid )
		return $foundid;

	return null;
}

function get_page_by_path_rewritten( $pages, $parts ) {
	$revparts = array_reverse( $parts );

	$foundid = 0;
	foreach ( $pages as $page ) {
		if ( $page->post_name == $revparts[0] ) {
			if ( 1 == count( $revparts ) ) {
					$foundid = $page->ID;
					break;
			}
			
			$cur_page = $page;
			$count = 0;
			while ( ! empty( $cur_page->post_parent ) && isset( $pages[ $cur_page->post_parent ] ) ) {
					if ( $cur_page->post_name != $revparts[ $count ] )
						break;

					$cur_page = $pages[ $cur_page->post_parent ];
					$count++;
			}

			if ( empty( $cur_page->post_parent ) && $count+1 == count($revparts) ) {
					$foundid = $page->ID;
					break;
			}
		}
	}

	if ( $foundid )
			return $foundid;

	return null;
}
