Degrading performance when increasing number of cores

前端 未结 3 1030
别那么骄傲
别那么骄傲 2021-02-07 20:36

My mac is armed with 16 cores.

System.out.println(Runtime.getRuntime().availableProcessors());  //16

I\'m running the code below to see the eff

3条回答
  •  不知归路
    2021-02-07 20:38

    My hunch is that you may have put so much burden on the disk I/O that you slowed everything down! See the I/O performance in "Activity Monitor" (if you are on OSX). On Linux, use vmstat command to get an idea of what is going on. [If you see lots of swapping or high rate of reads/s and writes/s then there you go]


    Few things I noticed:

    CountFileLineThread is not in the code. Please put it so we can see exactly what's going on.

    Next,

    for (Future future : futures)
    {
        Integer result = future.get();
        total+=result;
        System.out.println("result :"+result);
    
    }
    

    Here, note that you are blocked on on the result of the first Task (future.get()). Meanwhile the other results may have already been available but you can't see them until the first completes. Use CompletionService instead to get the results in the order they finish for better measurement. It doesn't matter though since you want all Threads to be done before ending the timer though.

    Another point: Blocking I/O is the key. It doesn't matter, per se, how many cores you have if the tasks are blocked on I/O, Network, etc. Modern Processors have what's what Hyper Threading and they can run a thread waiting to be run if currently executing thread blocks.

    So for example, if I have 16 cores and I spawn 16 Threads asking them to read 1 GB files, I will not get any performance improvements just by having more cores. The bottleneck is the disk and memory.

提交回复
热议问题