0

What is the difference between minus and mod operators in terms of time required for computation? I have noticed that mod takes significantly lesser time than minus. For example -

Problem statement - I'm incrementing a variable 'x' by 1, 'x' should not be greater than 'y'. This can be done in 2 ways.

1)

if(x > y) x = x - y;

or

2)

if(x > y) x = x % y;

The 2nd approach is faster than 1st. Can anyone please explain?

Here's the code - init, y and hop are inputs, hop is always less than y.

        int x = init;
        int count = 1;
        do{
            x += hop;
            count++;
            if (x > y)
                x = x - y;
        } while(x != y);

P.S. The code is just an example. I'm not concerned about what it does. I just want to know how using % instead of - reduces the time.

  • 8
    Would you care to explain first, what makes you believe that the 2nd approach is faster than the 1st ? – Mike Nakis May 29 '17 at 17:52
  • 4
    It's not a matter of which operation is faster - it's a matter of what they *do*. – user2357112 May 29 '17 at 17:55
  • I appeared for a coding challenge and the second piece of code brought significant difference in the time taken to execute the program. It was almost reduced by half. – CuriousKitty May 29 '17 at 17:57
  • 4
    Please post the code you used to determine the time. I suspect you're not computing it correctly, or that there were other differences in your algorithm that meant the behavior was not equivalent. There is no way that `%` should be inherently faster than `-` (and in older computers, `%` would have been significantly slower because it involves division, which was slower than subtraction; on modern computers there might not be a difference). – ajb May 29 '17 at 17:57
  • Your *problem statement* is not reflected in your conditionals. `if (x < y) x += 1` better reflects your *problem statement*. – Jonny Henly May 29 '17 at 17:59
  • 4
    Your two "approaches" do entirely separate things. The first way doesn't guarantee that `x <= y` unless you're calling it in a loop, which would incidentally explain the performance hit. Btw, the `if` is unnecessary in the second way; you'll get the same result without it. – shmosel May 29 '17 at 18:02
  • I found out a stack overflow link: https://stackoverflow.com/questions/27977834/why-is-modulus-operator-slow which says minus is faster than mod operator and it has a nice explanation. So do you mean why minus is faster than mod? – Ananth May 29 '17 at 18:02
  • @ajb I did not compute the time, I had to execute my code in an editor and the program would be tested against sample test cases and the time taken would be displayed. I know - should have taken lesser time than %, but it didn't. Hence the question. – CuriousKitty May 29 '17 at 18:02
  • 2
    @shmosel We're not given all the information. But this looks like a common idiom where we start with `x` = 1, and then in a loop we increment `x` and then apply one of the above statements. In this case, assuming `y` does not change and `y` > 1, the two _would_ accomplish the same thing since it could never be the case that `x >= 2 * y`. But I'm making a lot of assumptions, which is why I think we need to see more of the code. Most likely, the OP tried to implement something that looks like this idiom, but did something wrong. At least that's my guess. – ajb May 29 '17 at 18:07
  • 1
    @ajb Oops, I missed the incrementing by 1 part. – shmosel May 29 '17 at 18:09
  • 1
    Okay, instead of providing more information the OP is commenting, and 20 minutes should be enough already, so... this should be closed. – Mike Nakis May 29 '17 at 18:14
  • Please let me know if any more info is needed. I just want to know how % works faster than - – CuriousKitty May 29 '17 at 18:27
  • you are not incrementing by 1, you are incrementing by `hop`. If `hop` is greater than `y` your "solution" with `x = x-y;` is not sufficient to keep x less than y – Thomas Kläger May 29 '17 at 18:50
  • hop will be less than y. edited the question. – CuriousKitty May 29 '17 at 18:55

0 Answers0