ExecutorService's surprising performance break-even point — rules of thumb?

后端 未结 9 1987
臣服心动
臣服心动 2020-12-08 03:10

I\'m trying to figure out how to correctly use Java\'s Executors. I realize submitting tasks to an ExecutorService has its own overhead. However, I\'m surpris

9条回答
  •  太阳男子
    2020-12-08 03:48

    Here are results on my machine (OpenJDK 8 on 64-bit Ubuntu 14.0, Thinkpad W530)

    simpleCompuation:6
    computationWithObjCreation:5
    computationWithObjCreationAndExecutors:33
    

    There's certainly overhead. But remember what these numbers are: milliseconds for 100k iterations. In your case, the overhead was about 4 microseconds per iteration. For me, the overhead was about a quarter of a microsecond.

    The overhead is synchronization, internal data structures, and possibly a lack of JIT optimization due to complex code paths (certainly more complex than your for loop).

    The tasks that you'd actually want to parallelize would be worth it, despite the quarter microsecond overhead.


    FYI, this would be a very bad computation to parallelize. I upped the thread to 8 (the number of cores):

    simpleCompuation:5
    computationWithObjCreation:6
    computationWithObjCreationAndExecutors:38
    

    It didn't make it any faster. This is because Math.random() is synchronized.

提交回复
热议问题