0

I am making a java program which compares how much time it takes to calculate the total of prime numbers there are between two given numbers. I have to calculate how much it takes sequentially and also with threads.

For example, I have to check how much it takes to calculate sequentially:

PrimeNumbers pn = new PrimeNumbers (1,1000); 

And compare it with how much it takes to calculate the same thing but dividing it in several threads:

PrimeThread p1 = new PrimeThread (1, 200); PrimeThread p2 = new PrimeThread (201, 400); PrimeThread p3 = new PrimeThread (401, 600); PrimeThread p4 = new PrimeThread (601, 800); PrimeThread p5 = new PrimeThread (801, 1000); 

I have to use System.currentTimeMillis() to calculate the time. Threads should be able to calculate the same thing faster, but it is actually slower. So far I have this code:

PrimeNumbers pn = new PrimeNumbers(1,1000); long startTimeSeq = System.currentTimeMillis(); int totalNumbersSeq = pn.calculatePrimeNumbers(); long finishTimeSeq = System.currentTimeMillis(); float totalTimeSeq = finishTimeSeq - startTimeSeq; PrimeThread p1 = new PrimeThread (1, 200); PrimeThread p2 = new PrimeThread (201, 400); PrimeThread p3 = new PrimeThread (401, 600); PrimeThread p4 = new PrimeThread (601, 800); PrimeThread p5 = new PrimeThread (801, 1000); long startTimeThread = System.currentTimeMillis(); p1.start(); p2.start(); p3.start(); p4.start(); p5.start(); try { p1.join(); p2.join(); p3.join(); p4.join();p5.join(); } catch (InterruptedException e) { e.printStackTrace(); } long finishTimeThread = System.currentTimeMillis(); float totalTimeThread = finishTimeThread - startTimeThread; int totalNumbersThread = p1.getNumPrimers() + p2.getNumPrimers() + p3.getNumPrimers() + p4.getNumPrimers() + p5.getNumPrimers(); System.out.println("Total prime numbers sequentially: " + numPrimers); System.out.println("Total time: " + totalTimeSeq); System.out.println("Total prime numbers with threads: " + resultatFinal); System.out.println("Total time: " + totalTimeThread); 

The outcome of the prints is:

Total prime numbers sequentially: 169 Total time: 1.0 Total prime numbers with threads: 169 Total time: 3.0 

I'm sorry if I this is messy, it is my first time posting here and I'm new to programing. Thank you so much.

3
  • It's 1ms vs 3ms. It's not a large enough sample size to draw any meaningful conclusions. Also, starting threads, context switching, etc. are not free. Commented Oct 9, 2019 at 19:05
  • You're capturing some overhead of threading - perhaps if you try with a larger range, you might start to see multithreading overtake single threading in terms of performance. If instead of measuring the time in the main thread, you could measure how much time each thread spends computing, and add those up individually. This should be similar to the number your single thread gives. Commented Oct 9, 2019 at 19:05
  • Your measurement is flawed. You are primarily measuring side-effects that have nothing to do with your actual code, like CPU caching, garbage collection, JVM warmup time and many others. Use a microbenchmarking framework like jmh if you want a measurement that is not worthless. Commented Oct 9, 2019 at 19:06

1 Answer 1

4
  1. Don't benchmark like that, use JMH. Otherwise, the numbers you'll get will be almost meaningless due to JIT compilation and the 1001 optimizations performed by the JVM. Not to mention that the difference between 1ms and 3ms in a single run is certainly not enough to make any meaningful conclusion. JMH runs the same code many times so that the JVM will reach a steady state, and then performs many more runs that actually measure the code's performance.

  2. Threads aren't a magic powder you can sprinkle over a piece of code to make it faster. Threads have significant costs associated with them - thread creation and context switching are expensive operations. From the looks of it, the computation you're performing is pretty quick. So the cost of thread creation and context switching actually eclipse the run time of the algorithm itself.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.