1 | <?php |
---|
2 | |
---|
3 | class FakePost { |
---|
4 | public function __construct($ID, $post_parent) { |
---|
5 | $this->ID = $ID; |
---|
6 | $this->post_parent = $post_parent; |
---|
7 | } |
---|
8 | } |
---|
9 | |
---|
10 | |
---|
11 | function get_page_children( $page_id, $pages ) { |
---|
12 | $post_hash = array(); |
---|
13 | $children_hash = array(); |
---|
14 | |
---|
15 | // build a hash of ID -> children |
---|
16 | foreach ( (array) $pages as $page ) { |
---|
17 | $post_hash[$page->ID] = $page; |
---|
18 | if ( !array_key_exists( $page->post_parent, $children_hash ) ) { |
---|
19 | $children_hash[$page->post_parent] = [$page->ID]; |
---|
20 | } else { |
---|
21 | $children_hash[$page->post_parent][] = $page->ID; |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | if( array_key_exists( $page_id, $children_hash ) ) { |
---|
26 | $post_list = array(); |
---|
27 | $to_look = array_reverse( $children_hash[$page_id] ); |
---|
28 | // while we still have posts to_look add them to the list |
---|
29 | while ( $to_look ) { |
---|
30 | $id = array_pop( $to_look ); |
---|
31 | $post_list[] = $post_hash[$id]; |
---|
32 | if ( array_key_exists( $id, $children_hash ) ) { |
---|
33 | foreach( array_reverse( $children_hash[$id] ) as $child ) { |
---|
34 | $to_look[] = $child; |
---|
35 | } |
---|
36 | } |
---|
37 | } |
---|
38 | return $post_list; |
---|
39 | } else { |
---|
40 | return array(); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | function old_get_page_children($page_id, $pages) { |
---|
45 | $page_list = array(); |
---|
46 | foreach ( (array) $pages as $page ) { |
---|
47 | if ( $page->post_parent == $page_id ) { |
---|
48 | $page_list[] = $page; |
---|
49 | if ( $children = old_get_page_children($page->ID, $pages) ) { |
---|
50 | $page_list = array_merge($page_list, $children); |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | return $page_list; |
---|
55 | } |
---|
56 | |
---|
57 | function tests() { |
---|
58 | $pages = []; |
---|
59 | $pages[] = new FakePost(1, 0); |
---|
60 | $pages[] = new FakePost(2, 0); |
---|
61 | $pages[] = new FakePost(3, 2); |
---|
62 | $pages[] = new FakePost(4, 2); |
---|
63 | $pages[] = new FakePost(5, 0); |
---|
64 | for ($i=6; $i < 1000; $i++) { |
---|
65 | $pages[] = new FakePost($i, rand(0, $i-1)); |
---|
66 | } |
---|
67 | $max_depth = 50; |
---|
68 | for ($i=1000; $i < (1000 + $max_depth); $i++) { |
---|
69 | $pages[] = new FakePost($i, $i-1); |
---|
70 | } |
---|
71 | for ($i=(1000 + $max_depth); $i < 2000; $i++) { |
---|
72 | $pages[] = new FakePost($i, 5); |
---|
73 | } |
---|
74 | for ($i=2000; $i < 4000; $i++) { |
---|
75 | $pages[] = new FakePost($i, 1); |
---|
76 | } |
---|
77 | |
---|
78 | for ($i=0; $i < 8; $i++) { |
---|
79 | $time = []; |
---|
80 | $r = []; |
---|
81 | |
---|
82 | $time_start = microtime(true); |
---|
83 | $r[] = get_page_children($i, $pages); |
---|
84 | $time[] = (int) ((microtime(true) - $time_start) * 1000); |
---|
85 | |
---|
86 | $time_start = microtime(true); |
---|
87 | $r[] = old_get_page_children($i, $pages); |
---|
88 | $time[] = (int) ((microtime(true) - $time_start) * 1000); |
---|
89 | |
---|
90 | echo ($r[0] == $r[1])?'Equal':'Different'; |
---|
91 | echo " $time[0] ms (new), $time[1] ms (old)\n"; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | tests(); |
---|