前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
需要记录每个任务执行时间,或者记录一段代码执行时间,简单方法是打印当前时间与执行完时间的差值,若执行大量测试很麻烦并且不直观。若想对执行时间做进一步控制,则需要在程序中很多地方修改。spring-framework提供了一个StopWatch类可以做类似任务执行时间控制。
[java] view plain copy print?
- StopWatch sw = new StopWatch();
- sw.start();
- // 业务操作
- sw.stop();
- logger.info("耗时间:" + sw.getTotalTimeMillis());
2 配合拦截器
在filter中用spring StopWatch来统计每个请求的执行时间。在filter的doFilter中加入如下代码:
[java] view plain copy print?
- StopWatch stopWatch = new StopWatch(url+System.currentTimeMillis());
- stopWatch.start();
- doFilter(arg0,arg1);
- opWatch.stop();
- loginfo(stopWatch.getTotalTimeMillis()+"---"+request.getRequestURI()+"执行时间");
从源代码构造看出,StopWatch根据id构造对象,确保构造id唯一即可区分不同的请求。
[java] view plain copy print?
- public StopWatch() {
- keepTaskList = true;
- taskList = new LinkedList();
- id = "";
- }
- public StopWatch(String id) {
- keepTaskList = true;
- taskList = new LinkedList();
- this.id = id;
- }
原贴地址:
http://blog.csdn.net/linfssay/article/details/7680323
http://blog.csdn.net/u012186154/article/details/54923210