C# 用C语言编写以下代码的最快方法是什么?
在代码C#下,最快的写法是什么?我尝试了许多方法,但无法提高性能。任何解决方案都将受到高度赞赏 此ToNumber在for循环中称为1000,这需要花费很多时间来处理C# 用C语言编写以下代码的最快方法是什么?,c#,asp.net,C#,Asp.net,在代码C#下,最快的写法是什么?我尝试了许多方法,但无法提高性能。任何解决方案都将受到高度赞赏 此ToNumber在for循环中称为1000,这需要花费很多时间来处理 public static Dictionary<string, string> demoText = new Dictionary<string, string>() {
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;
}
publicstaticdictionary demoText=newdictionary()
{
{"", "09"},
{“ff”,“19”},
{“fdffsdfd”,“29”},
{“dfsd”,“39”},
{“DFSF”,“49”},
{“dsdfs”,“95”},
{“hjghjgh”,“79”},
{“ghjghj”,“89”},
{“hjghjgh”,“99”},
{“hjghj”,“190”},
{“ghjghj”,“191”},
{“hjghj”,“12”},
{“hghjg”,“133”},
{“GHJGHGH”,“124”},
{“hjghjgh”,“135”},
{“jghjgh”,“196”},
{“jjj”,“179”},
{“ttt”,“198”},
{“sdfsdf”,“199”},
{“sdffs”,“290”},
{“fsdfdf”,“291”},
{“fsdkfhsdf”,“22”},
{“fdfgdfg”,“23”},
{“fgdfg”,“339”},
};
私有静态双色调数字(字符串值,布尔isScore)
{
var结果=Double.MinValue;
如果(!string.IsNullOrEmpty(值))
{
国际单项体育联合会(isScore)
{
尝试
{
result=Double.Parse(demoext[value.ToLowerInvariant()]);
}
捕获(例外)
{
如果(value.ToLowerInvariant().Equals(“ttt”))结果=0.0;
}
}
其他的
{
尝试
{
result=Double.Parse(value.Replace(“%”,“”));
}
捕获(异常){}
}
}
返回结果;
}
根据我的评论
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;
}
}
}
}
publicstaticdictionary DemoText=新字典(StringComparer.InvariantIgnoreCase)
{
{"", 9},
{“ff”,19},
...
};
私有静态双色调数字(字符串值,布尔isScore)
{
if(string.IsNullOrEmpty(value))
返回Double.MinValue;
国际单项体育联合会(isScore)
{
if(DemoText.TryGetValue(值,输出双x)
{
返回x;
}
其他的
{
返回Double.MinValue;
}
}
其他的
{
if(Double.TryParse(value.Replace(“%”,“”),out双精度x)
{
返回x;
}
其他的
{
返回Double.MinValue;
}
}
}
}
<代码> >“TTT”<代码> > <代码> 0 /代码>在字典中。将贾景晖的注释加上字典中的百分比值或百分比仅出现在某些地方,考虑EG<代码> TrimeD/C> >而不是<代码>替换< /Calp>
避免在控制流中使用异常;它们非常昂贵,特别是如果您只是将它们抛出的话我注意到所有的值都是
int
s。我不知道为什么要将它们存储为双精度,但不管我怎么想,您都有理由这样做
我运行了一些基准测试,您的代码花了~1400ms
进行100次迭代
如果此备选方案需要~200ms
进行1000000次
迭代:
- 第一,因为我们没有非常大的数据量,我们可以将它们存储在一个2D数组中而不是字典中。但是让我们看看我们在大多数常见情况下所做的事情:我们正在迭代<代码>键<代码>。因此,最好将它们存储在<代码> SOA窗体中以获得更好的CPU缓存利用率:
公共静态字符串[]文本=新字符串[]
{
“,”ff“,”fdffsdfd“,”DFSD“,”DFSFSF“,”DSDFS“,
“hjghjgh”、“GHJGH”、“hjghjgh”、“hjghj”、“ghjghj”、“hjghj”、“hghjg”、“GHJGHGH”、“hjghjgh”,
“JGHGH”、“jjj”、“ttt”、“sdfsdf”、“sdffs”、“FSDFF”、“fsdkfhsdf”、“fdfgdfgdfg”、“fgdfgdfg”
};
公共静态双精度[]值=新双精度[]
{
9,
19,
29 ,
39 ,
49 ,
95 ,
79 ,
89,
99 ,
190 ,
191 ,
12,
133 ,
124 ,
135 ,
196 ,
179 ,
198 ,
199 ,
290 ,
291,
22 ,
23,
339,
};
现在,让我们用if替换try-catch(注意try-catch有两个缺点:A
:JIT编译器不能很好地优化try-catch中的汇编代码,B
:if的指令比try-catch少得多)
private static double ToNumber(字符串值,bool为