Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmarks for bytes/atom comparisons
Yup, atoms are great!
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
package html | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHashTable(t *testing.T) { | ||
assert.Equal(t, ToHash([]byte("address")), Address, "'address' must resolve to hash.Address") | ||
assert.Equal(t, "address", Address.String(), "hash.Address must resolve to 'address'") | ||
assert.Equal(t, "accept-charset", Accept_Charset.String(), "hash.Accept_Charset must resolve to 'accept-charset'") | ||
} | ||
|
||
//////////////////////////////////////////////////////////////// | ||
|
||
var result int | ||
|
||
// naive scenario | ||
func BenchmarkCompareBytes(b *testing.B) { | ||
var r int | ||
val := []byte("span") | ||
for n := 0; n < b.N; n++ { | ||
if bytes.Equal(val, []byte("span")) { | ||
r++ | ||
} | ||
} | ||
result = r | ||
} | ||
|
||
// using-atoms scenario | ||
func BenchmarkFindAndCompareAtom(b *testing.B) { | ||
var r int | ||
val := []byte("span") | ||
for n := 0; n < b.N; n++ { | ||
if ToHash(val) == Span { | ||
r++ | ||
} | ||
} | ||
result = r | ||
} | ||
|
||
// using-atoms worst-case scenario | ||
func BenchmarkFindAtomCompareBytes(b *testing.B) { | ||
var r int | ||
val := []byte("zzzz") | ||
for n := 0; n < b.N; n++ { | ||
if h := ToHash(val); h == 0 && bytes.Equal(val, []byte("zzzz")) { | ||
r++ | ||
} | ||
} | ||
result = r | ||
} |