0

i have function which finds a number in a table:

int find(int arrf[], int len, int seek)
{
    for (int i = 0; i < len; ++i)
        if (arrf[i] == seek) 
            return i;
    return -1;
}

Is there a faster way to do this?

Aditi Rawat
  • 784
  • 1
  • 12
  • 15

1 Answers1

0

For linear search two optimizations come to mind:

  1. Build a vectorized implementation that uses SIMD instructions to do multiple comparisons at the same time.
  2. Parallelize search to multiple CPUs. That only makes sense if the array to search is sufficiently large to warrant multi-threading overhead.

Intel Parallel STL can do both optimizations for you.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271