【问题标题】:Faster alternative to decimal.Parsedecimal.Parse 的更快替代方案
【发布时间】:2016-06-05 19:24:26
【问题描述】:

我注意到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 中的第三个参数高字节,因此它不适用于更大的数字。

有没有比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 快得多。

【问题讨论】:

  • 还应该考虑负数吗?
  • WillemVanOnsem Van Onsem,在我当前的用例中,我不需要负数。但是,这是一个对任何人都可能有用的一般性问题,因此如果它支持负数会更好。
  • Decimal.Parse() 在过去 20 年里用 C++ 编写并内置到操作系统中,速度达到了它需要的速度。你只能通过偷工减料来加快速度。你没有足够明确你认为什么样的错误是可以接受的。
  • 输入值的统计分布是您想要优化的朋友。例如,如果大多数输入值以 1 字符串的形式出现,那么您可以为这种情况编写更简单/更快的转换代码(您甚至不必处理符号字符)。如果负值不常见,只转换正值的代码会更快;您可能必须测试一次符号,但如果不存在,您不必再次测试它的值。我会查看您的输入值分布,看看您是否不能利用它。
  • 恕我直言,这属于Code Review

标签: c# performance parsing decimal


【解决方案1】:

感谢您的所有 cmets,它们让我有了更多的了解。最后我做了如下。如果输入太长,那么它会分隔输入字符串并使用 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,.并且可选 - 在开始时,此自定义函数将字符串解析为十进制的速度明显更快。

【讨论】:

    【解决方案2】:

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

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

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

    Sax 的方法非常聪明,但在有人编写 12 字节数据类型之前,您将不得不依赖 decimal.Parse。

    【讨论】:

    • 你的说法是错误的。看我的实现。您不需要在长缓冲区中同时拥有所有位。
    猜你喜欢
    • 2018-12-11
    • 2012-07-05
    • 2012-01-23
    • 2013-07-13
    • 2011-02-27
    • 2010-09-22
    • 2017-05-15
    • 1970-01-01
    相关资源
    最近更新 更多