使用string []进行包含的最快方法

时间:2023-02-04 19:16:43

I am getting back a "string[]" from a 3rd party library. I want to do a contains on it. what is the most efficient way of doing this?

我从第三方库中找回“string []”。我想做一个包含就可以了。这样做最有效的方法是什么?

6 个解决方案

#1


Array.IndexOf:

bool contains = Array.IndexOf(arr, value) >= 0;

Or just use LINQ:

或者只使用LINQ:

bool contains = arr.Contains(value);

LINQ should be "fast enough" for most purposes.

对于大多数目的,LINQ应该“足够快”。

#2


If you are only checking a single time, use Array.IndexOf or the LINQ Contains method like Marc proposed. If you are checking several times, it might be faster to first convert the string array into a HashSet<string>.

如果您只检查一次,请使用Array.IndexOf或像Marc提议的LINQ Contains方法。如果要多次检查,首先将字符串数组转换为HashSet 可能会更快。

#3


Unless you know the String array is sorted by a particular order the most efficient thing you can do is linear algorithm (i.e. compare each string in the array until you find a match or the end of the array.

除非您知道String数组按特定顺序排序,否则您可以做的最有效的事情是线性算法(即比较数组中的每个字符串,直到找到匹配或数组的结尾)。

If the array is sorted a binary search is much faster.

如果对数组进行排序,则二进制搜索要快得多。

Another way to optimize the algorithm (although the complexity is not reduced) is to vectorize the string comparisons.

优化算法的另一种方法(尽管不降低复杂性)是对字符串比较进行矢量化。

#4


I'm fairly certain that a for loop is faster, if absolute speed is your concern. I.e.,

如果绝对速度是您的关注点,我相当肯定for循环更快。即,

for (int i = 0; i < arr.Length; ++i)
  if (arr[i] == value) return true;
return false;

#5


If you're searching once or twice, use a linear search or IndexOf.

如果您要搜索一次或两次,请使用线性搜索或IndexOf。

If you're searching a few times, put the strings into a HashSet.

如果您正在搜索几次,请将字符串放入HashSet中。

If you're searching zillions of times in a time-critical fashion, use a HashSet and manage its bucket count yourself.

如果您正在以时间关键的方式搜索数十亿次,请使用HashSet并自行管理其桶数。

#6


You can use the IEnumerable.Foreach Custom Extension

您可以使用IEnumerable.Foreach自定义扩展

public static class CollectionExtensions
{
    public static void ForEach<T>(this IEnumerable list, Action<T> action)
    {
        foreach (T item in list)
        {
            action(item);
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        String[] list = new String[] { "Word1", "Word2", "Word3" };

        list.ForEach<String>(p => Console.WriteLine(p));
        list.ForEach(delegate(String p) { Console.WriteLine(p); });
    }
}

Hope this help's.

希望这可以帮助。

#1


Array.IndexOf:

bool contains = Array.IndexOf(arr, value) >= 0;

Or just use LINQ:

或者只使用LINQ:

bool contains = arr.Contains(value);

LINQ should be "fast enough" for most purposes.

对于大多数目的,LINQ应该“足够快”。

#2


If you are only checking a single time, use Array.IndexOf or the LINQ Contains method like Marc proposed. If you are checking several times, it might be faster to first convert the string array into a HashSet<string>.

如果您只检查一次,请使用Array.IndexOf或像Marc提议的LINQ Contains方法。如果要多次检查,首先将字符串数组转换为HashSet 可能会更快。

#3


Unless you know the String array is sorted by a particular order the most efficient thing you can do is linear algorithm (i.e. compare each string in the array until you find a match or the end of the array.

除非您知道String数组按特定顺序排序,否则您可以做的最有效的事情是线性算法(即比较数组中的每个字符串,直到找到匹配或数组的结尾)。

If the array is sorted a binary search is much faster.

如果对数组进行排序,则二进制搜索要快得多。

Another way to optimize the algorithm (although the complexity is not reduced) is to vectorize the string comparisons.

优化算法的另一种方法(尽管不降低复杂性)是对字符串比较进行矢量化。

#4


I'm fairly certain that a for loop is faster, if absolute speed is your concern. I.e.,

如果绝对速度是您的关注点,我相当肯定for循环更快。即,

for (int i = 0; i < arr.Length; ++i)
  if (arr[i] == value) return true;
return false;

#5


If you're searching once or twice, use a linear search or IndexOf.

如果您要搜索一次或两次,请使用线性搜索或IndexOf。

If you're searching a few times, put the strings into a HashSet.

如果您正在搜索几次,请将字符串放入HashSet中。

If you're searching zillions of times in a time-critical fashion, use a HashSet and manage its bucket count yourself.

如果您正在以时间关键的方式搜索数十亿次,请使用HashSet并自行管理其桶数。

#6


You can use the IEnumerable.Foreach Custom Extension

您可以使用IEnumerable.Foreach自定义扩展

public static class CollectionExtensions
{
    public static void ForEach<T>(this IEnumerable list, Action<T> action)
    {
        foreach (T item in list)
        {
            action(item);
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        String[] list = new String[] { "Word1", "Word2", "Word3" };

        list.ForEach<String>(p => Console.WriteLine(p));
        list.ForEach(delegate(String p) { Console.WriteLine(p); });
    }
}

Hope this help's.

希望这可以帮助。