Best way to test code speed in C++ without profiler, or does it not make sense to try?

后端 未结 8 2018
忘了有多久
忘了有多久 2021-02-06 02:01

On SO, there are quite a few questions about performance profiling, but I don\'t seem to find the whole picture. There are quite a few issues involved and most Q & A ignore

8条回答
  •  名媛妹妹
    2021-02-06 03:04

    Is it safe to say you're asking two questions?

    • Which one is faster, and by how much?

    • And why is it faster?

    For the first, you don't need high precision timers. All you need to do is run them "long enough" and measure with low precision timers. (I'm old-fashioned, my wristwatch has a stop-watch function, and it is entirely good enough.)

    For the second, surely you can run the code under a debugger and single-step it at the instruction level. Since the basic operations are so simple, you will be able to easily see roughly how many instructions are required for the basic cycle.

    Think simple. Performance is not a hard subject. Usually, people are trying to find problems, for which this is a simple approach.

提交回复
热议问题