[英]Why does using a boolean array take so much longer?
我正在上課作業。 我應該創建一個Conway的《人生游戲》的簡單版本。 我在使用布爾值數組與使用BitSet之間進行了一些實驗。 我最終創建了這個小測試:
每次對BitSet和布爾數組都運行測試后,bitset的平均值約為6ms,布爾數組的平均值約為1300ms。 所以我的問題是,究竟是什么導致使用布爾值而不是使用BitSet導致時間的巨大開銷? 我期望有所不同,但不會如此劇烈。
這是代碼:
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();
}
您的問題與BitSet
與boolean[]
性能無關。
在BitSetBoard
,您將length()
定義為:
class BitSetBoard {
...
@Override
public int length()
{
return board.length();
}
}
您的意思是返回board.size()
而不是board.length()
。 該BitSet.length()
方法返回的最高位集合的指數,它不返回的總規模。 因此,您的主循環實際上沒有執行任何操作,因為在清除木板時length()
返回0。
進行此更改(並在BitSetBoard.get()
添加了邊界檢查),對我來說, BitSetBoard
運行時間是ArrayBoard
兩倍。
BitSet比boolean []的存儲效率更高,但其大小非常小,您可以閱讀進一步澄清
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.