布尔[]对位集合:哪个更有效? [英] Boolean[] vs. BitSet: Which is more efficient?

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

问题描述

在什么内存和CPU使用方面更加高效 - 布尔值或位集合的数组?不使用特定的位集合的方法,只能得到/设置/清除(= =,=,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):

位集合更多的内存比布尔[]除了非常小的尺寸高效。阵列中的每个布尔取一个字节。从runtime.freeMemory()的数字是有点懵了位集合,但较少。

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.

布尔[]是更多的CPU除了非常大的规模,在那里他们约什高效。例如,尺寸为百万布尔[]大约快四倍(例如6ms的VS 27ms),十和一亿他们是约什。

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.

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

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