英文:
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µs
Here is my for loop:
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))
}
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µ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:\], " ")
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{"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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论