Design code to fit in CPU Cache?

前端 未结 7 1851
谎友^
谎友^ 2021-01-31 11:24

When writing simulations my buddy says he likes to try to write the program small enough to fit into cache. Does this have any real meaning? I understand that cache is faster

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 11:54

    If I were you, I would make sure I know which parts of code are hotspots, which I define as

    • a tight loop not containing any function calls, because if it calls any function, then the PC will be spending most of its time in that function,
    • that accounts for a significant fraction of execution time (like >= 10%) which you can determine from a profiler. (I just sample the stack manually.)

    If you have such a hotspot, then it should fit in the cache. I'm not sure how you tell it to do that, but I suspect it's automatic.

提交回复
热议问题