<p>Can the performance of this sequential search algorithm (taken from 
<a href="http://books.google.co.uk/books?id=to6M9_dbjosC&amp;dq=the+practice+of+programming&amp;pg=PP1&amp;ots=3YH6Ggq0_a&amp;sig=F2-ajdO37xA4iRec2sCyQF55Jjc&amp;hl=en&amp;sa=X&amp;oi=book_result&amp;resnum=1&amp;ct=result" rel="noreferrer">The Practice of Programming</a>) be improved using any of C's native utilities, e.g. if I set the i variable to be a register variable ?</p>

<pre><code>int lookup(char *word, char*array[])
{
    int i

    for (i = 0; array[i] != NULL; i++)
        if (strcmp(word, array[i]) == 0)
            return i;

    return -1;
}
</code></pre>

[{'answer_id': 15846, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 6, 'selected': True, 'text': "<p>Yes, but only very slightly. A much bigger performance improvement can be achieved by using better algorithms (for example keeping the list sorted and doing a binary search).</p>\n\n<p>In general optimizing a given algorithm only gets you so far. Choosing a better algorithm (even if it's not completely optimized) can give you a considerable (order of magnitude) performance improvement.</p>\n"}, {'answer_id': 15849, 'author': 'HS.', 'author_id': 1398, 'author_profile': 'https://Stackoverflow.com/users/1398', 'pm_score': 2, 'selected': False, 'text': '<p>I think, it will not make much of a difference. The compiler will already optimize it in that direction.</p>\n\n<p>Besides, the variable i does not have much impact, word stays constant throughout the function and the rest is too large to fit in any register. It is only a matter how large the cache is and if the whole array might fit in there.</p>\n\n<p>String comparisons are rather expensive computationally.</p>\n\n<p>Can you perhaps use some kind of hashing for the array before searching?</p>\n'}, {'answer_id': 15876, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 1, 'selected': False, 'text': "<p>If you're reading TPOP, you will next see how they make this search many times faster with different data structures and algorithms.</p>\n\n<p>But you can make things a bit faster by replacing things like</p>\n\n<pre><code>for (i = 0; i &lt; n; ++i)\n    foo(a[i]);\n</code></pre>\n\n<p>with</p>\n\n<pre><code>char **p = a;\nfor (i = 0; i &lt; n; ++i)\n    foo(*p);\n    ++p;\n</code></pre>\n\n<p>If there is a known value at the end of the array (e.g. NULL) you can eliminate the loop counter:</p>\n\n<pre><code>for (p = a; *p != NULL; ++p)\n    foo(*p)\n</code></pre>\n\n<p>Good luck, that's a great book!</p>\n"}, {'answer_id': 15915, 'author': 'popopome', 'author_id': 1556, 'author_profile': 'https://Stackoverflow.com/users/1556', 'pm_score': 2, 'selected': False, 'text': '<p>There is well-known technique as sentinal method.\nTo use sentinal method, you must know about the length of "array[]".\nYou can remove "array[i] != NULL" comparing by using sentinal.</p>\n\n<pre><code>int lookup(char *word, char*array[], int array_len)\n{\n    int i = 0;\n    array[array_len] = word;\n    for (;; ++i)\n        if (strcmp(word, array[i]) == 0) \n            break;\n    array[array_len] = NULL;\n    return (i != array_len) ? i : -1;\n}\n</code></pre>\n'}, {'answer_id': 16094, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 0, 'selected': False, 'text': "<p>To optimize that code the best bet would be to rewrite the strcmp routine since you are only checking for equality and don't need to evaluate the entire word.</p>\n\n<p>Other than that you can't do much else. You can't sort as it appears you are looking for text within a larger text. Binary search won't work either since the text is unlikely to be sorted.</p>\n\n<p>My 2p (C-psuedocode):</p>\n\n<pre><code>wrd_end = wrd_ptr + wrd_len;\narr_end = arr_ptr - wrd_len;\nwhile (arr_ptr &lt; arr_end)\n{\n    wrd_beg = wrd_ptr; arr_beg = arr_ptr;\n    while (wrd_ptr == arr_ptr)\n    {\n        wrd_ptr++; arr_ptr++;\n        if (wrd_ptr == wrd_en)\n            return wrd_beg;\n    }\n    wrd_ptr++;\n}\n</code></pre>\n"}, {'answer_id': 17209, 'author': 'Mat Noguchi', 'author_id': 1799, 'author_profile': 'https://Stackoverflow.com/users/1799', 'pm_score': 0, 'selected': False, 'text': '<p>Realistically, setting I to be a register variable won\'t do anything that the compiler wouldn\'t do already.</p>\n\n<p>If you are willing to spend some time upfront preprocessing the reference array, you should google "The World\'s Fastest Scrabble Program" and implement that. Spoiler: it\'s a DAG optimized for character lookups.</p>\n'}, {'answer_id': 71698, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 0, 'selected': False, 'text': '<p>Mark Harrison: Your for loop will never terminate! (++p is indented, but is not actually within the for :-)</p>\n\n<p>Also, switching between pointers and indexing will generally have no effect on performance, nor will adding register keywords (as mat already mentions) -- the compiler is smart enough to apply these transformations where appropriate, and if you tell it enough about your cpu arch, it will do a better job of these than manual psuedo-micro-optimizations. </p>\n'}, {'answer_id': 252622, 'author': 'Zan Lynx', 'author_id': 13422, 'author_profile': 'https://Stackoverflow.com/users/13422', 'pm_score': 0, 'selected': False, 'text': '<p>A faster way to match strings would be to store them Pascal style.  If you don\'t need more than 255 characters per string, store them roughly like this, with the count in the first byte:</p>\n\n<pre><code>char s[] = "\\x05Hello";\n</code></pre>\n\n<p>Then you can do:</p>\n\n<pre><code>for(i=0; i&lt;len; ++i) {\n    s_len = strings[i][0];\n    if(\n        s_len == match_len\n        &amp;&amp; strings[i][s_len] == match[s_len-1]\n        &amp;&amp; 0 == memcmp(strings[i]+1, match, s_len-1)\n    ) {\n        return 1;\n    }\n}\n</code></pre>\n\n<p>And to get really fast, add memory prefetch hints for string start + 64, + 128 and the start of the next string.  But that\'s just crazy. :-)</p>\n'}, {'answer_id': 252650, 'author': 'Zan Lynx', 'author_id': 13422, 'author_profile': 'https://Stackoverflow.com/users/13422', 'pm_score': 0, 'selected': False, 'text': "<p>Another fast way to do it is to get your compiler to use a SSE2 optimized memcmp.  Use fixed-length char arrays and align so the string starts on a 64-byte alignment.  Then I believe you can get the good memcmp functions if you pass const char match[64] instead of const char *match into the function, or strncpy match into a 64,128,256,whatever byte array.</p>\n\n<p>Thinking a bit more about this, these SSE2 match functions might be part of packages like Intel's and AMD's accelerator libraries.  Check them out.</p>\n"}, {'answer_id': 20116661, 'author': 'alchak', 'author_id': 3016771, 'author_profile': 'https://Stackoverflow.com/users/3016771', 'pm_score': -1, 'selected': False, 'text': '<pre><code>/* there is no more quick  */\nint lookup(char *word, char*array[])\n{\n    int i;\n    for(i=0; *(array++) != NULL;i++)\n        if (strcmp(word, *array) == 0)\n            return i;\n    return -1;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/15838', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/381/']