Java HashMap在Profiler中的性能和自我时间(netbeans)

时间:2015-06-30 23:21:29

标签: java netbeans hashmap profiling

我正在使用netbeans中的默认探查器测试hashmap的性能。我不明白大多数时间花在这个简单测试函数的 自我时间 上是什么意思

static void randGetKey( int n ){
    Key2i tmpkey = new Key2i( 0, 0 );
    for (int i = 0; i < n; i++) {
        tmpkey.iy = rnd.nextInt( max );
        tmpkey.ix = rnd.nextInt( max ); 
        Point p = keyMap.get( tmpkey  );
    }
}   

该循环的评估每个循环需要273纳秒(10e + 7个循环)。但是使用分析器我发现大部分时间实际上花费在self-time而不是HashMap.get(Object)Random.nextInt()

这是探查器的输出: enter image description here 可能我只是不明白如何读取探查器的结果(或如何使用探查器一般 - 我使用默认设置,即快速(样本))

以防完整代码在这里:

package pkg2d_hashmap_indexing;

import java.awt.Point;
import java.util.HashMap;
import java.util.Random;

class Key2i {

int ix,iy;

Key2i( int ix_, int iy_ ) {
    ix=ix_; iy=iy_;
}

void set( int ix_, int iy_ ){
    ix=ix_; iy=iy_;
}

@Override
public boolean equals(Object o) {
    Key2i b = (Key2i)o;
    return (b.ix == ix)&&(b.iy == iy);
}

@Override
public int hashCode() {
    //return (ix<<16)+iy;
    //return (ix<<16)+iy;
    //return (iy<<16)+ix;
    int x = ix+10000;
    int y = iy+10000;
    //int tmp = ( y +  ((x+1)/2));
    int tmp = ( y +  ((x+1)>>1));
    return x +  ( tmp * tmp);
/*  
    long l = (((long)ix+10000)<<16)+((long)iy+10000);
    return (int)( l*2654435761L )>>32;
*/  
}
}


public class Main {

static public HashMap<Key2i, Point> keyMap;
static public Random rnd;
//static public HashMap<Long, Point> integerPosSimpleMap;

static long t1,t2;
static int max = 1000;
static int nrep = 5;
static int NTOT = max*max;

static int nrand = 10000000;

static int randSpeed(){
    int sum = 0;
    for (int i = 0; i < nrand; i++) {
        sum += rnd.nextInt( max );
        sum += rnd.nextInt( max );
    }
    return sum;
}

static int randHashSpeed(){
    int sum = 0;
    for (int i = 0; i < nrand; i++) {
        sum += rnd.nextInt( max );
        sum += rnd.nextInt( max );
    }
    return sum;
}

static void randGetKey( int n ){
    Key2i tmpkey = new Key2i( 0, 0 );
    for (int i = 0; i < n; i++) {
        tmpkey.iy = rnd.nextInt( max );
        tmpkey.ix = rnd.nextInt( max ); 
        Point p = keyMap.get( tmpkey  );
    }
}   




public static void main(String[] args) {
    rnd = new Random();
    keyMap     = new HashMap<>( 2*NTOT );

    System.out.println("size of map "+NTOT);
    System.out.println( " ======== calibration ========= " );

    t1 = System.nanoTime();
    //Integer itmp;
    int sum;
    sum = randSpeed();
    t2 = System.nanoTime();
    System.out.println( "rand Sum calibration "+ ( (t2-t1)/(float)nrand )+" "+sum );

    t1 = System.nanoTime();
    //Integer itmp;
    sum = randHashSpeed();
    t2 = System.nanoTime();
    System.out.println( "rand hash calibration "+ ( (t2-t1)/(float)nrand )+" "+sum );



    System.out.println( " ======== Key2i ========= " );

    //stringPosSimpleMap
    t1 = System.nanoTime();
    for (int x = 1; x < max; x++) {
        for (int y = 1; y < max; y++) {
            Point p = new Point(x, y);
            Key2i k = new Key2i(x, y);
            Point p_old = keyMap.get(k);
            if( p_old != null ){
                System.out.println( "conflict: "+k+" "+p_old+"   = "+p_old );
            }
            keyMap.put( k, p);
            //System.out.println("getKeyFromPos(x, y) "+getKeyFromPos(x, y));
        }
    }
    t2 = System.nanoTime();
    System.out.println("HashMap<Key2i, Point> put "+ ( (t2-t1)/(float)NTOT ) );

    t1 = System.nanoTime();
    //Integer itmp;
    randGetKey( nrand );
    t2 = System.nanoTime();
    System.out.println( "rand HashMap<Key2i, Point> get "+ ( (t2-t1)/(float)nrand )+" "+nrand );


}

}

0 个答案:

没有答案