-1

I want to check which code is faster and why?

Or if we want to execute more faster than single core then How can we write the code ?

Concept : In multi threading, we used available cores to execute operation in parallel way.

But when I ran following codes , I came to know single core code is faster than both multi-threaded codes. Then what is the use of multi-threading in these kind of scenario?

Output : In both programs output you will get minutes level difference.

CODE : Declaration :

     List<Integer> list = new ArrayList<>();
         for (int i = 0; i < 10000000; i++) {
             list.add(i);
         }

Single core : Code 1 :

    while (i < 5) {
        list.forEach(e -> {
            System.out.println(LocalTime.now() + "- " + Thread.currentThread().getName() + " Value : " + e);
        });
        i++;
    }

Multi-thread : Code 2 :

 ExecutorService service = Executors.newFixedThreadPool(2);
    while (i < 5) {
        list.forEach(e -> service.execute(() -> System.out
                .println(LocalTime.now() + "- " + Thread.currentThread().getName() + " Value : " + e)));
        i++;
    }

Multi-thread :Code 3 :

ExecutorService service = Executors.newFixedThreadPool(2);
     while (i < 5) {
            service.execute(() -> list.forEach(e -> System.out
                    .println(LocalTime.now() + "- " + Thread.currentThread().getName() + " Value : " + e)));
            i++;
        }
vijayk
  • 2,633
  • 14
  • 38
  • 59
  • Do you really think you can get measurable differences or meaningful results when talking about stuff that runs in less than 100 milliseconds? – Federico klez Culloca Aug 18 '20 at 10:48
  • Take a look [here](https://stackoverflow.com/questions/2842695/what-is-microbenchmarking) on how to do this kind of things in a more sensible way. – Federico klez Culloca Aug 18 '20 at 10:49
  • 3
    multi-threaded code makes sense when a thread has something to do and when thread execution takes relatively long time. Otherwise you just waste system time on creating switching between threads. – Serge Aug 18 '20 at 10:49
  • @vijayk then please ask about your actual tests, not about a meaningless one. – Federico klez Culloca Aug 18 '20 at 10:50
  • @FedericoklezCulloca.. You can just try to print from 1- 100lakh then you will get more difference. – vijayk Aug 18 '20 at 10:51
  • this code cant test it its good or not. add delay so delay will be simulate like there is a hard process in your code then look the results. You will see multithreading is much faster. – egemenakturk Aug 18 '20 at 10:56
  • 1. Try to approach this from the *Asymptotic* point of view, for the large enough inputs; 2. If you run the same code in another thread, well, you at least allocate another Stack space, which will independently compete for the CPU, will have its clocking and time-space resource. – Giorgi Tsiklauri Aug 18 '20 at 10:57

1 Answers1

0
  1. Testing performance is quite complicated, just printing start and end times stopped working well around the time CPUs grew pipelining functionality and the VM gained hotspot abilities. In other words, this 'just stopwatch it' principle ceased to be a good idea 20 years ago. Use JMH.

  2. There's hidden synchronization everywhere. For example, System.out is incredibly slow compared to the rest of the tasks you are doing, AND is probably doing some internal synchronizing. You can't really make the printing of results part of the timing, even if you do use JMH, it throws off the measurement.

  3. multithreaded adds overhead. Because 'the job' is just querying LocalTime.now(), which is quite fast, the overhead is drowning out the gains.

Go calculate, I dunno, a bunch of hashes singlecore and then in a multicore arrangement, and you'll notice you'll get close to 100% efficiency (100% = theoretical maximum; if the job takes 10 seconds on 1 core, and you have 100% efficient parallelization, then it takes 10/X seconds on X cores).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72