In this approach, we'll find out how to most efficiently solve ISBN Verifier in Go.
The approaches page lists two idiomatic approaches to this exercise:
Benchmarks
To benchmark the approaches, we ran the following Benchmark code for each approach:
func BenchmarkIsValidISBN(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
for _, n := range testCases {
IsValidISBN(n.isbn)
}
}
}
and received the following results:
iterate as bytes
BenchmarkIsValidISBN-12 4561827 253.0 ns/op 0 B/op 0 allocs/op
iterate as runes
BenchmarkIsValidISBN-12 3094387 354.9 ns/op 0 B/op 0 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?