Coder Social home page Coder Social logo

go-enum-encoding's People

Contributors

nikolaydubina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

go-enum-encoding's Issues

Performance benchmarks and alternatives to map lookups

This is my follow up from r/golang Reddit I did some benchmarking.

Each unmarshal benchmark iteration parses all 5 values (4x 4-6 characters long and one <empty>) and one bad value (4 characters long) ie. abcd, bcdef, cdefg, defg, <empty> and zabc.
Similar for each marshal benchmark iteration parses all 5 values.

Map lookup is significantly slower than loop for 5 items. Converting bytes to string and comparing is faster than comparing bytes using bytes.Equal because bytes.Equal converts both arguments to string anyway.

Array index is 30x faster than map lookup. Map lookup in MarshalText() and String() functions are even 40+% slower than UnmarshalText.

func BenchmarkMarshalText(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, _ = Abcd.MarshalText()
		_, _ = Bcdef.MarshalText()
		_, _ = Cdefg.MarshalText()
		_, _ = Defg.MarshalText()
		_, _ = UnknownType.MarshalText()
	}
}

func BenchmarkUnmarshalText(b *testing.B) {
	x := MarketType{}
	for i := 0; i < b.N; i++ {
		x.UnmarshalText([]byte("abcd"))
		x.UnmarshalText([]byte("bcdef"))
		x.UnmarshalText([]byte("cdefg"))
		x.UnmarshalText([]byte("defg"))
		x.UnmarshalText([]byte(""))
		x.UnmarshalText([]byte("zabc"))
	}
}
cpu: AMD Ryzen 9 7950X3D 16-Core Processor          
BenchmarkMarshalText-32                 19269684            62.31 ns/op        0 B/op          0 allocs/op
BenchmarkMarshalTextLookup-32           502871172            2.200 ns/op           0 B/op          0 allocs/op
BenchmarkMarshalTextLookupString-32     541060243            2.202 ns/op           0 B/op          0 allocs/op
BenchmarkString-32                      20310391            57.20 ns/op        0 B/op          0 allocs/op
BenchmarkStringLookupString-32          439474542            2.646 ns/op           0 B/op          0 allocs/op
BenchmarkStringLookupBytes-32           100000000           10.97 ns/op        0 B/op          0 allocs/op
BenchmarkStringSwitch-32                372506850            3.955 ns/op           0 B/op          0 allocs/op
BenchmarkStringSwitchValue-32           262102232            3.888 ns/op           0 B/op          0 allocs/op
BenchmarkUnmarshalText-32                          	33958742	        35.89 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalTextLookup-32                    	38068413	        31.15 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalTextLookupString-32              	52166524	        23.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalTextLookupStringAndConvert-32    	31179402	        38.46 ns/op	       0 B/op	       0 allocs/op

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.