Coding Practices which enable the compiler/optimizer to make a faster program

后端 未结 30 1895
一个人的身影
一个人的身影 2020-12-02 03:24

Many years ago, C compilers were not particularly smart. As a workaround K&R invented the register keyword, to hint to the compiler, that maybe it woul

30条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 04:12

    One optimization i have used in C++ is creating a constructor that does nothing. One must manually call an init() in order to put the object into a working state.

    This has benefit in the case where I need a large vector of these classes.

    I call reserve() to allocate the space for the vector, but the constructor does not actually touch the page of memory the object is on. So I have spent some address space, but not actually consumed a lot of physical memory. I avoid the page faults associated the associated construction costs.

    As i generate objects to fill the vector, I set them using init(). This limits my total page faults, and avoids the need to resize() the vector while filling it.

提交回复
热议问题