首页 文章

更快的decimal.Parse替代品

提问于
浏览
10

我注意到 decimal.Parse(number, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture) 比基于Jeffrey Sax的代码的自定义十进制解析方法慢大约100%Faster alternative to 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));
}

我认为这是因为原生 decimal.Parse 旨在与数字风格和文化信息斗争 .

但是,上述方法在 new decimal 中不使用第3个参数hi字节,因此它不适用于较大的数字 .

是否有一个更快的替代 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 .

2 回答

  • 5

    感谢您的所有评论,这让我有了更多的见解 . 最后我做了如下 . 如果输入太长,则它将输入字符串分开,并使用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 ,.并且可选地 - 在开始时,这个自定义函数明显更快地将字符串解析为十进制 .

  • 1

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

    前两个参数:十进制构造函数中的 lomid 各使用4个字节 . 这是和long一样的内存量 . 这意味着一旦你达到一个很长的最大值,就没有空间继续前进了 .

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

    Sax的方法非常聪明,但在有人写一个12字节的数据类型之前,你只需要依赖decimal.Parse .

相关问题