Simple for() loop benchmark takes the same time with any loop bound

后端 未结 5 1550
有刺的猬
有刺的猬 2020-12-11 19:50

I\'m willing to write a code that makes my CPU execute some operations and see how much time does he take to solve them. I wanted to make a loop going from i=0 to i<5000

5条回答
  •  时光说笑
    2020-12-11 20:14

    It is often very difficult to get the compiler to keep the code you are interested in profiling.

    I highly recommend profiling the actual code that does something useful as there are so many pitfalls in predicting the timing of actual code based on a number of separate measurements.

    If you wish to continue, one option is to declare a variable as volatile and assign your answer to it.

    volatile int a = i * 434243;

    Another is to create a function and return the value. You may need to disable in-lining.

    You are very unlikely to answer a question like "how long does a multiply take?". You are always answering questions like, "how long does it take to do a multiply and give me the answer?".

    You typically need to "keep temps" and inspect the assembler to make sure it is doing what you expect. If you are calling a function then you may want to compare calling a function that does nothing to try and eliminate calling overhead from your timing.

提交回复
热议问题