问题描述
例如:
-
)
INT [X] [Y] [Z]
VS
B) INT [X * Y * Z]
起初以为我会用走)为简单起见
Initially thought i'd go with a) for simplicity
我知道,Java不线性阵列存储在内存中像C一样。但是,这是否有什么影响我的程序?
I know that Java doesn't store arrays linearly in memory like C does. But what implications does this have for my program?
推荐答案
做通常是最好的事情时,为这些问题寻找anwers是看选择是如何编译成字节JVM code:
Usually the best thing to do when searching anwers for such questions is to see how the choices are compiled into JVM bytecode:
multi = new int[50][50];
single = new int[2500];
这是翻译成
BIPUSH 50
BIPUSH 50
MULTIANEWARRAY int[][] 2
ASTORE 1
SIPUSH 2500
NEWARRAY T_INT
ASTORE 2
所以你可以看到JVM已经知道,我们是在谈论一个多维数组。
so as you can see the JVM already knows that we are talking about a multi dimensional array..
保持它进一步:
for (int i = 0; i < 50; ++i)
for (int j = 0; j < 50; ++j)
{
multi[i][j] = 20;
single[i*50+j] = 20;
}
这是翻译(跳过周期)为:
this is translated (skipping the cycles) into:
ALOAD 1: multi
ILOAD 3: i
AALOAD
ILOAD 4: j
BIPUSH 20
IASTORE
ALOAD 2: single
ILOAD 3: i
BIPUSH 50
IMUL
ILOAD 4: j
IADD
BIPUSH 20
IASTORE
因此,大家可以看到多维数组在内部处理到虚拟机,通过无用的指令没有开销产生的,同时采用了事务所人使用,因为是手工计算的偏移。更多说明
So as you can see the multi-dimensional array is treated internally into the VM, no overhead generated by useless instructions, while using a sigle one uses more instructions since offset is calculated by hand.
我不认为性能会这样一个问题。
I don't think that performance will be such an issue..
编辑:
我做了一些简单的基准测试,看看发生了什么事情到这里。我选择尝试不同的例子:线性读,写的线性和随机访问。时间是的毫秒pssed(使用同步除权计算$ P $ System.nanoTime()
..这里的结果:
I did some simple benchmarks to see what's going down here. I chose try different examples: linear read, linear write and random access. Time are expressed in millisecs (and calculated using System.nanoTime()
.. here are the results:
线性写
- 尺寸:100 * 100(10000)
多:5.786591
单:6.131748 - 尺寸:200×200(40000)
多:1.216366
单:0.782041 - 尺寸:500×500(250000)
多:7.177029
单:3.667017 - 尺寸:1000×1000(1000000)
多:30.508131
单:18.064592 - 尺寸:2000×2000(400万)
多:185.3548
单:155.590313 - 尺寸:5000x5000(25000000)
多:955.5299
单:923.264417 - 尺寸:10000x10000(100000000)
多:4084.798753
单:4015.448829
线性阅读
- 尺寸:100 * 100(10000)
多:5.241338
单:5.135957 - 尺寸:200×200(40000)
多:0.080209
单:0.044371 - 尺寸:500×500(250000)
多:0.088742
单:0.084476 - 尺寸:1000×1000(1000000)
多:0.232095
单:0.167671 - 尺寸:2000×2000(400万)
多:0.481683
单:0.33321 - 尺寸:5000x5000(25000000)
多:1.222339
单:0.828118
大小:10000x10000(100000000)
多:2.496302
单:1.650691
随机读
- 尺寸:100 * 100(10000)
多:22.317393
单:8.546134 - 尺寸:200×200(40000)
多:32.287669
单:11.022383 - 尺寸:500×500(250000)
多:189.542751
单:68.181343 - 尺寸:1000×1000(1000000)
多:1124.78609
单:272.235584 - 尺寸:2000×2000(400万)
多:6814.477101
单:1091.998395 - 尺寸:5000x5000(25000000)
多:50051.306239
单:7028.422262
但随机一个是有点误导,因为它会产生2的随机数的多维数组,而只有我一个一维(和PNRGs可能会占用一些CPU)。
But random one is a little bit misleading since it generates 2 random numbers for multi-dimensional array while just one for single dimensional (and PNRGs may consume some CPU).
介意,我想只有同一个循环的20后运行标杆让JIT的工作。为了完整我的Java VM如下:
Mind that I tried to let JIT work by benchmarking only after the 20th run of the same loop. For completeness my java VM is the following:
Java版本1.6.0_17
的Java(TM)SE运行时环境(建立1.6.0_17-B04)
Java的热点(TM)64位服务器VM(14.3建造-B01,混合模式)
这篇关于渣:多维阵列与一维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!