What is the most efficent way to print arrays in golang

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

What is the most efficent way to print arrays in golang

问题

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

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

这是一个程序示例:

  1. func main() {
  2. start := time.Now()
  3. fmt.Println(os.Args)
  4. fmt.Println(time.Since(start))
  5. }

它的输出结果是:

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

这是我的 for 循环示例:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. func main() {
  8. start := time.Now()
  9. var s string
  10. for i := 0; i < len(os.Args); i++ {
  11. s += os.Args[i] + " "
  12. }
  13. fmt.Println(s)
  14. fmt.Println(time.Since(start))
  15. }

它的输出结果是:

/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:

  1. func main() {
  2. start := time.Now()
  3. fmt.Println(os.Args)
  4. fmt.Println(time.Since(start))
  5. }

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:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;os&quot;
  5. &quot;time&quot;
  6. )
  7. func main() {
  8. start := time.Now()
  9. var s string
  10. for i:=0; i&lt;len(os.Args); i++{
  11. s += os.Args[i] + &quot; &quot;
  12. }
  13. fmt.Println(s)
  14. fmt.Println(time.Since(start))
  15. }

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

使用基准测试:

  1. 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"}
  2. func BenchmarkFMT(b *testing.B) {
  3. b.ReportAllocs()
  4. for n := 0; n < b.N; n++ {
  5. _ = fmt.Sprint(slice)
  6. }
  7. }
  8. func BenchmarkStringConcat(b *testing.B) {
  9. b.ReportAllocs()
  10. for n := 0; n < b.N; n++ {
  11. var str string
  12. for _, s := range slice {
  13. str += s + " "
  14. }
  15. }
  16. }
  17. func BenchmarkStringsBuilder(b *testing.B) {
  18. b.ReportAllocs()
  19. for n := 0; n < b.N; n++ {
  20. var l int
  21. for _, s := range slice {
  22. l += len(s)
  23. }
  24. var sb strings.Builder
  25. sb.Grow(l + len(slice)*len(" "))
  26. for _, s := range slice {
  27. sb.WriteString(s)
  28. sb.WriteString(" ")
  29. }
  30. _ = sb.String()
  31. }
  32. }
  33. func BenchmarkStringsJoin(b *testing.B) {
  34. b.ReportAllocs()
  35. for n := 0; n < b.N; n++ {
  36. _ = strings.Join(slice, " ")
  37. }
  38. }
  1. BenchmarkFMT
  2. BenchmarkFMT-8 734088 1633 ns/op 616 B/op 34 allocs/op
  3. BenchmarkStringConcat
  4. BenchmarkStringConcat-8 1290666 919.1 ns/op 1200 B/op 32 allocs/op
  5. BenchmarkStringsBuilder
  6. BenchmarkStringsBuilder-8 6074888 198.6 ns/op 64 B/op 1 allocs/op
  7. BenchmarkStringsJoin
  8. BenchmarkStringsJoin-8 4941542 241.7 ns/op 64 B/op 1 allocs/op
  9. PASS
英文:

Use benchmarks:

  1. 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;}
  2. func BenchmarkFMT(b *testing.B) {
  3. b.ReportAllocs()
  4. for n := 0; n &lt; b.N; n++ {
  5. _ = fmt.Sprint(slice)
  6. }
  7. }
  8. func BenchmarkStringConcat(b *testing.B) {
  9. b.ReportAllocs()
  10. for n := 0; n &lt; b.N; n++ {
  11. var str string
  12. for _, s := range slice {
  13. str += s + &quot; &quot;
  14. }
  15. }
  16. }
  17. func BenchmarkStringsBuilder(b *testing.B) {
  18. b.ReportAllocs()
  19. for n := 0; n &lt; b.N; n++ {
  20. var l int
  21. for _, s := range slice {
  22. l += len(s)
  23. }
  24. var sb strings.Builder
  25. sb.Grow(l + len(slice)*len(&quot; &quot;))
  26. for _, s := range slice {
  27. sb.WriteString(s)
  28. sb.WriteString(&quot; &quot;)
  29. }
  30. _ = sb.String()
  31. }
  32. }
  33. func BenchmarkStringsJoin(b *testing.B) {
  34. b.ReportAllocs()
  35. for n := 0; n &lt; b.N; n++ {
  36. _ = strings.Join(slice, &quot; &quot;)
  37. }
  38. }
  1. BenchmarkFMT
  2. BenchmarkFMT-8 734088 1633 ns/op 616 B/op 34 allocs/op
  3. BenchmarkStringConcat
  4. BenchmarkStringConcat-8 1290666 919.1 ns/op 1200 B/op 32 allocs/op
  5. BenchmarkStringsBuilder
  6. BenchmarkStringsBuilder-8 6074888 198.6 ns/op 64 B/op 1 allocs/op
  7. BenchmarkStringsJoin
  8. BenchmarkStringsJoin-8 4941542 241.7 ns/op 64 B/op 1 allocs/op
  9. 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:

确定