Faster code for leetcode reverse int This is my solution for the leetcode reverse int question:


  Given a 32-bit signed integer, reverse digits of an integer.
  
  Example 1:
  
  Input: 123 Output: 321 Example 2:
  
  Input: -123 Output: -321 Example 3:
  
  Input: 120 Output: 21 Note: Assume we are dealing with an environment
  which could only store integers within the 32-bit signed integer
  range: [−2^31,  2^31 − 1]. For the purpose of this problem, assume that
  your function returns 0 when the reversed integer overflows.


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions
{
    /// &lt;summary&gt;
    /// Given a 32-bit signed integer, reverse digits of an integer.
    ///
    /// Example 1:
    ///
    /// Input: 123
    /// Output: 321
    /// Example 2:
    ///
    /// Input: -123
    /// Output: -321
    /// Example 3:
    ///
    /// Input: 120
    /// Output: 21
    /// Note:
    /// Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
    /// &lt;/summary&gt;
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            int temp = 1534236469;
            int result = Reverse(temp);
            Assert.AreEqual(0, result);
        }

        [TestMethod]
        public void TestMethod2()
        {
            int temp = 123;
            int result = Reverse(temp);
            Assert.AreEqual(321, result);
        }
        public int Reverse(int x)
        {
            int result = 0;
            bool negative = false;
            if (x &lt; 0)
            {
                negative = true;
                x = -x;
            }

            int prevRevNum = 0;
            while (x != 0)
            {
                int currentDigit = x % 10;
                result = (result * 10) + currentDigit;

                if ((result - currentDigit) / 10 != prevRevNum)
                {
                    return 0;
                }
                prevRevNum = result;
                x = x / 10;
            }

            if (negative)
            {
                return -result;
            }

            return result;
        }
    }
}


This is the output:


  Runtime: 52 ms, faster than 62.65% of C# online submissions for Reverse Integer.


Can you please comment on how can I make this code run faster? I guess the modulo and division are the key factors of speed here.
Using a string instead of division / modulo seems to be a few percent faster than the solution from the question:

    public static int Reverse(int x)
    {
        var str = x.ToString();
        int res = 0;
        int multiplier = 1;
        var negative = str[0] == '-';
        for (int i = negative ? 1 : 0; i &lt; str.Length; i++)
        {
            var num = str[i] - 48;
            if (num &gt; 0 || multiplier &gt; 1)
            {
                res += num * multiplier;
                multiplier *= 10;
            }
        }
        return negative ? -res : res;
    }


However, improving the solutions from the question using System.Math.DivRem (as mistertribs suggested in a comment) will be even faster :):

    public static int Reverse(int x)
    {
        var result = 0;
        var negative = x &lt; 0;
        if (negative) x = -x;

        while (x != 0)
        {
            x = Math.DivRem(x, 10, out var rest);
            result = result * 10 + rest;
        }

        return negative ? -result : result;
    }