C++ performance of global variables

前端 未结 9 1909
谎友^
谎友^ 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 07:49

    There is no performance penalty, either way, that you should be concerned about. In adition to what everyone else has said, you should also bear in mind the paging overhead. Local instance variables are fetched from the object structure which has likely (?) already been paged into cache memory). Global variables, on the other hand, may cause a different pattern of virtual memory paging.

    Again, the performance is really not worth any consideration on your part.

提交回复
热议问题