duangengruan2144 2016-06-10 17:15
浏览 20
已采纳

核心语言中的字符串比较

Taking this simple comparison loopValue == "Firstname", is the following statement true?

If the internal operand inspecting the first char does not match the compared string, it will early abort

So taking the rawer form loopValue and "Firstname" are both []byte. And it would walk the array kind of like so as callback loop for truth:

someInspectionFunc(loopValue, "Firstname", func(charA, charB) {
    return charA == charB
})

... making it keep on going until it bumps false and checks if the number of iterations was equal to both their lengths. Also does it check length first?

if len(loopValue) != len("Firstname") {
    return false
}

I can't really find an explanation in the go source-code on GitHub as it's a bit above me.

The reason I'm asking this is because I'm doing big data processing and am benchmarking and doing cpu, memory and allocation pprof to squeeze some more juice out of the process. From that process it kind of made me think how Go (but also just C in general) would do this under the hood. Is this fully on an assembly level or does the comparison already happen in native Go code (kind of like sketched in the snippets above)?

Please let me know if I'm being too vague or if I missed something. Thank you

Update

When I did a firstCharater match in big strings of json, before really comparing I got about 3.7% benchmarking gain on 100k heavy entries:

<some irrelevant inspection code>.. v[0] == firstChar && v == lookFor {
    // Match found when it reaches here
}

the code above (especially on long strings) is faster than just going for v == lookFor.

  • 写回答

1条回答 默认 最新

  • drdu53813 2016-06-10 17:24
    关注

    The function is handled in assembly. The amd64 version is:

    TEXT runtime·eqstring(SB),NOSPLIT,$0-33
        MOVQ    s1str+0(FP), SI
        MOVQ    s2str+16(FP), DI
        CMPQ    SI, DI
        JEQ eq
        MOVQ    s1len+8(FP), BX
        LEAQ    v+32(FP), AX
        JMP runtime·memeqbody(SB)
    eq:
        MOVB    $1, v+32(FP)
        RET
    

    And it's the compiler's job to ensure that the strings are of equal length before that is called. (The runtime·memeqbody function is actually where the optimized memory comparisons happen, but there's probably no need to post the full text here)

    The equivalent Go code would be:

    func eqstring_generic(s1, s2 string) bool {
        if len(s1) != len(s2) {
            return false
        }
        for i := 0; i < len(s1); i++ {
            if s1[i] != s2[i] {
                return false
            }
        }
        return true
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题