在 C# 中编写以下代码的最快方法是什么?

时间:2021-04-09 05:42:01

标签: c# asp.net

编写下面的 C# 代码最快的方法是什么?我尝试了很多方法,但无法提高性能,任何解决方案都将受到高度赞赏。

这个 ToNumber 在 for 循环中被称为 1000,需要很多时间来处理。

     public static Dictionary<string, string> demoText = new Dictionary<string, string>()
                                {
                                        {"", "09"},
                                        {"ff","19"},
                                        {"fdffsdfd","29" },
                                        {"dfsdfsdfsd","39" },
                                        {"dfsdfsf","49" },
                                        {"dsdsdfs","95" },
                                        {"hjghjgh","79" },
                                        { "ghjghj","89"},
                                        {"hjghjgh","99" },
                                        {"hjghj","190" },
                                        {"ghjghj","191" },
                                        { "hjghj","12"},
                                        {"hghjg","133" },
                                        {"ghjghjgh","124" },
                                        {"hjghjgh","135" },
                                        {"jghjgh","196" },
                                        {"jjj","179" },
                                        {"ttt","198" },
                                        {"sdfsdf","199" },
                                        {"sdffs","290" },
                                        { "fsdfdf","291"},
                                        {"fsdkfhsdf","22" },
                                        { "fdfgdfgdfg","23"},
                                        { "fgdfgdfg","339"},
        
                                };
        
            private static Double ToNumber(string value, bool isScore)
            {
                var result = Double.MinValue;
                if (!string.IsNullOrEmpty(value))
                {
                    if (isScore)
                    {
                        try
                        {
                            result = Double.Parse(DemoText[value.ToLowerInvariant()]);
                        }
                        catch (Exception)
                        {
                            if (value.ToLowerInvariant().Equals("ttt")) result = 0.0;
                        }
                    }
                    else
                    {
                        try
                        {
                            result = Double.Parse(value.Replace("%", ""));
                        }
                        catch (Exception) { }
                    }
                }
                return result;
            }

2 个答案:

答案 0 :(得分:1)

根据我的评论

        public static Dictionary<string, double> DemoText = new Dictionary<string, double>(StringComparer.InvariantIgnoreCase)
                            {
                                    {"", 9},
                                    {"ff",19},
                                    ...
                            };
    
        private static Double ToNumber(string value, bool isScore)
        {
                if (string.IsNullOrEmpty(value))
                    return Double.MinValue;

            
                if (isScore)
                {
                    if(DemoText.TryGetValue(value, out double x)
                    {
                        return x;
                    }
                    else 
                    {
                        return Double.MinValue;
                    }
                }
                else
                {
                    if(Double.TryParse(value.Replace("%", ""), out double x)
                    {
                        return x;
                    }
                    else 
                    {
                        return Double.MinValue;
                    }
                }
            }
        }

value"ttt" 放在字典中作为 0.0。通过将百分比值放入字典中,或者如果百分比仅出现在某些地方,可以合并 Marc 的评论,例如考虑 TrimEnd 而不是 Replace

避免在控制流中使用异常;它们非常昂贵,尤其是如果你只是把它们扔掉

答案 1 :(得分:0)

我注意到所有的值都是 int。我不知道你为什么要将它们存储为 double,但无论我认为你有什么理由这样做。

我运行了一些基准测试,您的代码进行了 ~1400ms 次迭代。

此替代方法在 100 次迭代中使用 ~200ms

  • 首先,因为我们没有大量数据,我们可以将它们存储在二维数组而不是字典中。但是让我们看看在最常见的情况下我们做了什么:我们正在迭代 1000000。因此,最好以 keys 形式存储它们以提高 CPU 缓存利用率:
SoA

现在让我们用 if's 替换 try catches(注意 try catch 有两个缺点:public static string[] Texts = new string[] { "", "ff", "fdffsdfd", "dfsdfsdfsd", "dfsdfsf", "dsdsdfs", "hjghjgh", "ghjghj", "hjghjgh", "hjghj", "ghjghj", "hjghj", "hghjg", "ghjghjgh", "hjghjgh", "jghjgh", "jjj", "ttt", "sdfsdf", "sdffs", "fsdfdf", "fsdkfhsdf", "fdfgdfgdfg", "fgdfgdfg" }; public static double[] Values = new double[] { 9, 19, 29 , 39 , 49 , 95 , 79 , 89, 99 , 190 , 191 , 12, 133 , 124 , 135 , 196 , 179 , 198 , 199 , 290 , 291, 22 , 23, 339, }; :JIT 编译器不能很好地优化 try catch 中的汇编代码和 A:If 有很多指令而不是尝试捕获)。

B

我相信可以使用内在函数或任何其他附加信息/上下文进行更好的优化。

相关问题