Optimize fingerprint comparison time.

This commit is contained in:
Julius Volz 2013-05-16 17:16:39 +02:00
parent da0257bb0e
commit 259a0ea467
2 changed files with 67 additions and 1 deletions

View File

@ -152,7 +152,37 @@ func (f fingerprint) LastCharacterOfLastLabelValue() string {
}
func (f fingerprint) Less(o Fingerprint) bool {
return f.String() < o.String()
// BUG(julius): Deprecate Fingerprint interface and clean this up.
fp := o.(fingerprint)
if f.hash < fp.hash {
return true
}
if f.hash > fp.hash {
return false
}
if f.firstCharacterOfFirstLabelName < fp.firstCharacterOfFirstLabelName {
return true
}
if f.firstCharacterOfFirstLabelName > fp.firstCharacterOfFirstLabelName {
return false
}
if f.labelMatterLength < fp.labelMatterLength {
return true
}
if f.labelMatterLength > fp.labelMatterLength {
return false
}
if f.lastCharacterOfLastLabelValue < fp.lastCharacterOfLastLabelValue {
return true
}
if f.lastCharacterOfLastLabelValue > fp.lastCharacterOfLastLabelValue {
return false
}
return false
}
func (f fingerprint) Equal(o Fingerprint) (equal bool) {

View File

@ -14,6 +14,7 @@
package model
import (
"runtime"
"testing"
)
@ -66,3 +67,38 @@ func TestFingerprintComparison(t *testing.T) {
}
}
}
func BenchmarkFingerprinting(b *testing.B) {
b.StopTimer()
fps := []fingerprint{
{
hash: 0,
firstCharacterOfFirstLabelName: "a",
labelMatterLength: 2,
lastCharacterOfLastLabelValue: "z",
},
{
hash: 0,
firstCharacterOfFirstLabelName: "a",
labelMatterLength: 2,
lastCharacterOfLastLabelValue: "z",
},
}
for i := 0; i < 10; i++ {
fps[0].Less(fps[1])
}
b.Logf("N: %v", b.N)
b.StartTimer()
var pre runtime.MemStats
runtime.ReadMemStats(&pre)
for i := 0; i < b.N; i++ {
fps[0].Less(fps[1])
}
var post runtime.MemStats
runtime.ReadMemStats(&post)
b.Logf("allocs: %d items: ", post.TotalAlloc-pre.TotalAlloc)
}