boolean[] 与 BitSet:哪个更有效? [英] boolean[] vs. BitSet: Which is more efficient?

查看:36
本文介绍了boolean[] 与 BitSet:哪个更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在内存和 CPU 使用率方面哪个更有效——boolean 数组或 BitSet?不使用具体的BitSet方法,只使用get/set/clear(==,=,Arrays.fill分别为一个数组).

What is more efficient in terms of memory and CPU usage — an array of booleans or a BitSet? Specific BitSet methods are not used, only get/set/clear (==, =, Arrays.fill respectively for an array).

推荐答案

来自 Sun JDK 1.6 用筛子计算素数的一些基准测试(最好的 10 次迭代预热,给 JIT 编译器一个机会,并排除随机调度延迟, 酷睿 2 双核 T5600 1.83GHz):

From some benchmarks with Sun JDK 1.6 computing primes with a sieve (best of 10 iterations to warm up, give the JIT compiler a chance, and exclude random scheduling delays, Core 2 Duo T5600 1.83GHz):

BitSet 比 boolean[] 内存效率更高,除了非常小的尺寸.数组中的每个布尔值都占用一个字节.来自 runtime.freeMemory() 的数字对于 BitSet 来说有点混乱,但更少.

BitSet is more memory efficient than boolean[] except for very small sizes. Each boolean in the array takes a byte. The numbers from runtime.freeMemory() are a bit muddled for BitSet, but less.

boolean[] 的 CPU 效率更高,除了非常大的尺寸,它们几乎是偶数.例如,对于大小为 100 万的 boolean[] 大约快四倍(例如 6ms vs 27ms),对于 10 和 100 万,它们大约是相等的.

boolean[] is more CPU efficient except for very large sizes, where they are about even. E.g., for size 1 million boolean[] is about four times faster (e.g. 6ms vs 27ms), for ten and a hundred million they are about even.

这篇关于boolean[] 与 BitSet:哪个更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆