Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

java - 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?

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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

Output for 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...