-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME
67 lines (57 loc) · 3.49 KB
/
README
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
A simple evaluation of iterators in Go.
Run like this:
go test -bench .
Example output:
goos: darwin
goarch: arm64
pkg: github.com/ewencp/golang-iterators-benchmark
cpu: Apple M2 Max
BenchmarkIntsCallbackIterator
BenchmarkIntsCallbackIterator-12 3093 347019 ns/op
BenchmarkDataCallbackIterator
BenchmarkDataCallbackIterator-12 2554 469291 ns/op
BenchmarkIntsBuiltinPushIterator
BenchmarkIntsBuiltinPushIterator-12 616 1919919 ns/op
BenchmarkDataBuiltinPushIterator
BenchmarkDataBuiltinPushIterator-12 1976 600099 ns/op
BenchmarkIntsBuiltinPullIterator
BenchmarkIntsBuiltinPullIterator-12 27 42105520 ns/op
BenchmarkDataBuiltinPullIterator
BenchmarkDataBuiltinPullIterator-12 27 42632287 ns/op
BenchmarkIntsBuiltinPullManualIterator
BenchmarkIntsBuiltinPullManualIterator-12 618 1922295 ns/op
BenchmarkDataBuiltinPullManualIterator
BenchmarkDataBuiltinPullManualIterator-12 603 1963053 ns/op
BenchmarkIntsChannelIterator
BenchmarkIntsChannelIterator-12 8 138480906 ns/op
BenchmarkDataChannelIterator
BenchmarkDataChannelIterator-12 8 139482448 ns/op
BenchmarkIntsBufferedChannelIterator
BenchmarkIntsBufferedChannelIterator-12 25 46171112 ns/op
BenchmarkDataBufferedChannelIterator
BenchmarkDataBufferedChannelIterator-12 25 45705008 ns/op
BenchmarkIntsClosureIterator
BenchmarkIntsClosureIterator-12 619 1922210 ns/op
BenchmarkDataClosureIterator
BenchmarkDataClosureIterator-12 580 2046098 ns/op
BenchmarkIntStatefulIterator
BenchmarkIntStatefulIterator-12 586 2039062 ns/op
BenchmarkDataStatefulIterator
BenchmarkDataStatefulIterator-12 616 1933832 ns/op
BenchmarkIntStatefulIteratorInterface
BenchmarkIntStatefulIteratorInterface-12 589 2048435 ns/op
BenchmarkDataStatefulIteratorInterface
BenchmarkDataStatefulIteratorInterface-12 612 1925972 ns/op
PASS
ok github.com/ewencp/golang-iterators-benchmark 24.387s
Explanation of iterator types:
CallbackIterator: A callback function is passed in which is invoked once for each element.
BuiltinPushIterator: Uses the 1.23+ iter.Seq function type to use natural-looking for-range loops.
BuiltinPullIterator: Uses the 1.23+ iter.Pull wrapper of the BuiltinPushIterator to do pull-based iteration.
BuiltinPullManualIterator: Implements the pull-based iteration manually, avoiding channels overhead of the iter.Pull wrapper version.
ChannelIterator: Returns a channel as the iterator, using a background goroutine to "generate" the values, allowing for natural looking for-range loop.
BufferedChannelIterator: Same as ChannelIterator but with channeled buffer of 50 elements.
ClosureIterator: Externally, the user gets a function to call which raturns (element, has_next), and should only proceed if has_next is true. Actual iteration is more complex requiring 3-clause for loop or an extra conditional inside the loop body because of this additional check. Closure refers to the internal implementation.
StatefulIterator: Returns an iterator struct, requiring the user to store the variable and perform Next() and Value() method calls to iterate over elements.
StatefulIteratorInterface: Same as StatefulIterator but the returned type is an interface rather than a specific struct type.
Note that the Builtin* iterators are actually doing more than the rest since they allow stopping iteration.