Luk*_*741 10 c# performance parsing decimal
我注意到decimal.Parse(number, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture)比基于Jeffrey Sax的代码的自定义十进制解析方法慢大约100%,该代码来自Faster替代Convert.ToDouble
public static decimal ParseDecimal(string input) {
bool negative = false;
long n = 0;
int len = input.Length;
int decimalPosition = len;
if (len != 0) {
int start = 0;
if (input[0] == '-') {
negative = true;
start = 1;
}
for (int k = start; k < len; k++) {
char c = input[k];
if (c == '.') {
decimalPosition = k +1;
} else {
n = (n *10) +(int)(c -'0');
}
}
}
return new decimal(((int)n), ((int)(n >> 32)), 0, negative, (byte)(len -decimalPosition));
}
Run Code Online (Sandbox Code Playgroud)
我认为这是因为本机decimal.Parse旨在与数字风格和文化信息斗争.
但是,上面提到的方法不使用第三个参数hi字节,new decimal因此它不适用于较大的数字.
是否有更快的替代方法decimal.Parse将只包含数字和小数点的字符串转换为十进制,这将适用于大数字?
编辑:基准:
var style = System.Globalization.NumberStyles.AllowDecimalPoint;
var culture = System.Globalization.CultureInfo.InvariantCulture;
System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
s.Reset();
s.Start();
for (int i=0; i<10000000; i++)
{
decimal.Parse("20000.0011223344556", style, culture);
}
s.Stop();
Console.WriteLine(s.Elapsed.ToString());
s.Reset();
s.Start();
for (int i=0; i<10000000; i++)
{
ParseDecimal("20000.0011223344556");
}
s.Stop();
Console.WriteLine(s.Elapsed.ToString());
Run Code Online (Sandbox Code Playgroud)
输出:
00:00:04.2313728
00:00:01.4464048
Run Code Online (Sandbox Code Playgroud)
在这种情况下,自定义ParseDecimal明显快于decimal.Parse.
感谢您的所有评论,这让我有了更多的见解.最后我做了如下.如果输入太长,则它将输入字符串分开,并使用long解析第一部分,其余部分使用int,它仍然比decimal.Parse快.
这是我的最终生产代码:
public static int[] powof10 = new int[10]
{
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000
};
public static decimal ParseDecimal(string input)
{
int len = input.Length;
if (len != 0)
{
bool negative = false;
long n = 0;
int start = 0;
if (input[0] == '-')
{
negative = true;
start = 1;
}
if (len <= 19)
{
int decpos = len;
for (int k = start; k < len; k++)
{
char c = input[k];
if (c == '.')
{
decpos = k +1;
}else{
n = (n *10) +(int)(c -'0');
}
}
return new decimal((int)n, (int)(n >> 32), 0, negative, (byte)(len -decpos));
}else{
if (len > 28)
{
len = 28;
}
int decpos = len;
for (int k = start; k < 19; k++)
{
char c = input[k];
if (c == '.')
{
decpos = k +1;
}else{
n = (n *10) +(int)(c -'0');
}
}
int n2 = 0;
bool secondhalfdec = false;
for (int k = 19; k < len; k++)
{
char c = input[k];
if (c == '.')
{
decpos = k +1;
secondhalfdec = true;
}else{
n2 = (n2 *10) +(int)(c -'0');
}
}
byte decimalPosition = (byte)(len -decpos);
return new decimal((int)n, (int)(n >> 32), 0, negative, decimalPosition) *powof10[len -(!secondhalfdec ? 19 : 20)] +new decimal(n2, 0, 0, negative, decimalPosition);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基准代码:
const string input = "[inputs are below]";
var style = System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowLeadingSign;
var culture = System.Globalization.CultureInfo.InvariantCulture;
System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
s.Reset();
s.Start();
for (int i=0; i<10000000; i++)
{
decimal.Parse(input, style, culture);
}
s.Stop();
Console.WriteLine(s.Elapsed.ToString());
s.Reset();
s.Start();
for (int i=0; i<10000000; i++)
{
ParseDecimal(input);
}
s.Stop();
Console.WriteLine(s.Elapsed.ToString());
Run Code Online (Sandbox Code Playgroud)
在我的i7 920上的结果:
输入:123.456789
00:00:02.7292447
00:00:00.6043730
Run Code Online (Sandbox Code Playgroud)
输入:999999999999999123.456789
00:00:05.3094786
00:00:01.9702198
Run Code Online (Sandbox Code Playgroud)
输入:1.0
00:00:01.4212123
00:00:00.2378833
Run Code Online (Sandbox Code Playgroud)
输入:0
00:00:01.1083770
00:00:00.1899732
Run Code Online (Sandbox Code Playgroud)
输入:-3.3333333333333333333333333333333
00:00:06.2043707
00:00:02.0373628
Run Code Online (Sandbox Code Playgroud)
如果输入仅包含0-9 ,. 并且可选地 - 在开始时,这个自定义函数明显更快地将字符串解析为十进制.