title: Why is if (variable1 % variable2 == 0) inefficient?
body: <p>I am new to java, and was running some code last night, and this really bothered me. I was building a simple program to display every X outputs in a for loop, and I noticed a MASSIVE decrease in performance, when I used modulus as <code>variable % variable</code> vs <code>variable % 5000</code> or whatnot. Can someone explain to me why this is and what is causing it? So I can be better...</p>
  
  <p>Here is the "efficient" code (sorry if I get a bit of syntax wrong I'm not on the computer with the code right now)</p>
  
  <pre><code>long startNum = 0;
  long stopNum = 1000000000L;
  
  for (long i = startNum; i &lt;= stopNum; i++){
      if (i % 50000 == 0) {
          System.out.println(i);
      }
  }
  </code></pre>
  
  <p>Here is the "inefficient code"</p>
  
  <pre><code>long startNum = 0;
  long stopNum = 1000000000L;
  long progressCheck = 50000;
  
  for (long i = startNum; i &lt;= stopNum; i++){
      if (i % progressCheck == 0) {
          System.out.println(i);
      }
  }
  </code></pre>
  
  <p>Mind you I had a date variable to measure the differences, and once it became long enough, the first one took 50ms while the other took 12 seconds or something like that. You may have to increase the <code>stopNum</code> or decrease the <code>progressCheck</code> if your PC is more efficient than mine or what not.</p>
  
  <p>I looked for this question around the web, but I can't find an answer, maybe I'm just not asking it right.</p>
  
  <p>EDIT:
  I did not expect my question to be so popular, I appreciate all the answers. I did perform a benchmark on each half in time taken, and the inefficient code took considerably longer, 1/4 of a second vs 10 seconds give or take. Granted they are using println, but they are both doing the same amount, so I wouldn't imagine that would skew it much, especially since the discrepancy is repeatable. As for the answers, since I am new to Java, I will let votes decide for now which answer is best. I will try to pick one by Wednesday.</p>
  
  <p>EDIT2:
  I am going to make another test tonight, where instead of modulus, it just increments a variable, and when it reaches progressCheck, it will perform one, and then reset that variable to 0. for a 3rd option.</p>
  
  <p>EDIT3.5:</p>
  
  <p>I used this code, and below I'll show my results.. Thank you ALL for the wonderful help! I also tried comparing the short value of the long to 0, so all my new checks happen ever "65536" times making it equal in repeats.</p>
  
  <pre><code>public class Main {
  
  
      public static void main(String[] args) {
  
          long startNum = 0;
          long stopNum = 1000000000L;
          long progressCheck = 65536;
          final long finalProgressCheck = 50000;
          long date;
  
          // using a fixed value
          date = System.currentTimeMillis();
          for (long i = startNum; i &lt;= stopNum; i++) {
              if (i % 65536 == 0) {
                  System.out.println(i);
              }
          }
          long final1 = System.currentTimeMillis() - date;
          date = System.currentTimeMillis();
          //using a variable
          for (long i = startNum; i &lt;= stopNum; i++) {
              if (i % progressCheck == 0) {
                  System.out.println(i);
              }
          }
          long final2 = System.currentTimeMillis() - date;
          date = System.currentTimeMillis();
  
          // using a final declared variable
          for (long i = startNum; i &lt;= stopNum; i++) {
              if (i % finalProgressCheck == 0) {
                  System.out.println(i);
              }
          }
          long final3 = System.currentTimeMillis() - date;
          date = System.currentTimeMillis();
          // using increments to determine progressCheck
          int increment = 0;
          for (long i = startNum; i &lt;= stopNum; i++) {
              if (increment == 65536) {
                  System.out.println(i);
                  increment = 0;
              }
              increment++;
  
          }
  
          //using a short conversion
          long final4 = System.currentTimeMillis() - date;
          date = System.currentTimeMillis();
          for (long i = startNum; i &lt;= stopNum; i++) {
              if ((short)i == 0) {
                  System.out.println(i);
              }
          }
          long final5 = System.currentTimeMillis() - date;
  
                  System.out.println(
                  "\nfixed = " + final1 + " ms " + "\nvariable = " + final2 + " ms " + "\nfinal variable = " + final3 + " ms " + "\nincrement = " + final4 + " ms" + "\nShort Conversion = " + final5 + " ms");
      }
  }
  </code></pre>
  
  <p><strong>Results:</strong></p>
  
  <ul>
  <li>fixed = 874 ms (normally around 1000ms, but faster due to it being a power of 2)</li>
  <li>variable = 8590 ms</li>
  <li>final variable = 1944 ms (Was ~1000ms when using 50000)</li>
  <li>increment = 1904 ms</li>
  <li>Short Conversion = 679 ms</li>
  </ul>
  
  <p>Not surprising enough, due to a lack of division, the Short Conversion was 23% faster than the "fast" way. This is interesting to note. If you need to show or compare something every 256 times (or about there) you can do this, and use </p>
  
  <pre><code>if ((byte)integer == 0) {'Perform progress check code here'}
  </code></pre>
  
  <p>ONE FINAL INTERESTING NOTE, using modulus on the "Final declared Variable" with 65536 (not a pretty number) was half the speed of (slower) than the fixed value. Where before it was benchmarking near the same speed.</p>