## How fast can you make linear search?

Question!

I'm looking to optimize this linear search:

``````static int
linear (const int *arr, int n, int key)
{
int i = 0;
while (i < n) {
if (arr [i] >= key)
break;
++i;
}
return i;
}
``````

The array is sorted and the function is supposed to return the index of the first element that is greater or equal to the key. They array is not large (below 200 elements) and will be prepared once for a large number of searches. Array elements after the n-th can if necessary be initialized to something appropriate, if that speeds up the search.

No, binary search is not allowed, only linear search.

``````static int linear(const int *array, int arraySize, int key) {
``` By : strager ```
``` loop backwards, this might be translated... // loop backward for (int i = arraySize - 1; i By : Michael Buen this might force vector instructions (suggested by Gman): for (int i = 0; i By : Anycorn This video can help you solving your question :) By: admin ```
``` Related Questions Optimizing a search algorithm in C Is there a performance difference between i++ and ++i in C? profile-guided optimization (C) Which compiles to faster code: “n * 3” or “n+(n*2)”? (adsbygoogle = window.adsbygoogle || []).push({}); How do you normally set up your compiler's optimization settings? faster Math.exp() via JNI? Spatial Data Structures in C What is the fastest way to convert float to int on x86 Is there memset() that accepts integers larger than char? What coding techniques do you use for optimising C programs? [closed] Why doesn't GCC optimize structs? Should Local Variable Initialisation Be Mandatory? Performance challenge: NAL Unit Wrapping State of “memset” functionality in C++ with modern compilers Fastest way to do a case-insensitive substring search in C/C++? How cache memory works? Russian Peasant Multiplication How to synchronize C & C++ libraries with minimal performance penalty? How is Tail Call Optimization implemented in DrScheme? Does anybody have any experience with SSEPlus? ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com ```