温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

寻找golang中最高效的字符串连接函数

发布时间:2020-05-24 15:42:04 来源:网络 阅读:1904 作者:ustb80 栏目:编程语言

以下五个字符串连接函数,你认为哪一个最快?

func StrConcat1(strs []string) (string) {
    var str string
    for _, value := range strs {
        str += value
    }
    return str
}

func StrConcat2(strs []string) (string) {
    if len(strs) == 0 {
        return ""
    }
    b := bytes.Buffer{}
    for _, s := range strs {
        b.WriteString(s)
    }
    return b.String()
}

func StrConcat3(strs []string) (string) {
    return strings.Join(strs, "")
}

func StrConcat4(strs []string) (string) {
    var data []byte
    for _, value := range strs {
        data = append(data, value...)
    }
    return string(data)
}

func StrConcat5(strs []string) (string) {
    var length int
    for _, s := range strs {
        length += len(s)
    }
    bs := make([]byte, length)
    var i int
    for _, value := range strs {
        i += copy(bs[i:], value)
    }
    return string(bs[:])
}

编写如下的测试文件

import "testing"

var TestStr = []string{"hello", "world", "my", "god", "this", "is", "big", "content"}

func Benchmark_StrConcat1(b *testing.B)  {
    for i := 0; i < b.N ; i++ {
        StrConcat1(TestStr)
    }
}

func Benchmark_StrConcat2(b *testing.B)  {
    for i := 0; i < b.N ; i++ {
        StrConcat2(TestStr)
    }
}

func Benchmark_StrConcat3(b *testing.B)  {
    for i := 0; i < b.N ; i++ {
        StrConcat3(TestStr)
    }
}

func Benchmark_StrConcat4(b *testing.B)  {
    for i := 0; i < b.N ; i++ {
        StrConcat4(TestStr)
    }
}

func Benchmark_StrConcat5(b *testing.B)  {
    for i := 0; i < b.N ; i++ {
        StrConcat5(TestStr)
    }
}

然后执行测试命令

go test -test.bench=".*"

执行结果如下:

Benchmark_StrConcat1-4       5000000           366 ns/op
Benchmark_StrConcat2-4      10000000           178 ns/op
Benchmark_StrConcat3-4      10000000           127 ns/op
Benchmark_StrConcat4-4      10000000           178 ns/op
Benchmark_StrConcat5-4      20000000           102 ns/op

可见,第5个函数的效率是最高的,虽然它的代码量最多。
其次是第3个函数,其实我们查看第3个函数的源代码就可以发现,它与第5个函数本质上是一样的,但是因为多了对分隔符的拷贝操作,执行时间增加了。我认为这个系统函数可以再优化,判断分隔符是不是空的,如果是空的,就按第5个函数那个处理,这样效率还能提升15%。

附,strings.Join函数源代码:

// Join concatenates the elements of a to create a single string. The separator string
// sep is placed between elements in the resulting string.
func Join(a []string, sep string) string {
    switch len(a) {
    case 0:
        return ""
    case 1:
        return a[0]
    case 2:
        // Special case for common small values.
        // Remove if golang.org/issue/6714 is fixed
        return a[0] + sep + a[1]
    case 3:
        // Special case for common small values.
        // Remove if golang.org/issue/6714 is fixed
        return a[0] + sep + a[1] + sep + a[2]
    }
    n := len(sep) * (len(a) - 1)
    for i := 0; i < len(a); i++ {
        n += len(a[i])
    }

    b := make([]byte, n)
    bp := copy(b, a[0])
    for _, s := range a[1:] {
        bp += copy(b[bp:], sep)
        bp += copy(b[bp:], s)
    }
    return string(b)
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI