Skip to content

Instantly share code, notes, and snippets.

@santagada
Created February 22, 2015 01:14
Show Gist options
  • Save santagada/cbed739f7ddc19c5f6b2 to your computer and use it in GitHub Desktop.
Save santagada/cbed739f7ddc19c5f6b2 to your computer and use it in GitHub Desktop.
<?php
class FakePost {
public function __construct($ID, $post_parent) {
$this->ID = $ID;
$this->post_parent = $post_parent;
}
}
function get_post_children($page_id, $pages) {
$posts_hash = array();
$children_hash = array();
// build a hash of ID -> children
foreach ( (array) $pages as $page ) {
$posts_hash[$page->ID] = $page;
if ( !array_key_exists( $page->post_parent, $children_hash ) ) {
$children_hash[$page->post_parent] = [$page->ID];
} else {
$children_hash[$page->post_parent][] = $page->ID;
}
}
if( array_key_exists( $page_id, $children_hash ) ) {
$pages = array();
$to_look = array_reverse( $children_hash[$page_id] );
// while we still have posts
while ( $to_look ) {
$id = array_pop( $to_look );
$pages[] = $posts_hash[$id];
if ( array_key_exists( $id, $children_hash ) ) {
foreach( array_reverse( $children_hash[$id] ) as $child ) {
$to_look[] = $child;
}
}
}
return $pages;
} else {
return array();
}
}
function ordered_pages($ids, $pages_hash, $children_hash) {
$pages = [];
foreach ($ids as $root) {
$pages[] = $pages_hash[$root];
if(array_key_exists($root, $children_hash)) {
$op = ordered_pages($children_hash[$root], $pages_hash, $children_hash);
foreach($op as $oi){
$pages[] = $oi;
}
}
}
return $pages;
}
function new_get_page_children($page_id, $pages) {
$pages_hash = array();
$children_hash = array();
foreach ( (array) $pages as $page ){
$pages_hash[$page->ID] = $page;
if (!array_key_exists($page->post_parent, $children_hash)) {
$children_hash[$page->post_parent] = [$page->ID];
} else {
$children_hash[$page->post_parent][] = $page->ID;
}
}
if(array_key_exists($page_id, $children_hash))
return ordered_pages($children_hash[$page_id], $pages_hash, $children_hash);
else
return [];
}
function old_get_page_children($page_id, $pages) {
$page_list = array();
foreach ( (array) $pages as $page ) {
if ( $page->post_parent == $page_id ) {
$page_list[] = $page;
if ( $children = old_get_page_children($page->ID, $pages) ) {
$page_list = array_merge($page_list, $children);
}
}
}
return $page_list;
}
function tests() {
$pages = [];
$pages[] = new FakePost(1, 0);
$pages[] = new FakePost(2, 0);
$pages[] = new FakePost(3, 2);
$pages[] = new FakePost(4, 2);
$pages[] = new FakePost(5, 0);
for ($i=6; $i < 1000; $i++) {
$pages[] = new FakePost($i, rand(0, $i-1));
}
$max_depth = 50;
for ($i=1000; $i < (1000 + $max_depth); $i++) {
$pages[] = new FakePost($i, $i-1);
}
for ($i=(1000 + $max_depth); $i < 2000; $i++) {
$pages[] = new FakePost($i, 5);
}
for ($i=2000; $i < 4000; $i++) {
$pages[] = new FakePost($i, 1);
}
for ($i=0; $i < 8; $i++) {
$time = [];
$r = [];
$time_start = microtime(true);
$r[] = new_get_page_children($i, $pages);
$time[] = (int) ((microtime(true) - $time_start) * 1000);
$time_start = microtime(true);
$r[] = get_post_children($i, $pages);
$time[] = (int) ((microtime(true) - $time_start) * 1000);
$time_start = microtime(true);
$r[] = old_get_page_children($i, $pages);
$time[] = (int) ((microtime(true) - $time_start) * 1000);
echo ((($r[0] == $r[2]) && ($r[1]== $r[2]))?'Equal':'Different' );
echo " $time[0] ms (new), $time[1] ms (post_children), $time[2] ms (old)\n";
}
}
tests();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment