第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

是否有更有效的函數(shù)來查找 []byte 相似性?

是否有更有效的函數(shù)來查找 []byte 相似性?

Go
溫溫醬 2022-06-01 15:06:57
我正在尋找一種有效的方法來查找兩個(gè)字節(jié)片之間的前綴相似性。我目前正在使用它,但如果可能的話,我正在尋找一種更有效的方法。謝謝你。s1 -> [0 15 136 96 88 76 0 0 0 1] s2 -> [0 15 136 96 246 1 255 255 255 255]output -> [0 15 136 96] func bytesSimilar(s1 []byte, s2 []byte) []byte {    for !bytes.Equal(s1,s2) {        s1 = s1[:len(s1)-1]        s2 = s2[:len(s2)-1]    }    return s1}基準(zhǔn)代碼:func BenchmarkBytePrefix200(b *testing.B) {    s1 := []byte{0, 15, 136, 96, 88, 76, 0, 0, 0, 1}    s2 := []byte{0, 15, 136, 96, 246, 1, 255, 255, 255, 255}    b.ReportAllocs()    b.ResetTimer()    for i := 0; i < b.N; i++ {        bytePrefix(s1, s2)    }}MBP 的結(jié)果:BenchmarkBytePrefix200-8    48738078            29.5 ns/op         0 B/op          0 allocs/op
查看完整描述

2 回答

?
FFIVE

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

我認(rèn)為,從您上面的代碼來看,以下部分在 I/O 資源上非常昂貴


s1 = s1[:len(s1)-1]

s2 = s2[:len(s2)-1]

我們實(shí)際上可以做一個(gè)簡單的循環(huán)并在找到不同的字節(jié)時(shí)提前退出。使用這種方法,我們不需要太多的內(nèi)存分配過程。它的代碼行數(shù)更多,但性能更好。


代碼如下


func bytesSimilar2(s1 []byte, s2 []byte) []byte {

    l1 := len(s1)

    l2 := len(s2)

    least := l1

    if least > l2 {

        least = l2

    }

    count := 0

    for i := 0; i < least; i++ {

        if s1[i] == s2[i] {

            count++

            continue

        }

        break

    }

    if count == 0 {

        return []byte{}

    }

    return s1[:count]

}


func BenchmarkBytePrefix200v1(b *testing.B) {

    s1 := []byte{0, 15, 136, 96, 88, 76, 0, 0, 0, 1}

    s2 := []byte{0, 15, 136, 96, 246, 1, 255, 255, 255, 255}

    b.ReportAllocs()

    b.ResetTimer()

    for i := 0; i < b.N; i++ {

        bytesSimilar1(s1, s2)

    }

}


func BenchmarkBytePrefix200v2(b *testing.B) {

    s1 := []byte{0, 15, 136, 96, 88, 76, 0, 0, 0, 1}

    s2 := []byte{0, 15, 136, 96, 246, 1, 255, 255, 255, 255}

    b.ReportAllocs()

    b.ResetTimer()

    for i := 0; i < b.N; i++ {

        bytesSimilar2(s1, s2)

    }

}

比較結(jié)果如下,38.7ns/op vs 7.40ns/op


goos: darwin

goarch: amd64

pkg: git.kanosolution.net/kano/acl

BenchmarkBytePrefix200v1-8      27184414                38.7 ns/op             0 B/op          0 allocs/op

BenchmarkBytePrefix200v2-8      161031307                7.40 ns/op            0 B/op          0 allocs/op

PASS


查看完整回答
反對 回復(fù) 2022-06-01
?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果與您的問題bytePrefix相同:bytesSimilar


func BytesSimilarNew(s1 []byte, s2 []byte) []byte {

    for i := 0; i < len(s1); i++ {

        if s1[i] ^ s2[i] > 0 {

            return s1[:i]

        }

    }

    return []byte{}

}

然后比較:


BenchmarkBytePrefix200

BenchmarkBytePrefix200-8        28900861            36.5 ns/op         0 B/op          0 allocs/op

BenchmarkByteSimilarNew200

BenchmarkByteSimilarNew200-8    237646268            5.06 ns/op        0 B/op          0 allocs/op

PASS


查看完整回答
反對 回復(fù) 2022-06-01
  • 2 回答
  • 0 關(guān)注
  • 104 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號