我在Surface Pro 2平板电脑上运行带有Java 7更新45 x64(没有安装32位Java)的Windows 8.1 x64.
当i的类型为long时,下面的代码需要1688ms,当i是int时,代码需要109ms.为什么在具有64位JVM的64位平台上,long(64位类型)比int慢一个数量级?
我唯一的猜测是,CPU需要更长的时间来添加64位整数而不是32位整数,但这似乎不太可能.我怀疑Haswell不使用纹波进位加法器.
我在Eclipse Kepler SR1中运行它,顺便说一句.
public class Main {
private static long i = Integer.MAX_VALUE;
public static void main(String[] args) {
System.out.println("Starting the loop");
long startTime = System.currentTimeMillis();
while(!decrementAndCheck()){
}
long endTime = System.currentTimeMillis();
System.out.println("Finished the loop in " + (endTime - startTime) + "ms");
}
private static boolean decrementAndCheck() {
return --i < 0;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:以下是VS 2013(下面),同一系统编译的等效C++代码的结果. 长:72265ms int:74656ms 这些结果是在调试32位模式下.
在64位发布模式下: 长:875ms long long:906ms int:1047ms
这表明我观察到的结果是JVM优化怪异而不是CPU限制.
#include "stdafx.h"
#include "iostream"
#include …Run Code Online (Sandbox Code Playgroud) 通过小字节数组我的意思是从10长度最多为30个字节的阵列.
通过商店我的意思是将它们存储在RAM中,而不是序列化并持久保存到文件系统.
系统macOS 10.12.6,Oracle jdk1.8.0_141 64位,JVM args -Xmx1g
示例:
预期的行为new byte[200 * 1024 * 1024]是≈200mb的堆空间
public static final int TARGET_SIZE = 200 * 1024 * 1024;
public static void main(String[] args) throws InterruptedException {
byte[] arr = new byte[TARGET_SIZE];
System.gc();
System.out.println("Array size: " + arr.length);
System.out.println("HeapSize: " + Runtime.getRuntime().totalMemory());
Thread.sleep(60000);
}
Run Code Online (Sandbox Code Playgroud)
public static final int TARGET_SIZE = 200 * 1024 * 1024;
public static void main(String[] args) throws InterruptedException {
final …Run Code Online (Sandbox Code Playgroud) 在C#中,有两种方法可以创建多维数组.
int[,] array1 = new int[32,32];
int[][] array2 = new int[32][];
for(int i=0;i<32;i++) array2[i] = new int[32];
Run Code Online (Sandbox Code Playgroud)
我知道第一种方法在内部创建一维数组,第二种方法创建一个数组数组(访问速度较慢).
但是在Java中,没有[,]这样的东西,我看到多维数组声明如下:
int[][] array3 = new int[32][32];
Run Code Online (Sandbox Code Playgroud)
由于这种语法在C#中是非法的,而Java没有int[,],我想知道这是否等效array1?或者它仍然是一个数组数组?
我有三个关于三个嵌套循环的问题:
for (int x=0; x<400; x++)
{
for (int y=0; y<300; y++)
{
for (int z=0; z<400; z++)
{
// compute and store value
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要存储所有计算值.我的标准方法是使用3D阵列:
values[x][y][z] = 1; // test value
Run Code Online (Sandbox Code Playgroud)
但事实证明这很慢:完成这个循环需要192毫秒,其中只有一个int-assignment
int value = 1; // test value
Run Code Online (Sandbox Code Playgroud)
只需66毫秒.
1)为什么数组如此相对较慢?
2)当我把它放在内循环中时,为什么它变得更慢:
values[z][y][x] = 1; // (notice x and z switched)
Run Code Online (Sandbox Code Playgroud)
这需要超过4秒!
3)最重要的是:我可以使用与分配单个整数一样快的数据结构,但可以存储与3D数组一样多的数据吗?
java arrays performance multidimensional-array data-structures
我正在尝试使用Key,Value对实现数据结构,并且正在研究数组实现.
实现此目的的一种方法是为Key和Values声明单独的1-D数组.
private int[] keys = new int[N];
private int[] values = new int[N];
Run Code Online (Sandbox Code Playgroud)
但是,通过声明如下的二维阵列并且不会在数据局部性上妥协,可以实现同样的目的吗?
private int[][] keysAndValues = new int[2][N];
Run Code Online (Sandbox Code Playgroud)
在这里,Java以行主顺序实现多维数组似乎很重要吗?以这种方式声明数组是否有任何性能优势,或者这是否会降低代码的可读性?