为什么使用布尔数组需要更长的时间?

时间:2014-02-23 08:16:16

标签: java arrays bitset

我正在上课。我应该创建Conway的生命游戏的简单版本。我在使用布尔值数组与使用BitSet之间进行了一些实验。我最终创建了这个小测试:

每次为BitSet和布尔数组运行测试后,bitset的平均值约为6ms,布尔数组的平均值约为1300ms。所以我的问题是,使用布尔值使用布尔值时,究竟是什么造成了巨大的开销呢?我预计会有所不同,但不是那么激烈。

以下是代码:

Life.java - 主要类

public class Life
{
    private final int WIDTH = 100;
    private final int HEIGHT = 100;
    private Board board;

    public static void main(String[] args)
    {
        new Life();
    }

    public Life()
    {
        board = createBoard();

        // populate board with random data
        Random random = new Random();
        for (int j = 0; j < board.length(); j++)
        {
            boolean b = random.nextBoolean();
            board.set(j, b);
        }
        random = null;
        System.gc();


        System.out.println("Starting test...");
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < 10_000; i++)
        {
            Board tempBoard = createBoard();
            for (int j = 0; j < board.length(); j++)
            {
                byte count = getNeighborCount(j);
                boolean value = board.get(j);
                boolean next = value ? count >= 2 && count <= 3 : count == 3;
                tempBoard.set(j, next);
            }
            board = tempBoard;
        }

        long endTime = System.currentTimeMillis();
        System.out.format("Took %d ms", endTime - startTime);
    }

    public Board createBoard()
    {
        return new ArrayBoard(WIDTH * HEIGHT);
        //return new BitBoard(WIDTH * HEIGHT);
    }

    public byte getNeighborCount(int index)
    {
        byte count = 0;

        if (getRelative(index, -1, -1)) count++;
        if (getRelative(index, -1, 0)) count++;
        if (getRelative(index, -1, 1)) count++;

        if (getRelative(index, 0, -1)) count++;
        if (getRelative(index, 0, 0)) count++;
        if (getRelative(index, 0, 1)) count++;

        if (getRelative(index, 1, -1)) count++;
        if (getRelative(index, 1, 0)) count++;
        if (getRelative(index, 1, 1)) count++;

        return count;
    }

    public boolean getRelative(int index, int x, int y)
    {
        int relativeIndex = index + (WIDTH * y) + x;
        return board.get(relativeIndex);
    }
}

使用BitSet实现电路板

public class BitBoard implements Board
{
    private BitSet board;

    public BitBoard(int size)
    {
        board = new BitSet(size);
    }

    @Override
    public void set(int index, boolean value)
    {
        if (value) board.set(index);
        else board.clear(index);
    }

    @Override
    public boolean get(int index)
    {
        return board.get(index);
    }

    @Override
    public int length()
    {
        return board.length();
    }
}

使用数组实现电路板

public class ArrayBoard implements Board
{
    private boolean[] board;

    public ArrayBoard(int size)
    {
        board = new boolean[size];
    }

    @Override
    public void set(int index, boolean value)
    {
        board[index] = value;
    }

    @Override
    public boolean get(int index)
    {
        boolean output = false;
        if (index >= 0 && index < board.length)
            output = board[index];
        return output;
    }

    @Override
    public int length()
    {
        return board.length;
    }
}

最后,Board.java

public interface Board
{
    public boolean get(int index);

    public void set(int index, boolean value);

    public int length();
}

2 个答案:

答案 0 :(得分:4)

您的问题与BitSetboolean[]效果无关。

BitSetBoard中,您length()定义为:

class BitSetBoard {

    ...

    @Override
    public int length()
    {
        return board.length();
    }

}

您的意思是返回board.size()而不是board.length()BitSet.length()方法返回最高位集的索引,它 not 返回总大小。因此,你的主循环实际上并没有做任何事情,因为length()在董事会清楚时返回0。

通过此更改(并将边界检查添加到BitSetBoard.get()),BitSetBoard的运行时间仅为ArrayBoard的两倍。

答案 1 :(得分:0)

BitSet比boolean []的内存效率更高,除了非常小的尺寸以便进一步说明你可以阅读

boolean[] vs. BitSet: Which is more efficient?

BitSet uses about 1 bit per boolean value, and boolean[] uses about 1 byte per boolean value. that also the case that BitSet are more efficient
相关问题