`
BestUpon
  • 浏览: 284158 次
  • 性别: Icon_minigender_1
  • 来自: 兰州
社区版块
存档分类
最新评论

写点程序的那些糗事(1)

    博客分类:
  • J2EE
阅读更多

 

写点程序就要认真点,为什么呢?会有效率问题的!引例:

今天电力公司的满意度测评系统开始正式的运行测评了,然而有一件很溴的事情让人难看,前面填写一份问卷不到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,尤其是大循环。

 

分享到:
评论

相关推荐

    仿糗事百科微信小程序

    仿糗事百科微信小程序 1. 实现顶部页签菜单左右滑动效果 2. 实现顶部页签菜单切换效果,页签菜单选中时字体加粗,同时对应的内容也跟着变化 3. 实现专享界面糗事列表设计,包括发布人头像、发布人昵称、发布的段子等...

    高仿糗事百科程序源码 免费

    高仿糗事百科程序源码 免费

    uni-app实战仿糗事百科开发前端源码带素材.rar

    uni-app实战仿糗事百科开发前端源码带素材 资源目录: 【20190705更新】【前端完整代码】仿糗事百科.rar 【前端完整代码】【更新至256课时】仿糗事百科.rar App截图.rar socket测试工具.rar 图标.rar 图片素材.rar

    糗事百科python爬虫程序

    python程序用于自动获取糗事百科的内容

    仿糗事百科

    莫仿糗事百科

    2013最新糗事百科源码

    2013最新糗事百科源码|捧腹网源码|笑话类源码,yicms商业版内核,2套模板,含会员中心,完全开源,请放心使用。也是转来的,没有测试,自行测试,不喜欢的不要下,不存在骗分的说法。解压密码见注释

    微信小程序案例:仿糗事百科源码

    微信小程序案例:仿糗事百科源码

    高仿糗事百科项目源码

    糗事百科 系统的源码 可以拿来学习 希望对大家有帮助

    2014最新仿糗事百科源码

    最新2014仿糗事百科主题,PHP开发仿糗事百科主题!

    2013最新仿糗事百科源码

    2013最新仿糗事百科源码 欢迎下载,已测试完整

    python_爬虫——爬取糗事百科

    亲测有效,不管是windows 还是Linux都...使用python 爬取糗事百科的段子,通过回车键控制,一次一个,同时可以把看过的内容保存到本地查看 具体实现可以参照 http://blog.csdn.net/qiqiyingse/article/details/60583129

    仿糗事百科客户端_android版

    该程序为仿新版的糗事百科客户端,数据是通过jsoup来抓取糗事百科网页版的,程序能正常运行,由于原客户端中有些功能模块的数据不能通过网页中抓取到,所有这类功能模块没有实现。程序sdk为android 4.0以上的版本,...

    python获取糗事百科段子

    获取糗事百科段子程序,拥有UI交互界面 下载之后无需任何配置,可以直接运行 具体的程序请参考:http://blog.csdn.net/qiqiyingse/article/details/64522690

    糗事百科源码

    糗事百科的源码,有需要的可以看看,只要5积分就可以下载了

    糗事百科windows桌面客户端-纯文字版

    糗事百科windows桌面客户端-纯文字版 程序运行后位置任务栏位置上面 右键单击退出程序 左键单击翻页

    糗事百科源码 笑话网站源码程序 分享图

    解压密码:SDF234_zzcodes.net_DLS735 源码介绍:TINKPHP二次开发 糗事百科源码 ... 其它说明:以下程序完全采用TINKPHP开发! 在功能上也有查考某仿糗事百科上的一些技术。其次,TINKPHP完全开源,这点请放心使用。

    精仿糗事百科网源码

    程序安装: 输入 http://你的域名/install 按照提示填写 账号密码:admin admin888 安装完成后进入后台 系统 - 数据库备份/还原 - 数据还原 然后开始还原 还原后,先修改管理员密码。 注意: 用了一键更新之后...

    discuz仿糗事百科程序带手机版

    基于DiscuzX系列开发的discuz高仿糗事百科网站源码,支持外链视频,外链图显示。

    仿糗事百科.rar_Hbuilding

    仿糗事百科,Hbuilding 多端输出.

    糗事百科小偷程序V1.2.rar

    你可以通过这种小偷程序,完成过去一些似乎完全不可能实现的任务,比如说把某个站的页面偷梁换柱后变成自己的页面,或者把某个站的一些数据(文章,图片)保存到本地数据库中加以利用。   “小偷程序”的优点. ...

Global site tag (gtag.js) - Google Analytics