提高嵌套循环效率,使用嵌套循环将索引行乘以列索引的异或值输入到数组中?
Improve nested loop efficiency and enter the xor value of the index row times the index of the column into an array using the nested loop?
我正在编写这段代码,它遍历嵌套数组并输入索引行的异或值乘以列的索引。正常循环有效,但我想提高循环的效率,这样当我尝试 运行 多次使用更高的数字时,不会出现堆问题。
这是有效的常规循环代码-
long[][] ar= new long[(int)m][(int) n];
long m=8,n=5;
long k =1,newp=100;
long sum=0,sum1=0;
for(long i=0; i< ar.length;i++){
for(long j=0;j<ar[0].length;j++){//time received
ar[(int) i][(int) j]= i ^ j;
sum+=ar[(int) i][(int) j];
这是我对更高效循环的尝试-
long m=8,n=5;
long[][] ar= new long[(int)m][(int) n];
long sum1=0;
for(long i: ar){
for(long j[0]:ar){//time received
ar[j][i]= (i ^ j);
sum+=ar[i][j];
}
}
嵌套循环似乎有效,但数组似乎没有接收变量以及 integer/long 类型的总和。帮助将不胜感激。另外,我应该如何更改 xor 计算,使其正确-i ^ j
堆栈跟踪:
java.lang.OutOfMemoryError: Java heap space
at Immortal.elderAge(Immortal.java:6)
at ImmortalTest.example(ImmortalTest.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40)
at org.junit.vintage.engine.VintageTestEngine$$Lambda2/0x00000008400d9c40.accept(Unknown Source)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:195)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
考虑到 XOR 是一种交换运算,即 x ^ y == y ^ x
。
,可以稍微优化二维数组的填充
因此对于数组的“正方形”部分(而 i
和 j
低于 N = Math.min(m, n)
),应考虑“三角形”部分,不包括主对角线将填充 0(因为 x ^ x == 0
)。因此,而不是 N
2 操作,它将需要 N * (N - 1) / 2
操作。
对于剩余的部分(超过 min),异或结果应该像以前一样计算。
int min;
int max;
boolean moreRows;
if (m > n) {
min = n;
max = m;
moreRows = true;
} else {
min = m;
moreRows = false;
max = n;
}
int sum = 0;
int[][] ar2 = new int[m][n];
// square part
for (int i = 0; i < min; i++) {
for (int j = 0; j < i; j++) {
int t = i ^ j;
ar2[i][j] = ar2[j][i] = t;
sum += 2 * t;
}
}
for (int i = min; i < max; i++) {
for (int j = 0; j < min; j++) {
int t = i ^ j;
sum += t;
if (moreRows) {
ar2[i][j] = t;
} else {
ar2[j][i] = t;
}
}
}
for (int[] row: ar2) {
System.out.println(Arrays.toString(row));
}
System.out.println("sum: " + sum);
m = 5
、n = 8
的输出:
[0, 1, 2, 3, 4, 5, 6, 7]
[1, 0, 3, 2, 5, 4, 7, 6]
[2, 3, 0, 1, 6, 7, 4, 5]
[3, 2, 1, 0, 7, 6, 5, 4]
[4, 5, 6, 7, 0, 1, 2, 3]
sum: 140
我正在编写这段代码,它遍历嵌套数组并输入索引行的异或值乘以列的索引。正常循环有效,但我想提高循环的效率,这样当我尝试 运行 多次使用更高的数字时,不会出现堆问题。 这是有效的常规循环代码-
long[][] ar= new long[(int)m][(int) n];
long m=8,n=5;
long k =1,newp=100;
long sum=0,sum1=0;
for(long i=0; i< ar.length;i++){
for(long j=0;j<ar[0].length;j++){//time received
ar[(int) i][(int) j]= i ^ j;
sum+=ar[(int) i][(int) j];
这是我对更高效循环的尝试-
long m=8,n=5;
long[][] ar= new long[(int)m][(int) n];
long sum1=0;
for(long i: ar){
for(long j[0]:ar){//time received
ar[j][i]= (i ^ j);
sum+=ar[i][j];
}
}
嵌套循环似乎有效,但数组似乎没有接收变量以及 integer/long 类型的总和。帮助将不胜感激。另外,我应该如何更改 xor 计算,使其正确-i ^ j 堆栈跟踪:
java.lang.OutOfMemoryError: Java heap space
at Immortal.elderAge(Immortal.java:6)
at ImmortalTest.example(ImmortalTest.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40)
at org.junit.vintage.engine.VintageTestEngine$$Lambda2/0x00000008400d9c40.accept(Unknown Source)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:195)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
考虑到 XOR 是一种交换运算,即 x ^ y == y ^ x
。
因此对于数组的“正方形”部分(而 i
和 j
低于 N = Math.min(m, n)
),应考虑“三角形”部分,不包括主对角线将填充 0(因为 x ^ x == 0
)。因此,而不是 N
2 操作,它将需要 N * (N - 1) / 2
操作。
对于剩余的部分(超过 min),异或结果应该像以前一样计算。
int min;
int max;
boolean moreRows;
if (m > n) {
min = n;
max = m;
moreRows = true;
} else {
min = m;
moreRows = false;
max = n;
}
int sum = 0;
int[][] ar2 = new int[m][n];
// square part
for (int i = 0; i < min; i++) {
for (int j = 0; j < i; j++) {
int t = i ^ j;
ar2[i][j] = ar2[j][i] = t;
sum += 2 * t;
}
}
for (int i = min; i < max; i++) {
for (int j = 0; j < min; j++) {
int t = i ^ j;
sum += t;
if (moreRows) {
ar2[i][j] = t;
} else {
ar2[j][i] = t;
}
}
}
for (int[] row: ar2) {
System.out.println(Arrays.toString(row));
}
System.out.println("sum: " + sum);
m = 5
、n = 8
的输出:
[0, 1, 2, 3, 4, 5, 6, 7]
[1, 0, 3, 2, 5, 4, 7, 6]
[2, 3, 0, 1, 6, 7, 4, 5]
[3, 2, 1, 0, 7, 6, 5, 4]
[4, 5, 6, 7, 0, 1, 2, 3]
sum: 140