-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjs.php
More file actions
129 lines (102 loc) · 2.8 KB
/
objs.php
File metadata and controls
129 lines (102 loc) · 2.8 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
// performance test
class Token
{
public $text;
public $type;
private $prev;
private $next;
public function __construct($text, $type = null)
{
$this->text = $text;
$this->type = $type;
}
public function append(Token $other)
{
assert(!$other->prev);
if ($other->next) {
$last = $other->findItemBefore(null);
} else {
$last = $other;
}
if ($this->next) {
$this->next->prev = $last;
}
$last->next = $this->next;
$other->prev = $this;
$this->next = $other;
}
public function prepend(Token $other)
{
assert(!$other->next);
if ($other->prev) {
$first = $this->findItemAfter(null);
} else {
$first = $other;
}
if ($this->prev) {
$this->prev->next = $first;
}
$first->prev = $this->prev;
$other->next = $this;
$this->prev = $other;
}
public function remove()
{
$this->removeChain(null);
}
public function removeChain(Token $end = null)
{
if ($end) {
assert(!!$this->findItemBefore($end));
} else {
$end = $this;
}
if ($end->next) {
$end->next->prev = $this->prev;
$end->next = null;
}
if ($this->prev) {
$this->prev->next = $end->next;
$this->prev = null;
}
}
private function findItemBefore(Token $needle = null)
{
for ($p = $this; $p->next; $p = $p->next) {
if ($p->next === $needle) {
return $p;
}
}
return $needle === null ? $p : null;
}
private function findItemAfter(Token $needle = null)
{
for ($p = $this; $p->prev; $p = $p->prev) {
if ($p->prev === $needle) {
return $p;
}
}
return $needle === null ? $p : null;
}
}
// Mem test
$t0 = microtime(true);
$count = 100000;
for ($i = 0; $i < $count; $i++) {
$tokens[] = new Token((string)$i, 1);
}
$t1 = microtime(true);
printf("Took %.3f ms to make %d tokens\n", ($t1 - $t0) * 1000, $count);
printf("Took %.3f Mb of memeory\n", memory_get_usage(true) / 1024 / 1024);
// Free
$tokens = null;
printf("Using %.3f Mb of memeory\n", memory_get_usage(true) / 1024 / 1024);
$t0 = microtime(true);
$count = 100000;
for ($i = 0; $i < $count; $i++) {
$tokens[] = array((string)$i, 1);
}
$t1 = microtime(true);
printf("Took %.3f ms to make %d tokens\n", ($t1 - $t0) * 1000, $count);
printf("Took %.3f Mb of memeory\n", memory_get_usage(true) / 1024 / 1024);