提高嵌套循环效率,并使用嵌套循环将索引行的xor值乘以列的索引,将其输入到数组中?

问题描述:

我正在编写这段代码,该代码经过一个嵌套数组,然后输入索引行的xor值乘以列的索引.正常的循环可以工作,但是我想提高循环的效率,所以当我尝试对更高的数字多次运行时,不会出现堆问题.这是有效的常规循环代码-

I am writing this piece of code which goes through a nested array and enters the xor value of the index row times the index of the column. The normal loop works but I want to improve the efficiency of the loop so there won't be heap problems when I try to run it multiple times on higher numbers. here is the regular loop code that works-

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];

这是我尝试的更有效的循环-

here is my attempt at the more efficient loop-

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];
}
}

嵌套循环似乎有效,但数组似乎不接收变量以及整数/长型和.帮助将不胜感激.另外,我应该如何更改 xor 计算,使其正确无误- i ^ j 堆栈跟踪:

the nested loop seems to work, but the array doesn't seem to receive the variables as well as the integer/long type sum. help would be appreciated. Also, how should I change the xor calculation so that it will be correct-i ^ j stack trace:

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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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$$Lambda$212/0x00000008400d9c40.accept(Unknown Source)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$3$1.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 ^,可以稍微优化2D数组的填充量.x .

It is possible to optimize slightly population of the 2D array taking into account that XOR is a commutative operation, that is x ^ y == y ^ x.

因此表示正方形".数组的一部分(而 i j N = Math.min(m,n)之下),则是一个三角形";该部分应考虑排除将填充0的主对角线(因为 x ^ x == 0 ).因此,将执行 N *(N-1)/2 个操作,而不是 N 2 个操作.

Thus for the "square" part of the array (while i and j are below N = Math.min(m, n)), a "triangle" part should be considered excluding the main diagonal which will be populated with 0 (because x ^ x == 0). So instead of N2 operations it will take N * (N - 1) / 2 operations.

对于其余部分(超过分钟),XOR结果应与以前一样计算.

For the remaining part (over min) the XOR results should be calculated as before.

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