Golang拼接字符串性能对比

2024-01-04

g o l a n g golang g o l an g s t r i n g string s t r in g 类型是不可修改的,对于拼接字符串来说,本质上还是创建一个新的对象将数据放进去。主要有以下几种拼接方式

拼接方式介绍

1.使用 s t r i n g string s t r in g 自带的运算符 + + +

ans = ans + s

2. 使用格式化输出 f m t . S p r i n t f fmt.Sprintf f m t . Sp r in t f

ans = fmt.Sprintf("%s%s", ans, s)

3. 使用 s t r i n g s strings s t r in g s j o i n join j o in 函数

一般适用于将字符串数组转化为特定间隔符的字符串的情况

ans=strings.join(strs,",")

4. 使用 s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er

builder := strings.Builder{}
builder.WriteString(s)
return builder.String()

5. 使用 b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er

buffer := new(bytes.Buffer)
buffer.WriteString(s)
return buffer.String()

6. 使用 [ ] b y t e []byte [ ] b y t e ,并且提前设置容量

ans := make([]byte, 0, len(s)*n)
ans = append(ans, s...)

性能对比

先写一个随机生成长度为 n n n 的字符串的函数

func getRandomString(n int) string {
	var tmp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	ans := make([]uint8, 0, n)
	for i := 0; i < n; i++ {
		ans = append(ans, tmp[rand.Intn(len(tmp))])
	}
	return string(ans)
}

接下来分别写出上述拼接方式的实现,假设每次都拼接n次字符串s后返回。

1.使用 s t r i n g string s t r in g 自带的运算符 + + +

循环 n n n 次,每次都令答案字符串 a n s + ans+ an s + 源字符串 s s s

func plusOperatorJoin(n int, s string) string {
	var ans string
	for i := 0; i < n; i++ {
		ans = ans + s
	}
	return ans
}

2. 使用格式化输出 f m t . S p r i n t f fmt.Sprintf f m t . Sp r in t f

循环 n n n 次,使用 f m t . S p r i n t f fmt.Sprintf f m t . Sp r in t f 达到拼接的目的

func sprintfJoin(n int, s string) string {
	var ans string
	for i := 0; i < n; i++ {
		ans = fmt.Sprintf("%s%s", ans, s)
	}
	return ans
}

3. 使用 s t r i n g s strings s t r in g s j o i n join j o in 函数

拼接同一个字符串的话不适合用 j o i n join j o in 函数,所以跳过这种方式

4. 使用 s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er

初始化 s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er ,循环 n n n 次,每次调用 W r i t e S t r i n g WriteString W r i t e St r in g 方法

func stringBuilderJoin(n int, s string) string {
	builder := strings.Builder{}
	for i := 0; i < n; i++ {
		builder.WriteString(s)
	}
	return builder.String()
}

5. 使用 b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er

初始化 b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er ,循环 n n n 次,每次调用 W r i t e S t r i n g WriteString W r i t e St r in g 方法

func bytesBufferJoin(n int, s string) string {
	buffer := new(bytes.Buffer)
	for i := 0; i < n; i++ {
		buffer.WriteString(s)
	}
	return buffer.String()
}

6. 使用 [ ] b y t e []byte [ ] b y t e ,并且提前设置容量

定义 a n s ans an s b y t e byte b y t e 数组,并提前设置容量为 l e n ( s ) ∗ n len(s)*n l e n ( s ) n

func bytesJoin(n int, s string) string {
	ans := make([]byte, 0, len(s)*n)
	for i := 0; i < n; i++ {
		ans = append(ans, s...)
	}
	return string(ans)
}

测试代码

先随机生成一个长度为10的字符串,然后拼接10000次。

package high_strings

import "testing"

func benchmark(b *testing.B, f func(int, string) string) {
	var str = getRandomString(10)
	for i := 0; i < b.N; i++ {
		f(10000, str)
	}
}

func BenchmarkPlusOperatorJoin(b *testing.B) {
	benchmark(b, plusOperatorJoin)
}
func BenchmarkSprintfJoin(b *testing.B) {
	benchmark(b, sprintfJoin)
}
func BenchmarkStringBuilderJoin(b *testing.B) {
	benchmark(b, stringBuilderJoin)
}
func BenchmarkBytesBufferJoin(b *testing.B) {
	benchmark(b, bytesBufferJoin)
}
func BenchmarkBytesJoin(b *testing.B) {
	benchmark(b, bytesJoin)
}

在这里插入图片描述

测试结果:

使用 [ ] b y t e []byte [ ] b y t e > s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er >= b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er > f m t . S p r i n t f fmt.Sprintf f m t . Sp r in t f > + + + 运算符

源码分析

1.使用 s t r i n g string s t r in g 自带的运算符 + + +

代码在 runtime\string.go


// concatstrings implements a Go string concatenation x+y+z+...
// The operands are passed in the slice a.
// If buf != nil, the compiler has determined that the result does not
// escape the calling function, so the string data can be stored in buf
// if small enough.
func concatstrings(buf *tmpBuf, a []string) string {
	idx := 0
	l := 0
	count := 0
	for i, x := range a {
		n := len(x)
		if n == 0 {
			continue
		}
		if l+n < l {
			throw("string concatenation too long")
		}
		l += n
		count++
		idx = i
	}
	if count == 0 {
		return ""
	}

	// If there is just one string and either it is not on the stack
	// or our result does not escape the calling frame (buf != nil),
	// then we can return that string directly.
	if count == 1 && (buf != nil || !stringDataOnStack(a[idx])) {
		return a[idx]
	}
	s, b := rawstringtmp(buf, l)
	for _, x := range a {
		copy(b, x)
		b = b[len(x):]
	}
	return s
}

  • 首先计算拼接后的字符串长度
  • 如果只有一个字符串并且不在栈上就直接返回
  • 如果 b u f buf b u f 不为空并且 b u f buf b u f 可以放下这些字符串,就把拼接后的字符串放在 b u f buf b u f 里,否则在堆上重新申请一块内存
func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
	if buf != nil && l <= len(buf) {
		b = buf[:l]
		s = slicebytetostringtmp(&b[0], len(b))
	} else {
		s, b = rawstring(l)
	}
	return
}
// rawstring allocates storage for a new string. The returned
// string and byte slice both refer to the same storage.
// The storage is not zeroed. Callers should use
// b to set the string contents and then drop b.
func rawstring(size int) (s string, b []byte) {
	p := mallocgc(uintptr(size), nil, false)
	return unsafe.String((*byte)(p), size), unsafe.Slice((*byte)(p), size)
}

  • 然后遍历数组,将字符串 c o p y copy co p y 过去

2. 使用 s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er

介绍: s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er 用于使用 W r i t e Write W r i t e 方法高效地生成字符串,它最大限度地减少了内存复制
拼接过程: b u i l d e r builder b u i l d er 里有一个 b y t e byte b y t e 类型的切片,每次调用 W r i t e S t r i n g WriteString W r i t e St r in g 的时候,是直接往该切片里追加字符串。因为切片底层的扩容机制是以倍数申请的,所以对比1而言,2的内存消耗要更少。
**结果返回:**在返回字符串的 S t r i n g String St r in g 方法里,是将 b u f buf b u f 数组转化为字符串直接返回的。
扩容机制: 想要缓冲区容量增加 n n n 个字节,扩容后容量变为 2 ∗ l e n + n 2*len+n 2 l e n + n

// A Builder is used to efficiently build a string using Write methods.
// It minimizes memory copying. The zero value is ready to use.
// Do not copy a non-zero Builder.
type Builder struct {
	addr *Builder // of receiver, to detect copies by value
	buf  []byte
}

// String returns the accumulated string.
func (b *Builder) String() string {
	return unsafe.String(unsafe.SliceData(b.buf), len(b.buf))
}

// grow copies the buffer to a new, larger buffer so that there are at least n
// bytes of capacity beyond len(b.buf).
func (b *Builder) grow(n int) {
	buf := make([]byte, len(b.buf), 2*cap(b.buf)+n)
	copy(buf, b.buf)
	b.buf = buf
}
// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
	b.copyCheck()
	b.buf = append(b.buf, s...)
	return len(s), nil
}

3. 使用 b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er

介绍 b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er 的底层都是 b y t e byte b y t e 数组,区别在于扩容机制和返回字符串的 S t r i n g String St r in g 方法。
结果返回: 因为 b y t e s . B u f f e r bytes.Buffer b y t es . B u ff er 实际上是一个流式的字节缓冲区,可以向尾部写入数据,也可以读取头部的数据。所以在返回字符串的 S t r i n g String St r in g 方法里,只返回了缓冲区里 未读的部分 ,所以需要重新申请内存来存放返回的结果。内存会比 s t r i n g s . B u i l d e r strings.Builder s t r in g s . B u i l d er 稍慢一些。
扩容机制: 想要缓冲区容量至少增加 n n n 个字节, m m m 是未读的长度, c c c 是当前的容量。
优化点在于如果 n < = c / 2 − m n <= c/2-m n <= c /2 m ,也就是当前容量的一半都大于等于现有的内容(未读的字节数)加上所需要增加的字节数,就复用当前的数组,把未读的内容拷贝到头部去。

We can slide things down instead of allocating a new slice. We only need m+n <= c to slide, but we instead let capacity get twice as large so we don’t spend all our time copying.
我们可以向下滑动,而不是分配一个新的切片。我们只需要m+n<=c来滑动,但我们让容量增加了一倍,这样我们就不会把所有的时间都花在复制上。

否则的话也是 2 ∗ l e n + n 2*len+n 2 l e n + n 的扩张

// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.
type Buffer struct {
	buf      []byte // contents are the bytes buf[off : len(buf)]
	off      int    // read at &buf[off], write at &buf[len(buf)]
	lastRead readOp // last read operation, so that Unread* can work correctly.
}
// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
//
// To build strings more efficiently, see the strings.Builder type.
func (b *Buffer) String() string {
	if b == nil {
		// Special case, useful in debugging.
		return "<nil>"
	}
	return string(b.buf[b.off:])
}
// WriteString appends the contents of s to the buffer, growing the buffer as
// needed. The return value n is the length of s; err is always nil. If the
// buffer becomes too large, WriteString will panic with ErrTooLarge.
func (b *Buffer) WriteString(s string) (n int, err error) {
	b.lastRead = opInvalid
	m, ok := b.tryGrowByReslice(len(s))
	if !ok {
		m = b.grow(len(s))
	}
	return copy(b.buf[m:], s), nil
}

// grow grows the buffer to guarantee space for n more bytes.
// It returns the index where bytes should be written.
// If the buffer can't grow it will panic with ErrTooLarge.
func (b *Buffer) grow(n int) int {
	m := b.Len()
	// If buffer is empty, reset to recover space.
	if m == 0 && b.off != 0 {
		b.Reset()
	}
	// Try to grow by means of a reslice.
	if i, ok := b.tryGrowByReslice(n); ok {
		return i
	}
	if b.buf == nil && n <= smallBufferSize {
		b.buf = make([]byte, n, smallBufferSize)
		return 0
	}
	c := cap(b.buf)
	if n <= c/2-m {
		// We can slide things down instead of allocating a new
		// slice. We only need m+n <= c to slide, but
		// we instead let capacity get twice as large so we
		// don't spend all our time copying.
		copy(b.buf, b.buf[b.off:])
	} else if c > maxInt-c-n {
		panic(ErrTooLarge)
	} else {
		// Add b.off to account for b.buf[:b.off] being sliced off the front.
		b.buf = growSlice(b.buf[b.off:], b.off+n)
	}
	// Restore b.off and len(b.buf).
	b.off = 0
	b.buf = b.buf[:m+n]
	return m
}

字符串拼接性能及原理
GoLang bytes.Buffer基础使用方法详解

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Golang拼接字符串性能对比 的相关文章

  • Python机器学习实战:用Python构建10个有趣的应用

    机器学习是一门强大的工具 可以用于解决各种各样的问题 通过学习机器学习 您可以开发出能够自动化任务 做出预测甚至创造艺术的应用程序 如果您是一名 Python 开发人员 那么您将很高兴知道 有许多可以用 Python 构建的有趣机器学习应用
  • 江河湖泊生态水文监测物联网解决方案

    方案背景 江湖湖泊具有重要的经济效益和生态效益 是重要的资源储备 近年来 各级积极采取措施 加强江河湖泊治理 管理和保护 在防洪 供水 发电 航运 养殖等方面的综合发展 随着纳入管理的江河湖泊等水体越来越多 范围越来越广 很多水污染 非法采

随机推荐

  • Laya游戏开发中AI寻路解决方案

    1 AI自动寻路 机器人代码重构 按照目标点去执行逻辑 提前几帧判断直线 非直线的情况下 预设转弯角度 角度判断到达直线后开始执行到目标点的逻辑 2 U3D布点寻路 3 NevMesh Js寻路插件 NevMesh Js你可以在Laya引擎
  • Python生成器:优雅而高效的迭代器

    本文将为大家介绍下 Python 中的 生成器 它有何强大之处 实际开发任务中 for循环与生成器我们将如何取舍 Python是一种强大而灵活的编程语言 拥有丰富的标准库和特性功能 其中之一就是 生成器 生成器 是Python中一种非常实用
  • TXT文本删除第一行文本变成空要如何解决呢

    首先大家一起来看下这个TXT文本里面有多行内容 想把开头第一行批量删除不要掉 1 如果是一两个本可以手动删除也很方便哦 如果文本量比较大如几十几 几百个文本大家一直都选用 首助编辑高手 工具去批量操作哦 批量操作可以大大提高工作效率 接来看
  • 办公自动化-邮件通知和微信通知

    在工作中 或者学习中或者生活上等 需要定时提醒自己或其他人 处理一些待办任务事项 需要发邮件通知下 同时可能会要求发送文件之类的事情 由于现在大家微信使用频率最高 所以能发送微信通知消息 效果更好 同时开通下微信通知功能 第一步 邮件通知工
  • Java 跨平台实现

    Java 跨平台实现 1 Java虚拟机 JVM 2 中间代码 字节码 3 Write Once Run Anywhere WORA 4 Java 标准库 5 安全性与隔离 6 Java Community Process JCP
  • 朋渤WMS助力电商:无代码API连接广告与CRM系统

    优化用户运营的无代码解决方案 如今 企业追求高效的用户运营变得尤为重要 而这一切的基础是系统间的无缝集成 朋渤WMS提供了一个无需编写API代码的集成方案 通过其无代码开发平台 不仅优化了工业电气行业的业务流程 还让电商企业在激烈的市场竞争
  • GHM-XGBOOST模型的学习和理解

    Gradient Harmonized Margins GHM GHM 是一种损失函数 主要用于解决类别不平衡问题 GHM 损失函数能够关注较难的样本 对于容易的样本降低权重 从而在训练过程中更好地平衡了损失 这有助于提高模型对于少数类别的
  • SQL查询 字符串数据

    查询房屋特色 例如 1 2 3 多个 字符串 tag ids this gt request gt param tag ids s if tag ids arr explode tag ids tag ids map arr foreach
  • web前端学习怎么能快速入门

    Web前端开发是一个热门的职业领域 很多人都希望能够快速入门并掌握相关技能 下面将从制定学习计划 项目实战案例练习 以用为学 与培训班老师多交流 自主学习能力的培训等5个方面详细介绍如何在web前端培训班学习 1 制定web前端学习计划 在
  • 在Java培训班怎么学习?这5个学习技巧送给你

    在Java培训班学习Java编程语言是一个很好的学习平台 但是如何更有效地学习呢 下面我将为大家介绍5个学习技巧 希望能帮助到大家 Java是一种面向对象的编程语言 被广泛应用于企业级应用开发 移动应用开发 大数据处理等领域 学习Java编
  • 培训学习大数据知识有哪些方法

    学习大数据知识是当前非常热门的话题 随着大数据技术的不断发展 越来越多的人开始关注并学习大数据知识 在大数据培训班学习大数据知识是一个非常好的选择 下面我将从制定大数据学习计划 项目实战案例练习 以用为学 与培训班老师多交流等四个方面来详细
  • 旧硬盘插电脑上显示要初始化怎么办?了解原因和解决方案

    在使用旧的硬盘插入电脑时 有时会遇到需要进行初始化的情况 这种情况可能是由于多种原因引起的 而初始化硬盘将会导致所有数据丢失 给用户造成不便和损失 因此 本文将介绍解决旧硬盘需要初始化的问题的方法 并提供一些建议来帮助读者避免数据丢失 一
  • Linux配置Acado

    如果需要使用acado的matlab接口 请移步 Linux Matlab配置Acado 首先 安装必要的软件包 sudo apt get install gcc g cmake git gnuplot doxygen graphviz 在
  • APK 瘦身

    APK 瘦身的主要原因是考虑应用的下载转化率和留存率 应用太大了 用户可能就不下载了 再者 因为手机空间问题 用户有可能会卸载一些占用空间比较大的应用 所以 应用的大小也会影响留存率 1 APK 的结构 包含以下目录 assets 包含了应
  • 信息: 没有运行的任务匹配指定标准。

    文章 前言 错误场景 问题分析 解决方案 后言 前言 他们是天生勇敢的开发者 我们创造bug 传播bug 毫不留情地消灭bug 在这个过程中我们创造了很多bug以供娱乐 前端bug 这里是博主总结的一些前端的bug以及解决方案 感兴趣可以看
  • Centos8破解Root密码

    注 Centos7同理 1 重启Centos8系统后 在启动页面中选中第一行 按 e 键进入界面 2 找到linux开头所在行的找到 ro 改为上 rw init sysroot bin bash 同时按下 Ctrl X 跳转到紧急模式 3
  • 前端push.js桌面通知库

    push js 官网 https pushjs org 安装 1 npm 安装方式 npm install push js save 2 script引入方式 使用 1 获取用户许可 用户需要先授予权限才能发送通知 Push Permiss
  • [大厂实践] 零配置服务网格与按需集群发现

    本文介绍了Netflix通过扩展Envoy支持按需集群发现机制帮助团队无缝迁移服务网格的实践 原文 Zero Configuration Service Mesh with On Demand Cluster Discovery 在这篇文章
  • 使用vue实现一个网页的贴边组件。

    使用vue实现一个网页的贴边组件 先来看效果 2024 01 04 10 46 22 https www haolu com share V00O6HWYR8 36207fc21c35b2a8e09bf22787a81527 下面是具体代码
  • Golang拼接字符串性能对比

    g o l a n g golang g o l an g