使用Findbugs检查出的几个Performance问题
来源:互联网 发布:unity3d动画制作 编辑:程序博客网 时间:2024/05/15 04:42
1. Method invokes inefficient Number constructor; use static valueOf instead
Long buyerPickupAddressId = new Long(84); -> Long buyerPickupAddressId = Long.valueOf(84);
2. Method concatenates strings using + in a loop
Suggest: use StringBuffer
3. Inefficient use of keySet iterator instead of entrySet iterator
package com.cathy.mywebtest.common;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
/**
* 测试keySet()与entrySet()的迭代时间
* keySet():迭代后只能通过get()取key
* entrySet():迭代后可以e.getKey(),e.getValue()取key和value。返回的是Entry接口
* 最后发现keySet()的速度比entrySet()慢了很多。
*/
public class HashMapTest
{
public static void main(String[] args)
{
HashMap<String,String> kmap = new HashMap<String,String>();
HashMap<String, String> emap = new HashMap<String, String>();
for (int i = 0; i < 1000; i++)
{
kmap.put(""+i, "KEYSET");
}
for (int i = 0; i < 1000; i++)
{
emap.put(""+i, "ENTRYSET");
}
long stimes = System.currentTimeMillis();
long ctimes = Calendar.getInstance().getTimeInMillis();
long dtimes = new Date().getTime();
//初始时间 这里用了三种取值方式 最后发现System.currentTimeMillis();是最直接的取值方法
System.out.println(stimes+" "+ctimes+" "+dtimes);
Iterator<String> ktor = kmap.keySet().iterator();
while(ktor.hasNext())
{
System.out.println(kmap.get(ktor.next()));
}
long stimes1 = System.currentTimeMillis();
long ctimes1 = Calendar.getInstance().getTimeInMillis();
long dtimes1 = new Date().getTime();
//结束时间并且也是entrySet的开始时间
System.out.println(stimes1+" "+ctimes1+" "+dtimes1);
System.out.println((stimes1-stimes)+" "+(ctimes1-ctimes)+" "+(dtimes1-dtimes));
Iterator<Entry<String, String>> itor = emap.entrySet().iterator();
while(itor.hasNext())
{
Entry<String, String> e = itor.next();
//System.out.println(e.getKey());
System.out.println(e.getValue());
}
long stimes2 = System.currentTimeMillis();
long ctimes2 = Calendar.getInstance().getTimeInMillis();
long dtimes2 = new Date().getTime();
System.out.println(stimes2+" "+ctimes2+" "+dtimes2);
System.out.println((stimes2-stimes1)+" "+(ctimes2-ctimes1)+" "+(dtimes2-dtimes1));
}
}
- 使用Findbugs检查出的几个Performance问题
- FindBugs代码检查工具的使用
- FindBugs无法使用的问题
- findbugs自定义的问题
- findbugs,PDM,CheckStyle各自的检查项
- findbugs的使用
- FindBugs的使用
- FindBugs的使用
- FindBugs 的使用
- findbugs的使用
- FindBugs的使用
- FindBugs的使用
- Findbugs工具的使用
- FindBugs的使用
- FindBugs的使用
- FindBugs-IDEA的使用
- 使用Keil performance Analyzer 的问题
- 使用FindBugs-IDEA插件找到代码中潜在的问题
- Oracle 10g SGA 的自动化管理
- NKOJ 1791 Party at Hali-Bula(树状DP)
- Linux/Unix下ODBC的安装、配置与编程
- 做菜心得
- 【转】mysql中DATETIME、DATE和TIMESTAMP的区别
- 使用Findbugs检查出的几个Performance问题
- JAVA面试题解惑系列(十)——话说多线程
- [转]如何在捕获需求时开拓思路?
- 如何做MSCRM需求调研
- HDOJ 1754 (线段树)
- 用java实现cache
- JAVA面试题解惑系列(十一)——这些运算符你是否还记得?
- TextBox客户端JS赋值 后台获取
- 交叉编译的一般方法