1

In my string array, I want to look up some text and return the the line number of the first occurrence as an int.

This is working;

public static int LookUpLineNumber(String[] item, string TextToLookUp)
{
    int m;
    for (m = 0; m < item.Count(); m++)
    {
        if (item[m].Contains(TextToLookUp))
        {
            break;
        }
    }
    return m++;
}

However, I am wondering if there is any way to optimize it for efficiency and length?

Speed comparison: (average time on 10.000 runs with an string array of size 10.000)

  1. Using my code:

    • 1,259ms
  2. Using Habib's code: Array.FindIndex<string>(item, r => r.Contains(TextToLookUp));

    • 0,906ms
jacobz
  • 3,191
  • 12
  • 37
  • 61

3 Answers3

1

Your current solution looks OK. You can have return m; instead of return m++.

If you want to shorten your code you can use Array.FindIndex<T> like:

public static int LookUpLineNumber(String[] item, string TextToLookUp)
{
    return Array.FindIndex<string>(item, r => r.Contains(TextToLookUp));
}

Not really sure if it would give you any performance gain.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • It anything, that is probably slightly slower. Would need to test that though – Simon Mar 21 '14 at 17:21
  • @Simon, Can't really say without a proper test. But what ever performance difference is there, it would be negligible. That is why I said it is just a short code. – Habib Mar 21 '14 at 17:23
  • This is what I was gonna suggest – Tsukasa Mar 21 '14 at 17:23
  • 1
    It does exact same `for` loop internally, with additional parameter checks: http://referencesource.microsoft.com/#mscorlib/system/array.cs#1132 – MarcinJuraszek Mar 21 '14 at 17:25
  • @Habib I agree. It's just lambda's tend to add a little overhead, but not much. It is more concise, I agree. – Simon Mar 21 '14 at 17:26
  • 2
    @Simon So I did a performance test with both mine and Habib's methods. Average time on 10.000 runs with an string array of size 10.000: Using my code: 1,259ms; Using Habib's code: 0,906ms. – jacobz Mar 21 '14 at 18:04
  • Ok, fair enough, I believe you. Would be interesting to know what it's doing under the covers. That's a much more marked difference than I would have thought. Did you try a regex instead of the contains? That *may be* faster, but again, you would have to try it and see. – Simon Mar 21 '14 at 18:13
  • Also see Max's comment below. IndexOf may be faster – Simon Mar 21 '14 at 18:15
  • @Simon, I would never use Regex for a simple contain, faster or not, It won't be readable, and even if it turns out to be faster *(which I doubt)*, it won't be readable enough. – Habib Mar 21 '14 at 18:16
0

If you need to do this multiple times, a suffix tree built out of the array would be the fastest approach:

http://en.wikipedia.org/wiki/Suffix_tree

However, if you are not re-using the array, then I think the approach you have is likely fastest, short of using a regex to do the contains, which may be faster if the regex is pre-compiled.

Simon
  • 2,840
  • 2
  • 18
  • 26
0

You can also do the following :-

Array.FindIndex(item,i=>i.Contains(TextToLookUp));

The above would work even if it is not sorted.

The above can be further optimized by using IndexOf operations instead of Contains and passing StringComparison.OrdinalIgnoreCase. Then you will have to compare it against 0.

Max
  • 4,067
  • 1
  • 18
  • 29