Skip to content

Commit a1b9e95

Browse files
authored
improve performance of array list (#19)
1 parent 815c6dc commit a1b9e95

10 files changed

+459
-136
lines changed

docs/arraylist_README.md

+83-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (usr *user) Equal(val interface{}) bool {
3131
}
3232

3333
func main() {
34-
list := InitArrayList[*user]()
34+
userList := InitArrayList[*user]()
3535
users := []*user{
3636
&user{
3737
id: 1,
@@ -46,35 +46,35 @@ func main() {
4646
if err != nil{
4747
panic(fmt.Errorf("Error in adding at 0th index: %s", err.Error()))
4848
}
49-
data, err := list.Get(0)
49+
data, err := userList.Get(0)
5050
if err != nil{
5151
panic(fmt.Errorf("Error in getting value at a index: %s", err.Error()))
5252
}
53-
err := list.Set(0, &user{
53+
err := userList.Set(0, &user{
5454
id: 3,
5555
name: "test user 3",
5656
})
5757
if err != nil {
5858
panic(fmt.Errorf("Error in setting value at a index: %s", err.Error()))
5959
}
60-
index, err := list.Remove(users[1])
60+
index, err := userList.Remove(users[1])
6161
if err != nil {
6262
panic(fmt.Errorf("error when removing a node: %s", err.Error()))
6363
}
6464

65-
data, err := list.RemoveAtIndex(1)
65+
data, err := userList.RemoveAtIndex(1)
6666
if err != nil {
6767
panic(fmt.Errorf("error when removing a node at a index: %s", err.Error()))
6868
}
6969

70-
index := list.Find(&user{
70+
index := userList.Find(&user{
7171
id: 2,
7272
name: "test name 2",
7373
})
7474
if index == -1 {
7575
panic(fmt.Errorf("error when finding value: %s", err.Error()))
7676
}
77-
response := list.Filter(func(val *user) bool {
77+
response := userList.Filter(func(val *user) bool {
7878
return val.id == 1
7979
})
8080
}
@@ -129,3 +129,79 @@ This function helps to finding all elements for which the returns true for f, an
129129

130130
This function helps to remove all elements for which the returns true for f, and also returns all removed elements.
131131

132+
133+
## Benchmarks
134+
135+
This benchmarking was done against [emirpasic's GODS package](https://github.com/emirpasic/gods)
136+
137+
**System configuration used while doing these benchmark**<br />
138+
**goos:** darwin<br />
139+
**goarch:** arm64<br />
140+
**pkg:** github.com/architagr/golang_collections/test/benchmark<br />
141+
**cpu:** Apple M1 Pro<br />
142+
143+
<table>
144+
<thead>
145+
<tr>
146+
<td>Function name</td>
147+
<td>Package name</td>
148+
<td># operation</td>
149+
<td>Time taken per operation</td>
150+
<td></td>
151+
<td></td>
152+
</tr>
153+
</thead>
154+
<tbody>
155+
<tr >
156+
157+
</tr>
158+
<tr>
159+
<td rowspan=2>Add</td>
160+
<td>architagr/golang_collections</td>
161+
<td>74,762,736</td>
162+
<td>21.34 ns/op</td>
163+
<td>22 B/op</td>
164+
<td>0 allocs/op</td>
165+
</tr>
166+
<tr>
167+
<td>emirpasic/gods</td>
168+
<td>43,898,088</td>
169+
<td>26.29 ns/op</td>
170+
<td>48 B/op</td>
171+
<td>0 allocs/op</td>
172+
</tr>
173+
<tr>
174+
<td rowspan=2>AddAtIndex</td>
175+
<td>architagr/golang_collections</td>
176+
<td>363,227,274</td>
177+
<td>3.300</td>
178+
<td>0 B/op</td>
179+
<td>0 allocs/op</td>
180+
</tr>
181+
<tr>
182+
<td>emirpasic/gods</td>
183+
<td>812,377</td>
184+
<td>262970 ns/op</td>
185+
<td>41 B/op</td>
186+
<td>0 allocs/op</td>
187+
</tr>
188+
189+
<tr>
190+
<td rowspan=2>Find/IndexOf</td>
191+
<td>architagr/golang_collections</td>
192+
<td>99,529,309</td>
193+
<td>11.59</td>
194+
<td>0 B/op</td>
195+
<td>0 allocs/op</td>
196+
</tr>
197+
<tr>
198+
<td>emirpasic/gods <br/>
199+
<br/>
200+
issue found when we use pointer data as elements: https://github.com/emirpasic/gods/issues/269</td>
201+
<td>415,523,194</td>
202+
<td>2.888 ns/op</td>
203+
<td>0 B/op</td>
204+
<td>0 allocs/op</td>
205+
</tr>
206+
</tbody>
207+
</table>

docs/queue_README.md

+40-10
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,45 @@ This function returns ture if the queue is empty, i.e. count is 0, and return fa
6969

7070
## Benchmarks
7171

72-
This benchmarking was done against [emirpasic's GODS package](https://github.com/emirpasic/gods)
72+
This benchmarking was done against [emirpasic's GODS package (v1.18.1)](https://github.com/emirpasic/gods)
7373

7474
**System configuration used while doing these benchmark**<br />
75-
**goos:** linux<br />
76-
**goarch:** amd64<br />
77-
**pkg:** github.com/architagr/golang_collections/queue<br />
78-
**cpu:** Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz<br />
79-
80-
| Benchmark name | (1) | (2) | (3) | (4) |
81-
| ------------------------------------ | -------------:| ---------------:| -------------:| ---------------:|
82-
| BenchmarkGolang_collections_llQueue | **100000000** | **158.7 ns/op** | **16 B/op** | **1 allocs/op** |
83-
| BenchmarkEmirpasicGods_llQueue | 100000000 | 172.0 ns/op | 31 B/op | 1 allocs/op |
75+
**goos:** darwin<br />
76+
**goarch:** arm64<br />
77+
**pkg:** github.com/architagr/golang_collections/test/benchmark<br />
78+
**cpu:** Apple M1 Pro<br />
79+
80+
81+
<table>
82+
<thead>
83+
<tr>
84+
<td>Function name</td>
85+
<td>Package name</td>
86+
<td># operation</td>
87+
<td>Time taken per operation</td>
88+
<td></td>
89+
<td></td>
90+
</tr>
91+
</thead>
92+
<tbody>
93+
<tr >
94+
95+
</tr>
96+
<tr>
97+
<td rowspan=2>Enqueue</td>
98+
<td>architagr/golang_collections</td>
99+
<td>30,853,053</td>
100+
<td>43.30 ns/op</td>
101+
<td>16 B/op</td>
102+
<td>1 allocs/op</td>
103+
</tr>
104+
<tr>
105+
<td>emirpasic/gods</td>
106+
<td>19,681,999</td>
107+
<td>64.10 ns/op</td>
108+
<td>31 B/op</td>
109+
<td>1 allocs/op</td>
110+
</tr>
111+
112+
</tbody>
113+
</table>

docs/stack_README.md

+40-10
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,45 @@ This function returns ture if the stack is empty, i.e. count is 0, and return fa
6969

7070
## Benchmarks
7171

72-
This benchmarking was done against [emirpasic's GODS package](https://github.com/emirpasic/gods)
72+
This benchmarking was done against [emirpasic's GODS package (v1.18.1)](https://github.com/emirpasic/gods)
7373

7474
**System configuration used while doing these benchmark**<br />
75-
**goos:** linux<br />
76-
**goarch:** amd64<br />
77-
**pkg:** github.com/architagr/golang_collections/stack<br />
78-
**cpu:** Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz<br />
79-
80-
| Benchmark name | (1) | (2) | (3) | (4) |
81-
| ------------------------------------ | -------------:| ---------------:| -------------:| ---------------:|
82-
| BenchmarkGolang_collections_llStack | **100000000** | **109.0 ns/op** | **16 B/op** | **1 allocs/op** |
83-
| BenchmarkEmirpasicGods_llStack | 100000000 | 143.0 ns/op | 31 B/op | 1 allocs/op |
75+
**goos:** darwin<br />
76+
**goarch:** arm64<br />
77+
**pkg:** github.com/architagr/golang_collections/test/benchmark<br />
78+
**cpu:** Apple M1 Pro<br />
79+
80+
81+
<table>
82+
<thead>
83+
<tr>
84+
<td>Function name</td>
85+
<td>Package name</td>
86+
<td># operation</td>
87+
<td>Time taken per operation</td>
88+
<td></td>
89+
<td></td>
90+
</tr>
91+
</thead>
92+
<tbody>
93+
<tr >
94+
95+
</tr>
96+
<tr>
97+
<td rowspan=2>Push</td>
98+
<td>architagr/golang_collections</td>
99+
<td>23,863,994</td>
100+
<td>46.03 ns/op</td>
101+
<td>16 B/op</td>
102+
<td>1 allocs/op</td>
103+
</tr>
104+
<tr>
105+
<td>emirpasic/gods</td>
106+
<td>20,692,017</td>
107+
<td>61.07 ns/op</td>
108+
<td>31 B/op</td>
109+
<td>1 allocs/op</td>
110+
</tr>
111+
112+
</tbody>
113+
</table>

0 commit comments

Comments
 (0)