effect of goto on C++ compiler optimization

后端 未结 4 2331
执笔经年
执笔经年 2021-02-18 21:26

What are the performance benefits or penalties of using goto with a modern C++ compiler?

I am writing a C++ code generator and use of

4条回答
  •  长情又很酷
    2021-02-18 21:54

    I agree heartily with David Hammen's answer, but I only have one point to add.

    When people are taught about compilers, they are taught about all the wonderful optimizations that compilers can do.

    They are not taught that the actual value of this depends on who the user is.

    If the code you are writing (or generating) and compiling contains very few function calls and could itself consume a large fraction of some other program's time, then yes, compiler optimization matters.

    If the code being generated contains function calls, or if for some other reason the program counter spends a small fraction of its time in the generated code, it's not worth worrying about. Why? Because even if that code could be so aggressively optimized that it took zero time, it would save no more than that small fraction, and there are probably much bigger performance issues, that the compiler can't fix, that are happy to be evading your attention.

提交回复
热议问题