Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ewencp/golang-iterators-benchmark

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Benchmarks for a few iterator patterns in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.