What is the most efficent way to print arrays in golang

huangapple go评论90阅读模式
英文:

What is the most efficent way to print arrays in golang

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习编程,想了解更多关于内置函数的知识:

这是一个程序示例:

func main() {
	start := time.Now()

	fmt.Println(os.Args)

	fmt.Println(time.Since(start))
}

它的输出结果是:

[A B C D E F G H I J K L M N O P] 124.009µs

这是我的 for 循环示例:

package main

import (
	"fmt"
	"os"
	"time"
)

func main() {
	start := time.Now()

	var s string
	for i := 0; i < len(os.Args); i++ {
		s += os.Args[i] + " "
	}
	fmt.Println(s)

	fmt.Println(time.Since(start))
}

它的输出结果是:

/tmp/go-build994847456/b001/exe/main A B C D E F G H I J K L M N O P 25.71µs

我原本期望标准库中的内置函数会更快。

切片会让我的代码效率降低吗?

我应该使用 for 循环还是标准库的 fmt.Println

我也对 strings.Join(os.Args[1:], " ") 的执行时间为 74.293µs 感到困惑。

英文:

I'm new to programming and wanted know more about the built-in functions:

Here is the is the program:

func main() {
	start := time.Now()

	fmt.Println(os.Args)

	fmt.Println(time.Since(start))
}

And its output:

[A B C D E F G H I J K L M N O P] 124.009&#181;s

Here is my for loop:

package main

import (
&quot;fmt&quot;
&quot;os&quot;
&quot;time&quot;
)

func main() {
start := time.Now()

    var s string
    for i:=0; i&lt;len(os.Args); i++{
    	s += os.Args[i] + &quot; &quot; 
    }
    fmt.Println(s)
    
    fmt.Println(time.Since(start))

}

with the following output:

/tmp/go-build994847456/b001/exe/main A B C D E F G H I J K L M N O P 25.71&#181;s

I was expecting to have the built in function in the standard library to be faster.

Do slices make my code less efficient ?

Should I use for loops or standard library fmt.println?

I'm also confused how would the strings.Join(os.Args\[1:\], &quot; &quot;) performed 74.293µs

答案1

得分: 0

使用基准测试:

var slice = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"}

func BenchmarkFMT(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		_ = fmt.Sprint(slice)
	}
}

func BenchmarkStringConcat(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		var str string
		for _, s := range slice {
			str += s + " "
		}
	}
}

func BenchmarkStringsBuilder(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		var l int
		for _, s := range slice {
			l += len(s)
		}
		var sb strings.Builder
		sb.Grow(l + len(slice)*len(" "))
		for _, s := range slice {
			sb.WriteString(s)
			sb.WriteString(" ")
		}
		_ = sb.String()
	}
}

func BenchmarkStringsJoin(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		_ = strings.Join(slice, " ")
	}
}
BenchmarkFMT
BenchmarkFMT-8              	  734088	      1633 ns/op	     616 B/op	      34 allocs/op
BenchmarkStringConcat
BenchmarkStringConcat-8     	 1290666	       919.1 ns/op	    1200 B/op	      32 allocs/op
BenchmarkStringsBuilder
BenchmarkStringsBuilder-8   	 6074888	       198.6 ns/op	      64 B/op	       1 allocs/op
BenchmarkStringsJoin
BenchmarkStringsJoin-8      	 4941542	       241.7 ns/op	      64 B/op	       1 allocs/op
PASS
英文:

Use benchmarks:

var slice = []string{&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;, &quot;J&quot;, &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;, &quot;O&quot;, &quot;P&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;, &quot;J&quot;, &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;, &quot;O&quot;, &quot;P&quot;}
func BenchmarkFMT(b *testing.B) {
b.ReportAllocs()
for n := 0; n &lt; b.N; n++ {
_ = fmt.Sprint(slice)
}
}
func BenchmarkStringConcat(b *testing.B) {
b.ReportAllocs()
for n := 0; n &lt; b.N; n++ {
var str string
for _, s := range slice {
str += s + &quot; &quot;
}
}
}
func BenchmarkStringsBuilder(b *testing.B) {
b.ReportAllocs()
for n := 0; n &lt; b.N; n++ {
var l int
for _, s := range slice {
l += len(s)
}
var sb strings.Builder
sb.Grow(l + len(slice)*len(&quot; &quot;))
for _, s := range slice {
sb.WriteString(s)
sb.WriteString(&quot; &quot;)
}
_ = sb.String()
}
}
func BenchmarkStringsJoin(b *testing.B) {
b.ReportAllocs()
for n := 0; n &lt; b.N; n++ {
_ = strings.Join(slice, &quot; &quot;)
}
}
BenchmarkFMT
BenchmarkFMT-8              	  734088	      1633 ns/op	     616 B/op	      34 allocs/op
BenchmarkStringConcat
BenchmarkStringConcat-8     	 1290666	       919.1 ns/op	    1200 B/op	      32 allocs/op
BenchmarkStringsBuilder
BenchmarkStringsBuilder-8   	 6074888	       198.6 ns/op	      64 B/op	       1 allocs/op
BenchmarkStringsJoin
BenchmarkStringsJoin-8      	 4941542	       241.7 ns/op	      64 B/op	       1 allocs/op
PASS

huangapple
  • 本文由 发表于 2023年1月31日 02:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75288730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定