JAVA程序性能调优读书笔记 (1)-Java技巧

最新由于需要处理大数据,很多代码的写法不能像原先那样写的那么随意了。很多代码级别的程序要进行调优处理。为了达到这个目的,也参考和学习了好多的书籍。我将其总结出来供以后参考。

subString()的内存泄漏问题。

使用substring 尽量在new String对象套一下。

        private String str = new String(new char[100000]);
        public String getSubString(int begin, int end) {
            return new String(str.substring(begin, end));
            //在套一层new String()可以减少内存泄漏
        }

List中的 ArrayList、Vector和LinkedList的使用。

ArrayList和Vector使用的是相同的算法。
ArrayList线程是不安全的,Vector线程安全。
LinkedList 使用了循环双向链表结构。会对系统性能产生影响。如果List对象需要经常在任意位置上插入元素,请使用LinkedList。
遍历ArrayList尽量使用for循环是最快的,其次是迭代器,在是forEach语句。

  1. Map中HashMap是无序的,LinkedHashMap 是有序的,TreeMap是可以排序的。

TreeMap的性能慢了HashMap 25%,如果需要进行排序和筛选则TreeMap当仁不让。

优化集合访问代码调优


import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.junit.Test;

public class TestCollectionVisistTuning extends Vector{
    static List collection=new Vector();
    static{
        collection.add("north65");
        collection.add("west20");
        collection.add("east30");
        collection.add("south40");
        collection.add("west33");
        collection.add("south20");
        collection.add("north10");
        collection.add("east9");

    }
    public TestCollectionVisistTuning(){
        this.add("north65");
        this.add("west20");
        this.add("east30");
        this.add("south40");
        this.add("west33");
        this.add("south20");
        this.add("north10");
        this.add("east9");
    }
    /**
    性能最差
    **/
    @Test
    public void testCount() {//循环中重复调用代码  (性能最差)
        long starttime=System.nanoTime();
        int count = 0;
        for(int i = 0; i < collection.size(); i++)//collection.size()每一次都被调用
        {
          if(    ( ((String) collection.get(i)).indexOf("north") != -1 )
               || ( ((String) collection.get(i)).indexOf("west") != -1 )
               || ( ((String) collection.get(i)).indexOf("south") != -1 ) )
            count++;
        }
        long endtime=System.nanoTime();
        System.out.println(endtime-starttime);
    }
    /**
     * 分离循环中被重复的代码
     */
    @Test
    public void testCount1() {
        long starttime=System.nanoTime();
        int count = 0;
        int colsize= collection.size();//优化1:将collection.size() 提出来。
        for(int i = 0; i < colsize; i++)
        {
          if(    ( ((String) collection.get(i)).indexOf("north") != -1 )//有相同的代码,需要进行优化。
               || ( ((String) collection.get(i)).indexOf("west") != -1 )
               || ( ((String) collection.get(i)).indexOf("south") != -1 ) )
            count++;
        }
        long endtime=System.nanoTime();
        System.out.println(endtime-starttime);
    }
    /**
        省略相同的操作。
    **/
    @Test
    public void testCount2() {
        long starttime=System.nanoTime();
        int count = 0;
        String s=null;
        int colsize= collection.size();
        for(int i = 0; i < colsize; i++)
        {
          if(    ( (s = (String) collection.get(i)).indexOf("north") != -1 )//优化2:将相同的操作省略掉
               || (s.indexOf("west") != -1 )
               || (s.indexOf("south") != -1 ))
            count++;
        }
        long endtime=System.nanoTime();
        System.out.println(endtime-starttime);
    }
    /**
     * 减少方法的调用。尽量直接访问元素来处理,而不是调用相对应的接口,速度会更快。
     */
    @Test
    public void testCount2_5() {


        long starttime=System.nanoTime();
        int count = 0;
        String s=null;
        int colsize= this.elementCount;
        for(int i = 0; i < colsize; i++)
        {
          if(    ( (s = (String)this.get(i)).indexOf("north") != -1 )
               || (s.indexOf("west") != -1 )
               || (s.indexOf("south") != -1 ))
            count++;
        }
        long endtime=System.nanoTime();
        System.out.println(endtime-starttime);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值