码迷,mamicode.com
首页 > 其他好文 > 详细

string 中的一些优化事项

时间:2017-05-17 00:51:26      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:结果   var   16px   join   sprint   testing   microsoft   优化   size   

1.1 fmt  vs  "+" (无转义)

import (
    "testing"
    "fmt"
)

var (
    str = "hello gohpers!"
    sep = ","
)

func BenchmarkFmt(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = fmt.Sprint("%s%s%s%s%s", str, sep, str, sep, str)
    }
}

func BenchmarkPlus(b *testing.B) {
    for i := 0; i < b.N; i++ {
        _ = str + sep + str + sep + str
    }
}

运行结果概括如下:

BenchmarkFmt        3000000	       490 ns/op
BenchmarkPlus        15000000	        78 ns/op

1.1 fmt  vs  "+" (带有转义)

 

2. strings.join  VS  "+"

持续更新中

 

string 中的一些优化事项

标签:结果   var   16px   join   sprint   testing   microsoft   优化   size   

原文地址:http://www.cnblogs.com/liang1101/p/6864254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!