Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go lang string constants

Is there a difference between an inline string and a constant string being used in Go, or is this being optimized entirely by the compiler?

Example benchmark code with 100B iterations:

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)

}

Output:

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.
like image 977
RobinUS2 Avatar asked Feb 12 '14 11:02

RobinUS2


2 Answers

There is no difference in the compiled code between the two, so it should not affect your results. Take the following example file:

package foo

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

const MY_STRING = "Bar"

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

The assembler output shows that the two functions are the same (modulo the different string constants):

$ 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     ,

As you can see, the string constant is loaded the same way in both cases.

like image 68
James Henstridge Avatar answered Oct 03 '22 13:10

James Henstridge


There is no real difference. If you measure it you will see that the both run at the same speed. As all strings are constant in Go this should not be much of an surprise.

If your are really into this type of micro optimizations take a look at the generated assembler with

go tool 6g -S main.go

like image 34
Volker Avatar answered Oct 03 '22 11:10

Volker