compare of bytes or string?

166 views
Skip to first unread message

xie cui

unread,
Feb 10, 2021, 5:12:18 AM2/10/21
to golang-nuts
compare two byte slice,will convert to compare between two string, and my question is how golang compare two string, i don't think that it just compare the len and then comapare bytes in string one byte by one byte, it should have some optimization?where is the code in compiler generate the compare codes?

Amnon

unread,
Feb 10, 2021, 5:21:46 AM2/10/21
to golang-nuts
Matt Godblolt's web app is a fun way to explore these question.
https://godbolt.org/z/q6oz64

xie cui

unread,
Feb 10, 2021, 5:22:28 AM2/10/21
to golang-nuts
i look into map key compare key about string,it will use runtime·memequal, i thing the stra == strb will call memequal  two.

tapi...@gmail.com

unread,
Feb 11, 2021, 2:52:14 AM2/11/21
to golang-nuts
If both the memory addresses of the first bytes are equal, then the fast route will be selected by comparing their lengths.
Otherwise, byte by byte.
Reply all
Reply to author
Forward
0 new messages