C++ performance of global variables

前端 未结 9 1926
谎友^
谎友^ 2021-01-04 07:32

For clarification: I know how evil globals are and when not to use them :)

  • Is there any performance penalty when accessing/setting a global variable vs. a loca
9条回答
  •  爱一瞬间的悲伤
    2021-01-04 08:00

    In addition to other answers, I would just point out that accessing a global variable in a multithreaded environment is likely to be more expensive because you need to ensure it is locked properly and the threads may wait in line to access it. With local variables it is not an issue.

提交回复
热议问题