How fast can you make linear search?

前端 未结 20 1920
死守一世寂寞
死守一世寂寞 2020-12-23 21:46

I\'m looking to optimize this linear search:

static int
linear (const int *arr, int n, int key)
{
        int i = 0;
        while (i < n) {
                      


        
20条回答
  •  情书的邮戳
    2020-12-23 22:01

    In reality, the answer to this question is 100% dependent on the platform you're writing the code for. For example:

    CPU : Memory speed | Example CPU | Type of optimisation
    ========================================================================
        Equal          |    8086     | (1) Loop unrolling
    ------------------------------------------------------------------------
      CPU > RAM        |  Pentium    | (2) None
    
    1. Avoiding the conditional branch required to loop though the data will give a slight performance improvement.
    2. Once the CPU starts to get faster than the RAM, it doesn't matter how efficient the loop becomes (unless it's a really bad loop), it will be stalling due to having to wait for the data to be brought in from RAM. SIMD doesn't really help since the advantage of parallel testing is still outweighed by having to wait for more data to arrive. SIMD really comes into its own when you're CPU limited.

提交回复
热议问题