Fastest way to separate the digits of an int into an array in .NET?

后端 未结 11 786
天涯浪人
天涯浪人 2021-01-31 20:07

I want to separate the digits of an integer, say 12345, into an array of bytes {1,2,3,4,5}, but I want the most performance effective way to do that, because my program does tha

11条回答
  •  再見小時候
    2021-01-31 20:51

    A little loop unrolling perhaps?

    int num = 147483647;
    int nDigits = 1 + Convert.ToInt32(Math.Floor(Math.Log10(num)));
    byte[] array = new byte[10] {
                (byte)(num / 1000000000 % 10),
                (byte)(num / 100000000 % 10),
                (byte)(num / 10000000 % 10),
                (byte)(num / 1000000 % 10),
                (byte)(num / 100000 % 10),
                (byte)(num / 10000 % 10),
                (byte)(num / 1000 % 10),
                (byte)(num / 100 % 10),
                (byte)(num / 10 % 10),
                (byte)(num % 10)};
    byte[] digits;// = new byte[nDigits];
    digits = array.Skip(array.Length-nDigits).ToArray();
    

    Thanks above for the Log10 thingy.. ;)

    There's been some talk of benchmarking...

    I've fully unrolled the loops, and compared with the accepted memoized variant of Jons, and I get a consistently quicker time with this:-

        static int[] ConvertToArrayOfDigits_unrolled(int num)
        {
            if (num < 10)
            {
                return new int[1] 
                {
                    (num % 10) 
                };
            }
            else if (num < 100)
            {
                return new int[2] 
                {
                    (num / 10 % 10),
                    (num % 10)
                };
            }
            else if (num < 1000)
            {
                return new int[3] {
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else if (num < 10000)
            {
                return new int[4] {
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else if (num < 100000)
            {
                return new int[5] {
                (num / 10000 % 10),
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else if (num < 1000000)
            {
                return new int[6] {
                (num / 100000 % 10),
                (num / 10000 % 10),
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else if (num < 10000000)
            {
                return new int[7] {
                (num / 1000000 % 10),
                (num / 100000 % 10),
                (num / 10000 % 10),
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else if (num < 100000000)
            {
                return new int[8] {
                (num / 10000000 % 10),
                (num / 1000000 % 10),
                (num / 100000 % 10),
                (num / 10000 % 10),
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else if (num < 1000000000)
            {
                return new int[9] {
                (num / 100000000 % 10),
                (num / 10000000 % 10),
                (num / 1000000 % 10),
                (num / 100000 % 10),
                (num / 10000 % 10),
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
            else
            {
                return new int[10] {
                (num / 1000000000 % 10),
                (num / 100000000 % 10),
                (num / 10000000 % 10),
                (num / 1000000 % 10),
                (num / 100000 % 10),
                (num / 10000 % 10),
                (num / 1000 % 10),
                (num / 100 % 10),
                (num / 10 % 10),
                (num % 10)};
            }
        }
    

    It may be I've messed up somewhere - I don't have much time for fun and games, but I was timing this as 20% quicker.

提交回复
热议问题