Reputation: 147
just wondering if "go" compiler uses any sort of optimization such as constant folding.
https://en.wikipedia.org/wiki/Constant_folding
searched through google but couldn't find the answer i'm looking for.
Upvotes: 3
Views: 741
Reputation: 166785
Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime.
The Go Programming Language Specification
Constant expressions may contain only constant operands and are evaluated at compile time.
import "math/big"
Package big implements arbitrary-precision arithmetic (big numbers).
Go constant expressions are evaluated at compile time. The Go gc compiler, written in Go, uses package big
to evaluate numeric constant expressions.
Upvotes: 8
Reputation: 1233
Try to write a simple program: eg.
package main
import "fmt"
func main() {
fmt.Println(12345*1000)
}
And now compile it to assembly
go tool compile -S examle.go
And now find 12345 in the result and you will have an answer.
Upvotes: 1