没有影响。hashCode ---> position 基本上都是基于位操作的。比如,HashMap: /** * Applies a supplemental hash function to a given hashCode, which * defends against poor quality hash functions. This is critical * because HashMap uses power-of-two length hash tables, that * otherwise encounter collisions for hashCodes that do not differ * in lower bits. Note: Null keys always map to hash 0, thus index 0. */ static int hash(int h) { // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } /** * Returns index for hash code h. */ static int indexFor(int h, int length) { return h & (length-1); } hash是把对象的hashCode变成自己的hash,indexFor是把hash变成对应的position。现在几乎找不到32位以下系统了,32位系统,数据带宽都是32bit的,CPU一次能处理32位,也就是4个字节的数据。把int换成short,在32bit系统下,用到的指令和时钟周期也是完全一样,只不过short浪费了2个车道而已。
这也就是为什么short x = 100; short y = 200; x ^ y 的类型自动是int的缘故
不要说 10w 了,就算数值是 0 也需要占用 4 个字节。这 4 个字节一般是按 CPU 寻址系统中的字长为依据的。与 CPU 寻址字长一样处理速度是最快的。少于 4 个字节的整数类型,比如 byte, short, char 在内存中实际上是占用 4 个字节的,32 位系统中 4 个字节是最小的占用。理论上 byte 只有 true 和 false 占用 1 位就可以了,但实际远比我们想象得复杂,其占用了 4 个字节 32 位。PS:Java 中的 int 在 HotSpot JVM 中是经过特别优化的,对于 int 的运算速度比 C 语言还快。
是不就像等于在问这两者效率差别int hashCode() { byte b = xxx; return b; }int hashCode() { int i = xxx; return i; }撇开程序级算法复杂度区别的影响,剩下的是不是就是硬件级处理的差别了
声明int i = 1; 的时候不会因为 int 太大了
这么声明影响效率 还是 用 byte 吧
/**
* Applies a supplemental hash function to a given hashCode, which
* defends against poor quality hash functions. This is critical
* because HashMap uses power-of-two length hash tables, that
* otherwise encounter collisions for hashCodes that do not differ
* in lower bits. Note: Null keys always map to hash 0, thus index 0.
*/
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
} /**
* Returns index for hash code h.
*/
static int indexFor(int h, int length) {
return h & (length-1);
}
hash是把对象的hashCode变成自己的hash,indexFor是把hash变成对应的position。现在几乎找不到32位以下系统了,32位系统,数据带宽都是32bit的,CPU一次能处理32位,也就是4个字节的数据。把int换成short,在32bit系统下,用到的指令和时钟周期也是完全一样,只不过short浪费了2个车道而已。
不要说 10w 了,就算数值是 0 也需要占用 4 个字节。这 4 个字节一般是按 CPU 寻址系统中的字长为依据的。与 CPU 寻址字长一样处理速度是最快的。少于 4 个字节的整数类型,比如 byte, short, char 在内存中实际上是占用 4 个字节的,32 位系统中 4 个字节是最小的占用。理论上 byte 只有 true 和 false 占用 1 位就可以了,但实际远比我们想象得复杂,其占用了 4 个字节 32 位。PS:Java 中的 int 在 HotSpot JVM 中是经过特别优化的,对于 int 的运算速度比 C 语言还快。
byte b = xxx;
return b;
}int hashCode() {
int i = xxx;
return i;
}撇开程序级算法复杂度区别的影响,剩下的是不是就是硬件级处理的差别了