Golang笔记

Golang string 和byte 操作性能比较

Golang string 和byte 操作性能比较

先上结论:

Strings are faster for searches (contains, index, compare) purpose.
bytes are faster in create (replace, concat) purpose.

下面代码

value, ok := mymap[string(mybytes)]
BenchmarkMapHints-4 100000000 12.3 ns/op 0 B/op 0 allocs/op

优于

key := string(mybytes)
value, ok := mymap[key]
BenchmarkMapsHints_Dont-4 100000000 22.0 ns/op 0 B/op 0 allocs/op

byte to string 的开销

b:= string(bytes)
BenchmarkBytesToStrings-4  30000000  38.6 ns/op 32 B/op 1 allocs/op

测试代码

package main

import (
	"bytes"
	"strings"
	"testing"
)

var result interface{}

//BenchmarkBytesToStrings convert a []bytes in a string
//https://golang.org/pkg/bytes/#Compare
func BenchmarkBytesToStrings(b *testing.B) {

	s1 := []byte("string to convert")

	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		r = string(s1)
	}

	result = r
}

//BenchmarkBytesCompare compare 2 []bytes.
//https://golang.org/pkg/bytes/#Compare
func BenchmarkBytesCompare(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("string to compare")

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = bytes.Compare(s1, s2)
	}

	result = r
}

//BenchmarkStringsCompare compare 2 strings.
//https://golang.org/pkg/strings/#Compare
func BenchmarkStringsCompare(b *testing.B) {

	s1 := "string to compare"
	s2 := "string to compare"

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = strings.Compare(s1, s2)
	}

	result = r
}

//BenchmarkBytesContains check contains method
//https://golang.org/pkg/bytes/#Contains
func BenchmarkBytesContains(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("comparc")

	b.ResetTimer()
	b.ReportAllocs()
	var r bool
	for n := 0; n < b.N; n++ {
		r = bytes.Contains(s1, s2)
	}

	result = r
}

//BenchmarkStringsContains check contains method
//https://golang.org/pkg/strings/#Contains
func BenchmarkStringsContains(b *testing.B) {

	s1 := "string to compare"
	s2 := "comparc"

	b.ResetTimer()
	b.ReportAllocs()
	var r bool
	for n := 0; n < b.N; n++ {
		r = strings.Contains(s1, s2)
	}

	result = r
}

//BenchmarkBytesIndex check contains index
//https://golang.org/pkg/bytes/#Index
func BenchmarkBytesIndex(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("e")

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = bytes.Index(s1, s2)
	}

	result = r
}

//BenchmarkStringIndex check contains index
//https://golang.org/pkg/strings/#Index
func BenchmarkStringIndex(b *testing.B) {

	s1 := "string to compare"
	s2 := "e"

	b.ResetTimer()
	b.ReportAllocs()
	var r int
	for n := 0; n < b.N; n++ {
		r = strings.Index(s1, s2)
	}

	result = r
}

//BenchmarkBytesReplace check replace method
//https://golang.org/pkg/bytes/#Replace
func BenchmarkBytesReplace(b *testing.B) {

	s1 := []byte("string to comparc")
	s2 := []byte("comparc")
	s3 := []byte("compare")

	b.ResetTimer()
	b.ReportAllocs()
	var r []byte
	for n := 0; n < b.N; n++ {
		r = bytes.Replace(s1, s2, s3, -1)
	}

	result = r
}

//BenchmarkStringsReplace check replace method
//https://golang.org/pkg/strings/#Replace
func BenchmarkStringsReplace(b *testing.B) {

	s1 := "string to comparc"
	s2 := "comparc"
	s3 := "compare"

	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		r = strings.Replace(s1, s2, s3, -1)
	}

	result = r
}

//BenchmarkBytesConcat concats 2 bytes
func BenchmarkBytesConcat2(b *testing.B) {

	s1 := []byte("string to compare")
	s2 := []byte("string to add")
	b.ResetTimer()
	b.ReportAllocs()

	for n := 0; n < b.N; n++ {
		r := make([]byte, 0, len(s1)+len(s2))
		r = append(r, s1...)
		r = append(r, s2...)
	}
}

//BenchmarkStringsConcat concats 2 strings
func BenchmarkStringsConcat2(b *testing.B) {

	s1 := "string to compare"
	s2 := "string to add"
	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		r = s1 + s2
	}

	result = r
}

//BenchmarkStringsJoin joins 2 strings
//https://golang.org/pkg/strings/#Join
func BenchmarkStringsJoin2(b *testing.B) {

	s1 := "string to compare"
	s2 := "string to add"
	b.ResetTimer()
	b.ReportAllocs()
	var r string
	for n := 0; n < b.N; n++ {
		j := []string{s1, s2}
		r = strings.Join(j, "")
	}

	result = r
}

//BenchmarkMapHints bench mymap[string(abytes)]
func BenchmarkMapHints(b *testing.B) {
	mymap := make(map[string]string)
	mymap["key"] = "value"
	abytes := []byte("key")

	b.ResetTimer()
	b.ReportAllocs()

	var v string
	for n := 0; n < b.N; n++ {
		v, _ = mymap[string(abytes)]
	}

	result = v
}

//BenchmarkMapsHints_Dont bench key := string(abytes)
//v, _ = mymap[key]
func BenchmarkMapsHints_Dont(b *testing.B) {
	mymap := make(map[string]string)
	mymap["key"] = "value"
	abytes := []byte("key")

	b.ResetTimer()
	b.ReportAllocs()

	var v string
	for n := 0; n < b.N; n++ {
		key := string(abytes)
		v, _ = mymap[key]
	}

	result = v

}
本文网址: https://golangnote.com/topic/187.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles