Performance deep dive

Hamming
Hamming in Go

In this approach, we'll find out how to most efficiently solve Hamming in Go.

The approaches page lists two idiomatic approaches to this exercise:

  1. Iterating characters as bytes.
  2. Iterating characters as runes.

Benchmarks

To benchmark the approaches, we ran the following Benchmark code for each approach:

func BenchmarkHamming(b *testing.B) {
	// bench combined time to run through all test cases
	for i := 0; i < b.N; i++ {
		for _, tc := range testCases {
			// ignoring errors and results because we're just timing function execution
			_, _ = Distance(tc.s1, tc.s2)
		}
	}
}

and received the following results:

iterate as bytes
BenchmarkHamming-12    	43623610	        26.92 ns/op	       0 B/op	       0 allocs/o

iterate as runes
BenchmarkHamming-12    	 3330171	       361.5 ns/op	      64 B/op	       4 allocs/op

Generally, the fewer bytes allocated per op (B/op) the faster (i.e. the fewer ns/op) the implementation. More info on reading benchmarks can be found here.

The fastest is the iterate as bytes approach. As of the time of this writing we can iterate bytes, since all of the characters are ASCII.

7th Aug 2024 · Found it useful?