gpt4 book ai didi

Go 语言字符串常量

转载 作者:IT王子 更新时间:2023-10-29 01:23:48 26 4
gpt4 key购买 nike

Go 中使用的内联字符串和常量字符串之间是否存在差异,或者这是否完全由编译器优化?

具有 100B 次迭代的示例基准代码:

package main

import "fmt"
import "time"
import "log"

func main() {
// String
start := time.Now()
for i := 0; i < 100000000; i++ {
x := "My String"
if i % 1000000 == 0{
fmt.Printf(x)
}
}
elapsed := time.Since(start)
log.Printf("\nTook %s", elapsed)

// Constant
start2 := time.Now()
const MY_STRING = "My String 2"
for i := 0; i < 100000000; i++ {
x := MY_STRING
if i % 1000000 == 0 {
fmt.Printf(x)
}
}

elapsed2 := time.Since(start2)
log.Printf("\nTook %s", elapsed2)

// Proof that the timer does work :)
start3 := time.Now()
time.Sleep(100 * time.Millisecond)
elapsed3 := time.Since(start3)
log.Printf("\nTook %s", elapsed3)

}

输出:

My StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy String2009/11/10 23:00:00 
Took 0
My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 22009/11/10 23:00:00
Took 0
2009/11/10 23:00:00
Took 100ms

Program exited.

最佳答案

两者编译后的代码没有区别,所以应该不会影响你的结果。以下面的示例文件为例:

package foo

func foo() string {
x := "Foo"
return x
}

const MY_STRING = "Bar"

func bar() string {
x := MY_STRING
return x
}

汇编器输出显示这两个函数相同(模不同的字符串常量):

$ go tool 6g -S foo.go

--- prog list "foo" ---
0000 (foo.go:3) TEXT foo+0(SB),$0-16
0001 (foo.go:3) FUNCDATA $0,gcargs·0+0(SB)
0002 (foo.go:3) FUNCDATA $1,gclocals·0+0(SB)
0003 (foo.go:3) TYPE ~anon0+0(FP){string},$16
0004 (foo.go:4) LEAQ go.string."Foo"+0(SB),BX
0005 (foo.go:4) MOVQ (BX),CX
0006 (foo.go:4) MOVQ 8(BX),BP
0007 (foo.go:5) MOVQ CX,~anon0+0(FP)
0008 (foo.go:5) MOVQ BP,~anon0+8(FP)
0009 (foo.go:5) RET ,

--- prog list "bar" ---
0010 (foo.go:10) TEXT bar+0(SB),$0-16
0011 (foo.go:10) FUNCDATA $0,gcargs·1+0(SB)
0012 (foo.go:10) FUNCDATA $1,gclocals·1+0(SB)
0013 (foo.go:10) TYPE ~anon0+0(FP){string},$16
0014 (foo.go:11) LEAQ go.string."Bar"+0(SB),BX
0015 (foo.go:11) MOVQ (BX),CX
0016 (foo.go:11) MOVQ 8(BX),BP
0017 (foo.go:12) MOVQ CX,~anon0+0(FP)
0018 (foo.go:12) MOVQ BP,~anon0+8(FP)
0019 (foo.go:12) RET ,

如您所见,字符串常量在两种情况下的加载方式相同。

关于Go 语言字符串常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21726123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com