如何优化和预编译这个LINQ表达式?

时间:2010-10-14 12:12:23

标签: c# linq optimization

给出以下针对60%容忍的输入

"STACKOVERflow is a quesTions and ANSwers weBSITE"

我期待以下输出

// Extra spaces just to show %s
// 69%          50%  100%  22%!      33% 42%     14%
"Stackoverflow  Is   A     QuesTions And ANSwers Website"

QuestionsAnswers具有大写字符,但它们代表字符串的60%以下,因此应该保留。然后我想将每个字符串的第一个字符转换为大写。

我目前正在使用此方法

public static class StringExtender
{
    public static string ToTitleCase(this string str, double preserve)
    {
        return String.Join(" ",
            str.Split(' ')
            .Select(x => (x.Count(y => y.ToString() == y.ToString().ToUpper()) / (double)x.Length * 100) > preserve ? x.ToLower() : x)
            .Select(x =>
                String.Join(String.Empty,
                    x.Select((y, z) => z == 0 ? y.ToString().ToUpper() : y.ToString()).ToArray()
                )
            ).ToArray()
        );
    }
}

第一次运行时,我得到15000刻度线(Stopwatch.EllapsedTicks),其次是300。这似乎是第一次进行某种编辑...

  • 有没有办法在运行时编译它,所以第一次运行它就像下一个一样使用全速?
  • 有没有办法优化此代码?

完整代码(包括测量方法)

using System;
using System.Diagnostics;
using System.Linq;

public static class StopwatchExtender
{
    public static void Timer(this Stopwatch sw, Action x, int iterations, string name)
    {
        sw.Start();
        for (int i = 0; i < iterations; ++i)
        {
            x();
        }
        sw.Stop();

        Console.WriteLine("Name: {0}\nTicks: {1}\n", name, sw.ElapsedTicks);

        sw.Reset();
    }
}

public static class StringExtender
{
    public static string OP(this string str, double preserve)
    {
        return String.Join(" ",
            str.Split(' ')
            .Select(x => (x.Count(y => y.ToString() == y.ToString().ToUpper()) / (double)x.Length * 100) > preserve ? x.ToLower() : x)
            .Select(x =>
                String.Join(String.Empty,
                    x.Select((y, z) => z == 0 ? y.ToString().ToUpper() : y.ToString()).ToArray()
                )
            ).ToArray()
        );
    }

    public static string A01(this string str, double preserve)
    {
        return string.Join(" ",
            str.Split(' ')
                .Select(s => char.ToUpper(s[0]) + ((s.Count(c => char.IsUpper(c)) / (double)s.Length * 100) > preserve ? s.Substring(1).ToLower() : s.Substring(1)))
                .ToArray()
            );
    }
}

public class Program
{
    static void Main()
    {
        var sw = new Stopwatch();

        var str = "STACKOVERflow is a quesTions and ANSwers weBSITE";

        sw.Timer(() =>
        {
            str.OP(60);
            str.A01(60);
        }, 1, "Starup takes more time");

        sw.Timer(() =>
        {
            str.OP(60);
        }, 1000000, "OP solution");

        sw.Timer(() =>
        {
            str.A01(60);
        }, 1000000, "LukeH's answer");

        Console.ReadLine();
    }
}

结果

results

2 个答案:

答案 0 :(得分:3)

public static string ToTitleCase(this string str, double preserve)
{
    return string.Join(" ",
        str.Split(' ')
           .Select(s => s.Length == 0 ? s : char.ToUpper(s[0]) + ((s.Count(c => char.IsUpper(c)) / (double)s.Length * 100) > preserve ? s.Substring(1).ToLower() : s.Substring(1)))
           .ToArray());
}

(如果您使用的是.NET 4,请不要忘记删除最后的ToArray调用。)

答案 1 :(得分:1)

这并没有真正回答你的问题,但是一段时间以来一直在.NET Framework中使用了TitleCase()方法...查看TextInfo类:http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx