-4

I wanted to get some numbers on how fast is multiplication over addition. I wrote a simple code where I am multiplying 2 numbers, finding the time taken. Then I am adding the 2 numbers and find the time taken. The results are a bit disturbing. Before I show the results, here is the code

package com.np.fun;

import java.util.Scanner;

import org.apache.commons.lang3.time.StopWatch;

public class HowSlowIsMultiplication {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int x = scanner.nextInt();
    int y = scanner.nextInt();
    long z;
    StopWatch stopWatchMultipy = new StopWatch();
    stopWatchMultipy.start();
    z = x*y;
    stopWatchMultipy.stop();
    System.out.println("Time taken for multiplication is : " + stopWatchMultipy.getNanoTime());
    StopWatch stopWatchAdd_1 = new StopWatch();
    stopWatchAdd_1.start();
    for(int i =0 ;i <Math.min(x, y); i++){
        z = z + Math.max(x, y);
    }
    stopWatchAdd_1.stop();
    System.out.println("Time taken for adding in less for loops is : " + stopWatchAdd_1.getNanoTime());
    StopWatch stopWatchAdd_2 = new StopWatch();
    stopWatchAdd_2.start();
    for(int i =0 ;i <Math.max(x, y); i++){
        z = z + Math.min(x, y);
    }
    stopWatchAdd_2.stop();
    System.out.println("Time taken for adding in more for loops is : " + stopWatchAdd_1.getNanoTime());
}

}

I tried this with varying values of x & y. Here is the output for x=10000 and y=5000 (all times are in naoseconds)
Time taken for multiplication is : 61593
Time taken for adding in less for loops is : 1622599
Time taken for adding in more for loops is : 1622599

As you can see, multiplication is several orders of magnitude faster than addition.

Any reasons for this?

Nishit
  • 1,276
  • 2
  • 11
  • 25
  • Given we're talking under 2 milliseconds for each of these, there could be any number of reasons unrelated to your code. – Joe C Sep 28 '17 at 05:28
  • Are you sure your code and analysis is correct? I mean you are adding in loop and seeing the time for additions in entire loop and for multiplication taking time for just one operation? – Optional Sep 28 '17 at 05:29
  • 1) do it with an calculator: do you really expect to do 5000 (or more) additions faster than just one multiplication? And also consider the additional 5000 times calculating `max`, `min`, incementing and comparing the counter. 2) isn't it strange that both loops take the EXACT same time in nanoseconds? that should be an indication that the resolution of the stopwatch is not enough... – user85421 Sep 28 '17 at 05:36
  • No matter what exactly you measure, you measure it completely wrong. It may be wrong not by a few percent, but literally by millions of percent. Both multiplication and addition take much less than the timer resolution and the loop overhead. All broken benchmarks need the same answer, that's why I'm closing this as a duplicate. – maaartinus Oct 08 '17 at 14:59

1 Answers1

0

First off, it's not clear what you're trying to measure, as you appear to be comparing a single multiplication against doing multiple additions in a loop. You shouldn't be surprised that the later is slower.

That being said, microbenchmarks like this are essentially random noise. A multiplication is just one instruction at the machine code level. It's likely to be swamped by any sort of branch or function call you do to measure the timing. In order to get accurate measurements at that level, you have to use special assembly instructions. You also have to account for out of order execution and pipelining, since modern CPUs execute multiple instructions simultaneously, especially in simple cases like integer multiplication.

Add on top of that all the abstractions of the JVM and of Java, and the enterprise is even more hopeless. Java compiles to bytecode, which is in turn interpreted or JIT compiled by the JVM. The JVM has wide latitude to do whatever optimizations it sees fit, so even operations that take different amounts of bytecode instructions may have little or no relation to the ultimate performance.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • 2
    You can make pretty reasonable attempt to measure this with a tool like JMH. Just because things are JIT'ed and optimized doesn't mean it's not measurable or any conceivable result is 'random noise'. – pvg Sep 28 '17 at 05:33