首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ExecutorService令人惊讶的性能盈亏平衡点--经验法则?

ExecutorService令人惊讶的性能盈亏平衡点--经验法则?
EN

Stack Overflow用户
提问于 2009-10-30 04:17:15
回答 11查看 23.4K关注 0票数 23

我正在努力弄清楚如何正确地使用Java的执行器。我意识到向ExecutorService提交任务有它自己的开销。然而,我惊讶地看到它是如此之高。

我的程序需要以尽可能低的延迟处理大量数据(股票市场数据)。大多数计算都是相当简单的算术操作。

我试着测试一些非常简单的东西:"Math.random() * Math.random()

最简单的测试是在一个简单的循环中运行这个计算。第二个测试在匿名Runnable中执行相同的计算(这应该是用来度量创建新对象的成本)。第三个测试将Runnable传递给ExecutorService (这是用于度量引入执行器的成本)。

我在我的小型笔记本电脑(2个cpus,1.5g内存)上运行了这些测试:

代码语言:javascript
复制
(in milliseconds)
simpleCompuation:47
computationWithObjCreation:62
computationWithObjCreationAndExecutors:422

(大约四分中有一次,前两个数最后相等)

注意,执行程序比在单个线程上执行花费的时间要长得多。对于1到8之间的线程池大小,这些数字大致相同。

问:我是不是遗漏了一些显而易见的东西,或者这些结果是预期的?这些结果告诉我,我传递给执行者的任何任务都必须做一些非平凡的计算。如果我正在处理数百万条消息,并且需要对每条消息执行非常简单(廉价)的转换,那么我可能仍然无法使用executors...trying将计算分散到多个CPU上,结果可能比在一个线程中执行它们要花费更多。设计决策比我原先想象的要复杂得多。有什么想法吗?

代码语言:javascript
复制
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExecServicePerformance {

 private static int count = 100000;

 public static void main(String[] args) throws InterruptedException {

  //warmup
  simpleCompuation();
  computationWithObjCreation();
  computationWithObjCreationAndExecutors();

  long start = System.currentTimeMillis();
  simpleCompuation();
  long stop = System.currentTimeMillis();
  System.out.println("simpleCompuation:"+(stop-start));

  start = System.currentTimeMillis();
  computationWithObjCreation();
  stop = System.currentTimeMillis();
  System.out.println("computationWithObjCreation:"+(stop-start));

  start = System.currentTimeMillis();
  computationWithObjCreationAndExecutors();
  stop = System.currentTimeMillis();
  System.out.println("computationWithObjCreationAndExecutors:"+(stop-start));


 }

 private static void computationWithObjCreation() {
  for(int i=0;i<count;i++){
   new Runnable(){

    @Override
    public void run() {
     double x = Math.random()*Math.random();
    }

   }.run();
  }

 }

 private static void simpleCompuation() {
  for(int i=0;i<count;i++){
   double x = Math.random()*Math.random();
  }

 }

 private static void computationWithObjCreationAndExecutors()
   throws InterruptedException {

  ExecutorService es = Executors.newFixedThreadPool(1);
  for(int i=0;i<count;i++){
   es.submit(new Runnable() {
    @Override
    public void run() {
     double x = Math.random()*Math.random();     
    }
   });
  }
  es.shutdown();
  es.awaitTermination(10, TimeUnit.SECONDS);
 }
}
EN

Stack Overflow用户

发布于 2022-01-22 19:49:17

如果它对其他人有用的话,下面是一个现实场景的测试结果--在三星安卓设备上反复使用ExecutorService直到所有任务结束。

代码语言:javascript
复制
 Simple computation (MS): 102
 Use threads (MS): 31049
 Use ExecutorService (MS): 257

代码:

代码语言:javascript
复制
   ExecutorService executorService = Executors.newFixedThreadPool(1);
        int count = 100000;

        //Simple computation
        Instant instant = Instant.now();
        for (int i = 0; i < count; i++) {
            double x = Math.random() * Math.random();
        }
        Duration duration = Duration.between(instant, Instant.now());
        Log.d("ExecutorPerformanceTest", "Simple computation (MS): " + duration.toMillis());


        //Use threads
        instant = Instant.now();
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                double x = Math.random() * Math.random();
            }
            ).start();
        }
        duration = Duration.between(instant, Instant.now());
        Log.d("ExecutorPerformanceTest", "Use threads (MS): " + duration.toMillis());


        //Use ExecutorService
        instant = Instant.now();
        for (int i = 0; i < count; i++) {
            executorService.execute(() -> {
                        double x = Math.random() * Math.random();
                    }
            );
        }
        duration = Duration.between(instant, Instant.now());
        Log.d("ExecutorPerformanceTest", "Use ExecutorService (MS): " + duration.toMillis());
票数 0
EN
查看全部 11 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1647990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档