码迷,mamicode.com
首页 > 其他好文 > 详细

一次简单的优化问题

时间:2020-01-19 18:56:02      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:eval   length   equals   answer   ima   直接   数据   额外   start   

先看一段效率极其低下的代码

 startOne = System.currentTimeMillis();

        if (titlelists != null && titlelists.size() > 0) {
            for (int i = 0; i < titlelists.size(); i++) {
                String titleid = titlelists.get(i).getTitleid();
                titleMapper title = titleService.selectByPrimaryKey(titleid);
                String str = "";
                if (title.getTitletype().equals("1")) {
                    str = "单选题";
                }
                if (title.getTitletype().equals("2")) {
                    str = "多选题";
                }
                if (title.getTitletype().equals("3")) {
                    str = "不定项选择题";
                }
                if (title.getTitletype().equals("4")) {
                    str = "问答题";
                }
                resultJson += "{ \"titletype\":\"" + str + "\"," +
                        "\"stem\":\"" + title.getStem() + "\",\"questionbankid\":\"";
                resultJson += titlelists.get(i).getQuestionbankid() + "\",\"titleid\":\"";
                resultJson += titlelists.get(i).getTitleid() + "\"";
                Long one =  System.currentTimeMillis();
                List<optionMapper> option = optionService.selectByTitleId(title.getTitleid());
                System.out.println(i+":1:"+(System.currentTimeMillis()-one));
                one =  System.currentTimeMillis();
                analyseMapper analyse = analyseService.selectByTitleId(title.getTitleid());
                System.out.println(i+":2:"+(System.currentTimeMillis()-one));
                one =  System.currentTimeMillis();
                answerMapper answer = answerService.selectByTitleId(title.getTitleid());
                System.out.println(i+":3:"+(System.currentTimeMillis()-one));
                one =  System.currentTimeMillis();
                resultJson += ",\"options\":\"";
                for (int k = 0; k < option.size(); k++) {
                    if (option.get(k).getOptionnumber() == 1) {
                        resultJson += "A:";
                    }
                    if (option.get(k).getOptionnumber() == 2) {
                        resultJson += "B:";
                    }
                    if (option.get(k).getOptionnumber() == 3) {
                        resultJson += "C:";
                    }
                    if (option.get(k).getOptionnumber() == 4) {
                        resultJson += "D:";
                    }
                    resultJson += option.get(k).getOptionvalue() + "|";
                }
                resultJson = resultJson.substring(0, resultJson.length() - 1);
                System.out.println(i+":4:"+(System.currentTimeMillis()-one));
                one =  System.currentTimeMillis();
                resultJson += "\"";
                resultJson += ",\"answer\":\"";
                resultJson += answer.getAnswervalue();
                resultJson += "\"";
                resultJson += ",\"analyse\":\"";
                resultJson += analyse.getAnalysevalue();
                resultJson += "\",";
                resultJson += "\"";
                resultJson += "titlenumber\":\"";
                resultJson += qbMapper.getTitlenumber() + "\",\"inputanswer\":\"";
                resultJson += titlelists.get(i).getInputAnswer() + "\"}";
                System.out.println(i+":5:"+(System.currentTimeMillis()-one));
            }
        }
        startTwo = System.currentTimeMillis();
        System.out.println("拼接题目花费时间"+(startTwo-startOne));

 这段代码,再titles有80个,执行了大概有18-20秒钟,等待太久了,

  1  主要问题,在于,在for循环里面直接调用额数据库,额外的开销太大的了,应该把数据先一次性提取出来,再进行操作,

  2  还有一个问题,拼接字符串不要用String,而是应该用StringBuilder【非线程安全情况下】,StringBuffer【线程安全情况下】

  目前就是这两个地方可以进行优化,优化后的代码和时间,后续贴上来。

一次简单的优化问题

标签:eval   length   equals   answer   ima   直接   数据   额外   start   

原文地址:https://www.cnblogs.com/Koaler/p/12215074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!