简体   繁体   中英

Java: method in static initialization block is slower than in main methd

For some reason, my method "bishops" runs much faster when called from the main method than from the static initialization block. Is this normal, or a bug?

public class Magic
{
    public static void main(String[] args)
    {
        bishops();
    }

    public static void bishops()
    {
        //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("bishops.txt")));
        BISHOP_SHIFTS = new int[64];
        BISHOP_COMBOS = new long[64][];
        for (int square = 0; square < 64; square++) {System.out.println("bbb " + square);
            int NUMBER = bitCount(BISHOP_ATTACKS[square]);
            BISHOP_SHIFTS[square] = 64 - NUMBER;
            long x = BISHOP_ATTACKS[square];
            long[] MAPS = new long[NUMBER];
            for (int n = 0; n < NUMBER; n++) {
                int i = bitScan(x);
                MAPS[n] = (1L << i);
                x -= MAPS[n];
            }
            int C = 1 << NUMBER;
            BISHOP_COMBOS[square] = new long[C];
            for (int i = 0; i < C; i++) {
                BISHOP_COMBOS[square][i] = 0;
                int j = i;
                for (int n = 0; n < NUMBER; n++) {
                    if ((j & 1) == 1)
                        BISHOP_COMBOS[square][i] |= MAPS[n];
                    j >>>= 1;
                }
                //out.println("SQUARE " + square);
                //out.println(toBitboardString(BISHOP_COMBOS[square][i]));
                //out.println();
            }
        }
        //out.close();

        bishopMagics();
    }

    public static void bishopMagics()
    {
        BISHOP_MAGICS = new long[64];
        Random r = new Random();

        for (int square = 0; square < 64; square++) {System.out.println("asdffff " + square);
            int i;
            int LENGTH = BISHOP_COMBOS[square].length;
            long magic;
            do {
                magic = r.nextLong() & r.nextLong() & r.nextLong();
                //final int COUNT = bitCount(BISHOP_MASKS[square]);
                boolean[] used = new boolean[LENGTH];
                for (int j = 0; j < used.length; j++)
                    used[j] = false;
                for (i = 0; i < LENGTH; i++) {
                    int index = (int) ((BISHOP_COMBOS[square][i] * magic) >>> BISHOP_SHIFTS[square]);
                    if (used[index])
                        break;
                    else
                        used[index] = true;
                }
            } while (i < LENGTH);
            BISHOP_MAGICS[square] = magic;
            System.out.println(magic);
        }

        //bishopTable();
    }

    /*
     * Lots of stuff omitted
     */

    static
    {
        //bishops();
    }
}

It will run much faster the second time than the first as the JVM warms up (loads class es and compiles code). The static block is always called first.

Try running it twice from the main() or the static block and see how long it takes each time

BTW: I would take out any logging to the console as this can slow down the code dramatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM