Performance comparison of string concatenation in Go language

  sonic0002        2024-05-13 00:33:20       1,866        0    

In Go language, string concatenation is an inevitable operation during development, and efficient string concatenation is crucial for improving application performance. This article will delve into several common ways of string concatenation, provide performance comparisons, and offer optimization suggestions aimed at helping developers write more efficient code.

Common ways of string concatenation

In Go language, common ways of string concatenation include the following:

Using the + operator for concatenation

It's simple and straightforward, but each concatenation generates a new string, resulting in significant memory allocation overhead.

func plusConcat(n int, str string) string {
	s := ""
	for i := 0; i < n; i++ {
		s += str
	}
	return s
}

Using fmt.Sprintf for formatted concatenation

It supports rich formatting features, but its performance is not as good as other methods.

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

Using strings.Builder

Introduced in Go 1.10, this type is specifically designed for efficient string concatenation.

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

Using bytes.Buffer buffer

It's backed by a []byte slice, but converting to a string incurs additional memory allocation.

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

Using []byte slice concatenation

Manual memory management, performs well but prone to errors.

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

Performance comparison

To compare the performance of different concatenation methods, we concatenate strings of length 10 for 10,000 times and test the time taken and memory usage. Here are the test results for different concatenation methods:

 

time/op (ms)

memory/op(MB)

allocs/op

+ concatenation

56

530

10026

fmt.Sprintf

112

835

37435

strings.Builder

0.13

0.5

23

bytes.Buffer

0.14

0.4

13

[]byte pre-allocated

0.07

0.2

2

Principles behind performance

Why does strings.Builder perform so much better than other methods? The reason lies in the memory allocation mechanism.

For + concatenation, each concatenation generates a new string, leading to continuous memory reallocation.

strings.Builder uses an underlying []byte slice and employs exponential memory allocation strategy, avoiding frequent memory allocation.

When converting to a string, it directly returns the underlying []byte slice, avoiding additional memory allocation.

Optimization suggestions

Considering usability and performance, it's recommended to use strings.Builder for string concatenation. If extremely high performance is required, consider using pre-allocated memory with []byte slice concatenation.

Conclusion

This article compared the performance of different string concatenation methods, analyzed the underlying principles, and provided optimization suggestions. In practical development, choose the appropriate concatenation method based on performance requirements to avoid unnecessary performance overhead. We hope this article provides assistance in scenarios involving string concatenation. Different opinions are welcome in the comments.

COMPARISON  PERFORMANCE  STRING CONCATENATION 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Try and fail and finally down