写点程序就要认真点,为什么呢?会有效率问题的!引例:
今天电力公司的满意度测评系统开始正式的运行测评了,然而有一件很溴的事情让人难看,前面填写一份问卷不到10秒钟的时间,现在呢?有多少时间呢?2分钟,两天以后5分钟,三天以后10分钟,...N天以后40分钟,还伴随着有什么情况呢?
tomcat6.0 cup 100% 内存:512
这真是让人想都不敢想的事情,居然发生呢?居然做了一下的假设:
1。本来以为是Tomcat运行时间很长了,不稳定的原因;
2。本系统装载小型机的虚拟机上,并且虚拟机有异常,怀疑是不是系统的问题;
3。使用的连接池+mysql,是不是mysql连接数堵塞的原因。
。。。。。。。。。。
种种的假设都有了,就是没有检查源代码。
很致命的源代码见如下:
public String add() { String result = "tip"; if (StringUtils.isEmpty(indexItemids)) { setOperationMessage("你交了一张白卷,请重新填写问卷!<br><a href='/paper/paperItem!queryAll.action'>重新填写</a>"); return "tip"; } survey = surveyManager.querySurvey(survey.getSurveyID()); customerType = customerTypeManager.queryCustomerType(customerType.getCustomerTypeID()); surveyArea = surveyAreaManager.querySurveyArea(surveyArea.getSurveyAreaID()); String[] itemids = indexItemids.split(":"); String[] scores = itemScores.split(":"); user = randomGenerateMethod(); UserInfo uInfo = user.getUserInfo(); // 添加调查结果 SurveyResult surveyResult = surveyResultSaveOrUpdate(user); // 添加调查结果项 surveyResultItemSaveOrUpdate(surveyResult, itemids, scores); // 添加调查结果致地区客户类别调查结果 customerTypeResultSaveOrUpdate(surveyResult, itemids, scores, uInfo); // 添加建议项 List<Suggest> haveSuggests = new ArrayList<Suggest>(); if (null != suggestids) { for (int id : suggestids) { Suggest suggest = suggestManager.querySuggest(id); haveSuggests.add(suggest); } } user.getUserInfo().setSuggests(haveSuggests); user.setAccountlocked(true); user.getUserInfo().setCustomerName(userInfo.getCustomerName()); user.getUserInfo().setCustomerUnit(userInfo.getCustomerUnit()); user.getUserInfo().setAddress(userInfo.getAddress()); user.getUserInfo().setPhone(userInfo.getPhone()); user.getUserInfo().setCustomerAdvice(userInfo.getCustomerAdvice()); user.getUserInfo().setTradeClass(userInfo.getTradeClass()); user.getUserInfo().setAge(userInfo.getAge()); user.getUserInfo().setSex(userInfo.getSex()); user.getUserInfo().setOccupationClass(userInfo.getOccupationClass()); userManager.modifyUser(user); setOperationMessage("您的问卷信息已经成功提交,感谢您的参与,再见!"); return result; } private SurveyResult surveyResultSaveOrUpdate(User user) { SurveyResult surveyResult = surveyResultManager.querySurveyResult(user.getUserInfo().getSurvey(), user); if (surveyResult == null) { surveyResult = new SurveyResult(); surveyResult.setSurvey(user.getUserInfo().getSurvey()); surveyResult.setTime(new Date()); surveyResult.setUser(user); surveyResultManager.addSurveyResult(surveyResult); } else { surveyResult.setSurvey(user.getUserInfo().getSurvey()); surveyResult.setTime(new Date()); surveyResult.setUser(user); surveyResultManager.modifySurveyResult(surveyResult); } return surveyResult; } private void surveyResultItemSaveOrUpdate(SurveyResult surveyResult, String[] itemids, String[] scores) { for (int i = 0; i < itemids.length; i++) { SurveyResultItem surveyResultItem = null; int indexItemID = Integer.parseInt(itemids[i]); double itemScore = Double.parseDouble(scores[i]); indexItem = indexItemManager.queryIndexItem(indexItemID); if (null == surveyResultItemManager.querySurveyResultItem(indexItem, surveyResult)) { surveyResultItem = new SurveyResultItem(); surveyResultItem.setIndexItem(indexItem); surveyResultItem.setSurveyResult(surveyResult); surveyResultItem.setItemScore(itemScore); surveyResultItemManager.addSurveyResultItem(surveyResultItem); } else { surveyResultItem = surveyResultItemManager.querySurveyResultItem(indexItem, surveyResult); surveyResultItem.setIndexItem(indexItem); surveyResultItem.setSurveyResult(surveyResult); surveyResultItem.setItemScore(itemScore); surveyResultItemManager.modifySurveyResultItem(surveyResultItem); } } } private void customerTypeResultSaveOrUpdate(SurveyResult surveyResult, String[] itemids, String[] scores, UserInfo uInfo) { for (int i = 0; i < itemids.length; i++) { int indexItemID = Integer.parseInt(itemids[i]); indexItem = indexItemManager.queryIndexItem(indexItemID); double itemScore = Double.parseDouble(scores[i]); customerTypeResult = customerTypeResultDao.getCustomerTypeResultByIndexItemAndCustomerType(indexItem, uInfo.getSurveyArea(), uInfo.getSurvey(), uInfo.getCustomerType()); if (customerTypeResult == null) { customerTypeResult = new CustomerTypeResult(); customerTypeResult.setIndexItem(indexItem); customerTypeResult.setItemScore(itemScore); customerTypeResult.setSurvey(uInfo.getSurvey()); customerTypeResult.setSurveyArea(uInfo.getSurveyArea()); customerTypeResult.setCustomerType(uInfo.getCustomerType()); customerTypeResultDao.addCustomerTypeResult(customerTypeResult); } else { customerTypeResult.setIndexItem(indexItem); customerTypeResult.setItemScore(itemScore); customerTypeResult.setSurvey(uInfo.getSurvey()); customerTypeResult.setSurveyArea(uInfo.getSurveyArea()); customerTypeResult.setCustomerType(uInfo.getCustomerType()); customerTypeResultDao.modifyCustomerTypeResult(customerTypeResult); } } }
看见这点代码本来角色没有什么,因为都是固定的,怎么会有这样大的效率差别呢? 再来看看一下的代码:
@Override public SurveyResult querySurveyResult(Survey survey, User user) { SurveyResult result = null; for (SurveyResult surveyResult : queryAllSurveyResult().getResultlist()) { if (user.getUserID() == surveyResult.getUser().getUserID()) { if (survey.getSurveyID() == surveyResult.getSurvey().getSurveyID()) { result = surveyResultDao.querySurveyResult(surveyResult.getSurveyResultID()); } } } return result; }
这个代码会不会哟效率的问题呢? 再来看看一下的代码:
@Override public SurveyResult querySurveyResult(Survey survey, User user) { SurveyResult result = null; StringBuffer wherejpql = new StringBuffer(" o.survey.surveyID=?1 and o.user.userID=?2 "); Integer[] params = new Integer[] { survey.getSurveyID(), user.getUserID() }; List<SurveyResult> temps = surveyResultDao.queryAllSurveyResult(-1, -1, wherejpql.toString(), params, null).getResultlist(); if (temps != null && temps.size() == 1) { result = temps.get(0); } return result; }
上面的代码的实行时间在1356条的时候是0:01:30,而这些代码的执行时间呢?0.002S效率在那呢?因为伴随业务逻辑循环在变大。
总结:
1。其实写循环没有什么,只是不要随业务逻辑的增加而循环量增大就可以了!
2。循环很好CUP,尤其是大循环。