有和没有LINQ的线性搜索之间的区别

时间:2017-11-19 18:53:27

标签: c# linq lambda comparison linear-search

常规搜索算法与比较变量和lambda运算符之间有什么区别?

他们在内部做了什么不同的事情,你会如何描述它尽可能简单?两个版本都向用户提供相同的输出。

//LINQ: 
Console.WriteLine("Search for a word: ");
string userInput = Console.ReadLine();
var entries = logbook.Where(entry => entry.Any(item =>item.IndexOf(userInput, StringComparison.OrdinalIgnoreCase) > -1));
foreach (var entry in entries)
{   
    Console.WriteLine(string.Join(", ", entry));    
}
break;

//Regular foreach: 
Console.WriteLine("Search for a word: ");

string userInput = Console.ReadLine();
foreach (string[] logs in logbook)
{
    if (logs[0] == userInput)
        Console.WriteLine("Word found!\n\nTitle: " + logs[0] + "\nText: " + logs[1] + "\n");
    else if (logs[1] == userInput)
        Console.WriteLine("Word found!\n\nTitle: " + logs[0] + "\nText: " + logs[1]);
}

1 个答案:

答案 0 :(得分:0)

我希望您已准备好阅读一些代码。我已经更改了您的代码,因此它具有可比性。

foreach版本

以下是使用foreach的代码版本:

public static void Main()
{
    Console.WriteLine("Using foreach");
    string userInput = "1";
    var logbook = new List<string[]> { new string[] { "1", "2" } };
    foreach (string[] logs in logbook)
    {
        if (logs[0] == userInput)
            Console.WriteLine("Word found!\n\nTitle: " + logs[0] + "\nText: " + logs[1] + "\n");
        else if (logs[1] == userInput)
            Console.WriteLine("Word found!\n\nTitle: " + logs[0] + "\nText: " + logs[1]);
    }
}

以下是编译器为上述代码生成的内容:

public static void Main()
{
    Console.WriteLine("Using foreach");
    string b = "1";
    List<string[]> list = new List<string[]>();
    List<string[]> arg_2F_0 = list;
    string[] expr_1F = new string[2];
    expr_1F[0] = "1";
    string[] expr_27 = expr_1F;
    expr_27[1] = "2";
    arg_2F_0.Add(expr_27);
    List<string[]> list2 = list;
    List<string[]>.Enumerator enumerator = list2.GetEnumerator();
    try
    {
        while (enumerator.MoveNext())
        {
            string[] current = enumerator.Current;
            bool flag = current[0] == b;
            if (flag)
            {
                string[] expr_64 = new string[5];
                expr_64[0] = "Word found!\n\nTitle: ";
                string[] expr_6C = expr_64;
                expr_6C[1] = current[0];
                string[] expr_73 = expr_6C;
                expr_73[2] = "\nText: ";
                string[] expr_7B = expr_73;
                expr_7B[3] = current[1];
                string[] expr_82 = expr_7B;
                expr_82[4] = "\n";
                Console.WriteLine(string.Concat(expr_82));
            }
            else
            {
                bool flag2 = current[1] == b;
                if (flag2)
                {
                    Console.WriteLine("Word found!\n\nTitle: " + current[0] + "\nText: " + current[1]);
                }
            }
        }
    }
    finally
    {
        ((IDisposable)enumerator).Dispose();
    }
}

Lambda版本

以下是使用lambda的代码版本:

public static void Main()
{
    Console.WriteLine("Using lambda");
    string userInput = "1";
    var logbook = new List<string[]> { new string[] { "1", "2" } };
    var entries = logbook.Where(entry => entry.Any(item => item.IndexOf(userInput, StringComparison.OrdinalIgnoreCase) > -1));
    foreach (var entry in entries)
    {
        Console.WriteLine(string.Join(", ", entry));
    }
}

以下是编译器为上述代码生成的内容。请注意下面代码中属性为CompilerGenerated的私有类:

[CompilerGenerated]
private sealed class <>c__DisplayClass0_0
{
    public string userInput;

    public Func<string, bool> <>9__1;

    internal bool <Main>b__0(string[] entry)
    {
        IEnumerable<string> arg_20_0 = entry;
        Func<string, bool> arg_20_1;
        if ((arg_20_1 = this.<>9__1) == null)
        {
            arg_20_1 = (this.<>9__1 = new Func<string, bool>(this.<Main>b__1));
        }
        return arg_20_0.Any(arg_20_1);
    }

    internal bool <Main>b__1(string item)
    {
        return item.IndexOf(this.userInput, StringComparison.OrdinalIgnoreCase) > -1;
    }
}

public static void Main()
{
    Program.<>c__DisplayClass0_0 <>c__DisplayClass0_ = new Program.<>c__DisplayClass0_0();
    Console.WriteLine("Using lambda");
    <>c__DisplayClass0_.userInput = "1";
    List<string[]> list = new List<string[]>();
    List<string[]> arg_3A_0 = list;
    string[] expr_2A = new string[2];
    expr_2A[0] = "1";
    string[] expr_32 = expr_2A;
    expr_32[1] = "2";
    arg_3A_0.Add(expr_32);
    List<string[]> source = list;
    IEnumerable<string[]> enumerable = source.Where(new Func<string[], bool>(<>c__DisplayClass0_.<Main>b__0));
    IEnumerator<string[]> enumerator = enumerable.GetEnumerator();
    try
    {
        while (enumerator.MoveNext())
        {
            string[] current = enumerator.Current;
            Console.WriteLine(string.Join(", ", current));
        }
    }
    finally
    {
        if (enumerator != null)
        {
            enumerator.Dispose();
        }
    }
}

如果你注意Main方法中的代码,它会利用编译器生成的类来完成它的工作。

我使用SharLab来完成上述工作。