不稳定态maj 2019-05-30 21:07 采纳率: 0%
浏览 2606

求问java处理大量数据的时候处理速度越来越慢怎么办?

代码大概是这样的 就是构造一个轨道系统,其中createAthlete方法就是new一个对象,然后addo就是add到一个arraylist里
在读1000条数据以内时候就几毫秒构造一条,在读到七八千条数据的时候就特别特别慢了。想知道要怎么加快后面的处理速度呢?(一共要处理50w条数据)

(这是几千条后打印的时间:

构造Athlete ::= 时间:777ms

构造Athlete ::= 时间:728ms

构造Athlete ::= 时间:444ms

构造Athlete ::= 时间:764ms

构造Athlete ::= 时间:703ms

构造Athlete ::= 时间:730ms

构造Athlete ::= 时间:426ms

构造Athlete ::= 时间:797ms

构造Athlete ::= 时间:951ms

构造Athlete ::= 时间:266ms

构造Athlete ::= 时间:1853ms

构造Athlete ::= 时间:715ms

 for(int k=0;k<arrayList.size();k++){
      long startTime1 = System.currentTimeMillis(); 
      line=arrayList.get(k);
      String pattern = "(\\w+)(\\W+)(.*)";
      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(line);
      if(m.find()) {
          if(m.group(1).equals("Game")) {
              cir.addCenter(m.group(3));
          }
          else if(m.group(1).equals("Athlete")) {
              String line1=m.group(3);
              String pattern1 = "(\\w+),(\\d+),(\\w+),(\\d+),(\\d+.\\d+)>";
              Pattern r1 = Pattern.compile(pattern1);
              Matcher m1 = r1.matcher(line1);
              if(m1.find()) {
                  Athlete athlete=physicalObject.createAthlete(m1.group(1),Integer.parseInt(m1.group(2)),m1.group(3),Integer.parseInt(m1.group(4)),Double.parseDouble(m1.group(5)));
                 cir.addo(athlete);
              }
          }
          else {
              int a=Integer.parseInt(m.group(3).trim());
             for(int i=0;i<a;i++) {
                 cir.addTrack();
             }
         }
      }
      long endTime1 = System.currentTimeMillis();
      System.out.println("构造"+line+"时间:" + (endTime1 - startTime1) + "ms"); 
    }

  }
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-05-30 23:55
    关注

    大量小的对象的创建会导致性能非常低,这是因为内存碎片造成的。
    解决这个问题有几个办法,具体要根据你的场景。
    首先,可以考虑使用享元模式。
    其次,如果大量的对象的所有字段都是一样的,这时如果可以封闭掉所有对对象的修改(set函数),这个时候可以通过创建不变对象(只允许构造函数一次性赋值,不允许更改)的方式,让所有相同的对象共享同一个实例。减少对象创建的开销。

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?