对比 +(运算符)、strings.Join、sprintf、bytes.Buffer对字符串拼接的性能

package main

import (
"bytes"
"fmt"
"strings"
"testing"
) func TestfourPlusFour(t *testing.T) {
fmt.Println("testing")
} func BenchmarkAddStringWithOperator(b *testing.B) {
hello := ""
for i := 0; i < b.N; i++ {
hello += "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello"
}
} func BenchmarkAddStringWithSptint(b *testing.B) {
hello := ""
for i := 0; i < b.N; i++ {
hello = fmt.Sprintf("%s%s", hello, "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello")
}
} func BenchmarkAddStringWithJoin(b *testing.B) {
hello := ""
for i := 0; i < b.N; i++ {
hello = strings.Join([]string{hello, "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello"}, "")
}
} func BenchmarkAddStringWithBuffer(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
buf.WriteString("hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello")
}
_ = buf.String()
}

  运行测试结果:

在一些性能要求较高的场合,尽量使用buf.WriteString()以获得较好的可读性

golang字符串拼接性能对比的更多相关文章

  1. [Golang]字符串拼接方式的性能分析

    本文100%由本人(Haoxiang Ma)原创,如需转载请注明出处. 本文写于2019/02/16,基于Go 1.11.至于其他版本的Go SDK,如有出入请自行查阅其他资料. Overview 写 ...

  2. java字符串格式化性能对比String.format/StringBuilder/+拼接

    String.format由于每次都有生成一个Formatter对象,因此速度会比较慢,在大数据量需要格式化处理的时候,避免使用String.format进行格式化,相反使用StringUtils.l ...

  3. C# 字符串拼接性能探索

    本文通过ANTS Memory Profiler工具探索c#中+.string.Concat.string.Format.StringBuilder.Append四种方式进行字符串拼接时的性能. 本文 ...

  4. C# 字符串拼接性能探索 c#中+、string.Concat、string.Format、StringBuilder.Append四种方式进行字符串拼接时的性能

    本文通过ANTS Memory Profiler工具探索c#中+.string.Concat.string.Format.StringBuilder.Append四种方式进行字符串拼接时的性能. 本文 ...

  5. golang字符串拼接

    四种拼接方案: 1,直接用 += 操作符, 直接将多个字符串拼接. 最直观的方法, 不过当数据量非常大时用这种拼接访求是非常低效的. 2,直接用 + 操作符,这个和+=其实一个意思了. 3,用字符串切 ...

  6. C# 利用StringBuilder提升字符串拼接性能

    一个项目中有数据图表呈现,数据量稍大时显得很慢. 用Stopwatch分段监控了一下,发现耗时最多的函数是SaveToExcel 此函数中遍列所有数据行,通过Replace替换标签生成Excel行,然 ...

  7. Java字符串拼接效率对比

    1.来自:http://blog.csdn.net/Zen99T/article/details/51255418 2.来自:http://blog.csdn.net/kimsoft/article/ ...

  8. golang 字符串与整数, 布尔转换 strconv

    strconv 是golang对于字符串和基本数据类型之间的转换字符串转整数testStr := "1000" testInt, err := strconv.Atoi(testS ...

  9. 操作 html 的时候是使用 dom 方法还是字符串拼接?

    比如一个列表里面有很多个 li,要给他们加上数据.但多少个 li 是不确定的,由后台数据确定.这时候,就要动态生成 html 内容了. 那么,这个过程, 是使用 += 方法把标签.数据进行一个个的字符 ...

随机推荐

  1. 2016.6.21 -Dmaven.multiModuleProjectDirectory system propery is not set,Check $M2_HOME environment variable and mvn script match.

    eclipse中使用maven插件的时候,运行run as maven build的时候报错: -Dmaven.multiModuleProjectDirectory system propery i ...

  2. mysql下监测数据库语句creating sort index时间过长的问题

    在一张单表5000W数据上进行数据查询时传入两个单列索引条件,进行组合索引查询时,如果最后有order by id排序,与去除该排序,性能差距接近两个数量级 结论:在使用列的默认排序时,不应该再ord ...

  3. openLayers加载高德地图

    之前用openlayers对高德,百度,腾讯,bing,supermap,天地图,arcgis,google等地图进行了对接,今天简单介绍一下openlayers+高德: 在Openlayers.La ...

  4. jquery相冊图片来回选择

    <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <script sr ...

  5. 【Excle数据透视】如何在数据透视表字段列表中显示更多的字段

    创建完数据透视表之后,由于字段太多,在列表中没有完全显示 解决方案 通过"字段节和区域节并排"功能来显示更多字段 修改后结果 字段已经完全显示出来了! "字段节和区域节层 ...

  6. 1BIT,1BYTE,1KB,1MB,1GB,1TB等计量单位换算

    http://iask.sina.com.cn/b/8961090.html知识   在数字世界里没有电影.没有杂志.没有一首首的乐曲,只有一个个的数字“1”和“0”.以前人们对于数字世界中的这两个数 ...

  7. HDU 2255 奔小康赚大钱 KM裸题

    #include <stdio.h> #include <string.h> #define M 310 #define inf 0x3f3f3f3f int n,nx,ny; ...

  8. Java结束线程的三种方法

    线程属于一次性消耗品,在执行完run()方法之后线程便会正常结束了,线程结束后便会销毁,不能再次start,只能重新建立新的线程对象,但有时run()方法是永远不会结束的.例如在程序中使用线程进行So ...

  9. rtems 4.11 时钟驱动(arm, beagle)

    根据bsp_howto手册,时钟驱动的框架主要在 c/src/lib/libbsp/shared/Clockdrv_shell.h 文件中实现 时钟初始化 时钟驱动初始化函数为 Clock_initi ...

  10. sql 注入 与解决

    package cn.itcast.jdbc; import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLExce ...