Go字符串连接

对于字符串的连接大致有两种方式:

1、通过+号连接

func StrPlus1(a []string) string {
var s, sep string
for i := 0; i < len(a); i++ {
s += sep + a[i]
sep = " "
}
return s
}

2、通过strings.Join连接

func StrPlus2(a []string) string {
return strings.Join(a, " ")
}

对比两种方式的效率,通过压力测试进行对比

import "testing"

func BenchmarkStrPlus1(b *testing.B) {
for i := 0; i < b.N; i++ {
StrPlus1([]string{"xxx", "bbb", "aaa"})
}
} func BenchmarkStrPlus2(b *testing.B) {
for i := 0; i < b.N; i++ {
StrPlus2([]string{"xxx", "bbb", "aaa"})
}
}

运行压力测试go test -test.bench=".*"

goos: darwin
goarch: amd64
BenchmarkStrPlus1-4 10000000 127 ns/op
BenchmarkStrPlus2-4 20000000 78.7 ns/op

从本机来看通过+号连接字符串每个操作消耗127ns时间,strings.Join消耗78.7ns。效率上strings.Join更高

来看下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)
}

可以看出当连接字符串数量较大时,是先通过make分配足够的空间,然后把每个字符串copy到空间里面,而不是每次通过+号来多次分配内存。

Go 字符串连接+=与strings.Join性能对比的更多相关文章

  1. 教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是

    (41)StringBuilder is NOT the answer for all string concatenation scenarios; String.Join could be 招数4 ...

  2. 前端性能优化:使用Array.prototype.join代替字符串连接

    来源:GBin1.com 有一种非常简单的客户端优化方式,就是用Array.prototype.join代替原有的基本的字符连接的写法.在这个系列的第一篇中,我在代码中使用了基本字符连接: htmlS ...

  3. golang字符串拼接性能对比

    对比 +(运算符).strings.Join.sprintf.bytes.Buffer对字符串拼接的性能 package main import ( "bytes" "f ...

  4. JS中三种字符串连接方式及其性能比较

    工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方法,这里将它们一一列出顺便也对它们的性能做个具体的比较. 第一种方法  用连接符“+”把要连接的字符串连起来 ...

  5. Java字符串连接的多种实现方法及效率对比

    JDK 1.8(Java 8)里新增String.join()方法用于字符串连接.本文基于<Java实现String.join()和效率比较>一文,分析和比较四种自定义实现与String. ...

  6. [笔记]Go语言的字符串拼装方式性能对比

    Go语言中字符串的拼装方法很多,那么问题来了,到底哪家性能好? 下面代码,分别比较了 fmt.Sprintf,string +,strings.Join,bytes.Buffer,方法是循环若干次比较 ...

  7. JavaScript中三种字符串连接方式及其性能比较

    参考地址: https://www.cnblogs.com/programs/p/5554742.html 工作中经常会碰到要把2个或多个字符串连接成一个字符串的问题,在JS中处理这类问题一般有三种方 ...

  8. Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比

    比较的指标: 1.cpu 2.流量 3.电量 4.内存占用 5.联网时间 功能点: 1.重试机制 2.提供的扩展功能 3.易用性 4.是否https 5.是否支持reflect api,OkHttp有 ...

  9. Effective Java 第三版——63. 注意字符串连接的性能

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

随机推荐

  1. 4 扩展库Scipy

    https://www.scipy.org/ 1. numpy    矩阵 2. matplotlib 绘图库 3. pandas 高效的Series和DataFrame数据结构 4.5 ndarry ...

  2. python-之-深浅拷贝二(元组)

    元组比较特殊 1.----元组本身为不可变类型 import copy v1 = (1, 2, 3, 4) v2 = copy.copy(v1) print(id(v1), id(v2)) v3 = ...

  3. hadoop sentry错误记录

    1.报无法实例化metastore连接 hive> show tables; FAILED: SemanticException org.apache.hadoop.hive.ql.metada ...

  4. 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员

    转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...

  5. 软工个人作业4——Alpha阶段个人总结

    一.个人总结 1.在alpha 结束之后, 每位同学写一篇个人博客, 总结自己的alpha 过程: 经过本次alpha阶段的冲刺,首先学到了很多,收获了很多,同时也蛮辛苦的.其实我觉得作为组员我有很认 ...

  6. Windows7上安装Ubuntu双系统

    零.前言 最近不小心把Ubuntu系统搞崩了打不开了,在网上找了找方法,从最初的步骤开始安装,本文是安装Ubuntu16.04,不过安装啥版本步骤都一样,下面逐一介绍. 一.如何卸载Ubuntu(第一 ...

  7. Vue中transition和animation的使用

    一:二者的对比 1.动画循环就用animation.在animation中有一个animation-iteration-count属性可以定义循环次数.transition是执行一次以后就不会执行,但 ...

  8. Java学习——类与对象

    在学习面向对象之前首先我们要理解一下几点: 什么是面向对象 对象的概念 类 类与对象的关系/区别 什么是对象的属性 什么是对象的方法 什么是面向对象.对象.类 讲到面向对象就不得提到面向过程,早期的计 ...

  9. 编译原理子cygwin的使用

    目的:熟悉cygwin环境的使用,学习使用lex写简单的词法分析程序,会在cygwin环境下使用flex调试lex写的程序 内容:使用cygwin下的flex工具将exam1.l和exam2.l编译并 ...

  10. 玩转BLE(2)_使用bluepy扫描BLE的广播数据

    1. 前言 在linux平台下,bluez是一个很不错的软件,提供了很多基于命令行的测试工具,如hciconfig.hcitool.hcidump.bluetoothctl等.利用这些工具,我们可以方 ...