decimal.Parse 的更快替代品

Faster alternative to decimal.Parse

我注意到 decimal.Parse(number, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture) 比基于来自 Faster alternative to Convert.ToDouble

的 Jeffrey Sax 代码的自定义十进制解析方法慢大约 100%
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));
}

我认为这是因为原生 decimal.Parse 旨在与数字样式和文化信息作斗争。

但是,上述方法在 new decimal 中没有使用第 3 个参数高位字节,因此它不适用于更大的数字。

是否有比 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());

输出:

00:00:04.2313728
00:00:01.4464048

自定义 ParseDecimal 在这种情况下明显快于 decimal.Parse。

Sax 的方法之所以快,有两个原因。第一个,你已经知道了。第二,是因为它能够利用 n 的非常高效的 8 字节长数据类型。了解这种方法的长用法,也可以解释为什么(不幸的是)目前无法对非常大的数字使用类似的方法。

十进制构造函数中的前两个参数:lomid各使用4个字节。这与 long 的内存量相同。这意味着一旦您长时间达到最大值,就没有 space 可以继续前进了。

要使用类似的方法,您需要一个 12 字节的数据类型来代替 long。这将为您提供使用 hi 参数所需的额外四个字节。

Sax 的方法很聪明,但是在有人写出 12 字节的数据类型之前,您将不得不依赖 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;
}

基准代码:

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());

我的 i7 920 结果:

输入:123.456789

00:00:02.7292447
00:00:00.6043730

输入:999999999999999123.456789

00:00:05.3094786
00:00:01.9702198

输入:1.0

00:00:01.4212123
00:00:00.2378833

输入:0

00:00:01.1083770
00:00:00.1899732

输入:-3.3333333333333333333333333333333

00:00:06.2043707
00:00:02.0373628

如果输入仅包含 0-9,.并且可选 - 在开始时,此自定义函数将字符串解析为十进制的速度明显更快。