-
Notifications
You must be signed in to change notification settings - Fork 1
/
example-1.php
93 lines (75 loc) · 1.99 KB
/
example-1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
//instantiate the list
$ll = new JDoublyLinkedList();
//First node goes after the head.
$firstNode = $ll->createNode('First Node');
$ll->addFirst($firstNode);
//10 more nodes
for($i = 0; $i < 5; $i++) {
$node = $ll->createNode('Node-' . $i);
$ll->addNext($firstNode, $node);
$firstNode = $node;
}
//and this is how we iterate...iterate
$it = new JLinkedListIterator($ll);
foreach($it as $k => $v) {
echo '<pre>', $k, ': ', print_r($v, true), '</pre>';
}
unset($ll, $it);
echo 'Peak: ' . number_format(memory_get_peak_usage(), 0, '.', ',') . " bytes<br>";
echo 'End: ' . number_format(memory_get_usage(), 0, '.', ',') . " bytes<br>";
/*
The output would be something like this:
[sourcecode language="HTML"]
Initial: 126,360 bytes
4d7f677322b38: JNode Object
(
[_value:private] => First Node
[_previous:private] => 4d7f677322b06
[_next:private] => 4d7f677322b69
[_uid:private] => 4d7f677322b38
)
4d7f677322b69: JNode Object
(
[_value:private] => Node-0
[_previous:private] => 4d7f677322b38
[_next:private] => 4d7f677322b95
[_uid:private] => 4d7f677322b69
)
4d7f677322b95: JNode Object
(
[_value:private] => Node-1
[_previous:private] => 4d7f677322b69
[_next:private] => 4d7f677322bbf
[_uid:private] => 4d7f677322b95
)
4d7f677322bbf: JNode Object
(
[_value:private] => Node-2
[_previous:private] => 4d7f677322b95
[_next:private] => 4d7f677322be8
[_uid:private] => 4d7f677322bbf
)
4d7f677322be8: JNode Object
(
[_value:private] => Node-3
[_previous:private] => 4d7f677322bbf
[_next:private] => 4d7f677322c12
[_uid:private] => 4d7f677322be8
)
4d7f677322c12: JNode Object
(
[_value:private] => Node-4
[_previous:private] => 4d7f677322be8
[_next:private] => 4d7f677322b1b
[_uid:private] => 4d7f677322c12
)
Peak: 175,952 bytes
End: 135,080 bytes
[/sourcecode]
Notice that the cleanup was not bad. If I had to create 1000 nodes then this is what the memory utilization looks like:
Initial: 126,376 bytes
Peak: 803,464 bytes
End: 192,440 bytes
That's pretty decent in terms of GC but there is still room for improvement.
*/