java 高性能代码优化

 1、避免创建不必要的对象

       创建对象从来都不应该是一件随意的事情,因为创建一个对象就意味着垃圾回收器需要回收一个对象,而这两步操作都是需要消耗时间的。虽说创建一个对象的代价确实非常小,并且Android 2.3版本当中又增加了并发垃圾回收器机制,这让GC操作时的停顿时间也变得难以察觉,但是这些理由都不足以让我们可以肆意地创建对象,需要创建的对象我们自然要创建,但是不必要的对象我们就应该尽量避免创建。
代码块一:
[java]  view plain  copy
  1. List<String> tempList = new ArrayList<String>();  
  2. String value = "";  
  3. for(int i=0;i<20;i++){  
  4.     value = ""+i;  
  5.     tempList.add(value);  
  6. }  
  7. value = null;  
代码块二:
[java]  view plain  copy
  1. List<String> tempList = new ArrayList<String>();  
  2. for(int i=0;i<20;i++){  
  3.     String value = ""+i;  
  4.     tempList.add(value);  
  5. }  
从上面的代码块来看,第二种显然比第一种更加节省资源。

2、静态比抽象要优
     如果你并不需要访问一个对象中的某些字段,只是想调用它的某个方法来去完成一项通用的功能,那么可以将这个方法设置成静态方法,这会让调用的速度提升15%-20%,同时也不用为了调用这个方法而去专门创建对象了,这样还满足了上面的一条原则。另外这也是一种好的编程习惯,因为我们可以放心地调用静态方法,而不用担心调用这个方法后是否会改变对象的状态(静态方法内无法访问非静态字段)。
[java]  view plain  copy
  1. private static int tempValue = 1;  
  2.   
  3. public static int getValue1(){  
  4.     return tempValue;  
  5. }  
  6.   
  7. public int getValue2(){  
  8.     return tempValue;  
  9. }  
3、使用增强型for循环语法
     增强型for循环(也被称为for-each循环)可以用于去遍历实现Iterable接口的集合以及数组,这是jdk 1.5中新增的一种循环模式。当然除了这种新增的循环模式之外,我们仍然还可以使用原有的普通循环模式。
代码块一:
[java]  view plain  copy
  1. for(int i=0;i<tempList.size();i++){  
  2.     System.out.println("-----tempList:"+tempList.get(i));  
  3. }  
代码块二:
[java]  view plain  copy
  1. int len = tempList.size();  
  2. for(int i=0;i<len;i++){  
  3.     System.out.println("-----tempList:"+tempList.get(i));  
  4. }  
代码块三:
[java]  view plain  copy
  1. for (String string : tempList) {  
  2.     System.out.println("-----tempList:"+string);  
  3. }  
代码块三优于代码块二,代码块二优于代码块一。
4、避免在内部调用Getter/Setter方法
     因为字段搜寻要比方法调用效率高得多,我们直接访问某个字段可能要比通过getters方法来去访问这个字段快3到7倍。不过我们肯定不能仅仅因为效率的原因就将封装这个技巧给抛弃了,编写代码还是要按照面向对象思维的,但是我们可以在能优化的地方进行优化,比如说避免在内部调用getter/setter方法。
[java]  view plain  copy
  1. private int value = 1;  
  2. public int getValue() {  
  3.     return value;  
  4. }  
  5. public void setValue(int value) {  
  6.     this.value = value;  
  7. }  
这是一段get/set方法的代码块,我们来看使用.
[java]  view plain  copy
  1. //setValue(12); 不建议这么使用  
  2. value = 12;  
  3. //int tt = getValue(); 不建议这么使用  
  4. int tt = value;  
spring的良好的扩展性,集成度,IOC,AOP事务,已经是项目的基础条件. 整个项目使用了spring 没有struts,没有hibernate //就极简而言,一个数据库只需要一个Service,就可以查询这个数据库的任意一张表 //以下是我的测试用例 //@Test 查询基本型 public void testObject() throws Exception{ Finder finder=new Finder("select id from [Users] where 1=1 "); finder.append("and userId=:userId").setParam("userId", 6); Integer id = baseFangService.queryForObject(finder, Integer.class); System.out.println(id); } //@Test 查询日期 public void testObjectDate() throws Exception{ Finder finder=new Finder("select cteateTime from Users where id=6 order by id"); Date id = baseFangService.queryForObject(finder, Date.class); System.out.println(id); } //@Test 查询一个对象 public void testObjectUser() throws Exception{ Finder finder=new Finder("select * from Users where id=6 order by id"); Users u = baseFangService.queryForObject(finder, Users.class); System.out.println(u.getName()); } //@Test 查询分页 public void testMsSql() throws Exception{ Finder finder=new Finder("select * from Users order by id"); List<Users> list = baseFangService.queryForList(finder, Users.class, new Page(2)); System.out.println(list.size()); for(Users s:list){ System.out.println(s.getName()); } } //@Test 调用数据库存储过程 public void testProc() throws Exception{ Finder finder=new Finder(); finder.setParam("unitId", 0); finder.setProcName("proc_up"); Map queryObjectByProc = (Map) baseFangService.queryObjectByProc(finder); System.out.println(queryObjectByProc.get("#update-count-10")); } //@Test 调用数据库函数 public void testFunction() throws Exception{ Finder finder=new Finder(); finder.setFunName("fun_userId"); finder.setParam("userId", 6); String userName= baseFangService.queryForObjectByByFunction(finder,String.class); System.out.println(userName); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值