代码之家  ›  专栏  ›  技术社区  ›  pandascope

golang编译器是否使用常数折叠?

  •  1
  • pandascope  · 技术社区  · 7 年前

    只是想知道“go”编译器是否使用任何类型的优化,例如常数折叠。

    https://en.wikipedia.org/wiki/Constant_folding

    在谷歌上搜索,但找不到我要找的答案。

    2 回复  |  直到 6 年前
        1
  •  6
  •   peterSO    7 年前

    Constant folding - Wikipedia

    常数折叠是识别和计算常数的过程 表达式在编译时,而不是在运行时进行计算。


    The Go Programming Language Specification

    Constant expressions

    常量表达式只能包含常量操作数,并且是 在编译时计算。


    Package big

    import "math/big"
    

    包big实现任意精度算法(大数字)。


    Go常量表达式在编译时求值。用Go编写的Go gc编译器使用包 big

        2
  •  1
  •   lofcek    7 年前

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println(12345*1000)
    }
    

    现在把它编译成汇编

    go tool compile -S examle.go
    

    现在在结果中找到12345,你就会得到答案。

    推荐文章