Two very similar functions involving sin() exhibit vastly different performance — why?

后端 未结 2 743
后悔当初
后悔当初 2021-02-14 02:47

Consider the following two programs that perform the same computations in two different ways:

// v1.c
#include 
#include 
int main(v         


        
2条回答
  •  庸人自扰
    2021-02-14 03:36

    In the first example, it runs 100000 loops of sin, 8192 times.

    In the second example, it runs 8192 loops of sin, 100000 times.

    Other than that and storing the result differently, I don't see any difference.

    However, what does make a difference is that the input is being changed for each loop in the second case. So I suspect what happens is that the sin value, at certain times in the loop, gets much easier to calculate. And that can make a big difference. Calculating sin is not entirely trivial, and it's a series calculation that loops until the exit condition is hit.

提交回复
热议问题