Java:静态初始化 block 中的方法比主方法中的方法慢

标签 java methods

由于某种原因,我的方法“bishops”在从主方法调用时比从静态初始化 block 调用时运行得更快。这是正常现象还是错误?

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();
    }
}

最佳答案

随着 JVM 预热(加载类并编译代码),第二次运行速度将比第一次快得多。静态 block 总是首先被调用。

尝试从 main() 或静态 block 运行两次,看看每次需要多长时间

顺便说一句:我会删除控制台的所有日志记录,因为这会大大减慢代码速度。

关于Java:静态初始化 block 中的方法比主方法中的方法慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11229291/

相关文章:

java - 在 Java 中如何在字符串 I/O 和对象 I/O 流之间切换?

java - 重写 equals 和 hashCode - java

c# 字典方法作为一个值

oop - Golang,调用 parent 的方法

java - 如何获取JAVA中所有被调用的方法

java - 如何知道方法是否使用反射将数组作为参数

java - 在类 NullPointerException 中实例化一个类

Java嵌套泛型类型

java - Exoplayer - 在 fragment 内旋转时保存和恢复状态

java - 如何推断此泛型类型的上限?