-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array iteration performance issue #310
Comments
Oh well. I try to cache locally whenever possible. If you see places where I didn't, it is because I forgot it. A PR would be welcome! |
Additionally, I have noticed that iterating over the array in the forward direction is faster and more readable compared to iterating in reverse order. I believe this could be considered as an optimization, and I am willing to submit a pull request to modify the reverse iteration in fastutil if necessary. Looking forward to your guidance. |
fastutil has been written > 20y ago, and at that time a series of compilation optimizations made reserve loop faster. I think things change and it's possible current JVM have better compilation. Do you have any benchmarks for, say, Arrays.fill()? And, what is java.util.Arrays.fill() doing today? |
In the benchmark of filling array, reverse filling is twice as fast as forward filling in Java 8, with no significant differences observed in Java 17. Benchmark code: @Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 3)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.SECONDS)
public class ArrayFillBenchmark {
private int[] a;
@Setup
public void setup() {
a = new int[1000000];
}
@Benchmark
public void fill() {
Arrays.fill(a, 1);
}
@Benchmark
public void forward() {
int[] a = this.a;
for (int i = 0, len = a.length; i < len; i++) {
a[i] = 1;
}
}
@Benchmark
public void reverse() {
int[] a = this.a;
for (int i = a.length - 1; i >= 0; i--) {
a[i] = 1;
}
}
@Benchmark
public void reverse2() {
int[] a = this.a;
for (int i = a.length; i-- != 0;) {
a[i] = 1;
}
}
} Benchmark result:
|
Arrays.fill, Arrays.copyOf, System.arraycopy etc. are AFAIK (a tiny bit) faster than their "naive" counterparts. Concretely |
Based on the conclusion in pull request #316, running benchmark on JDK
According to the benchmark results, iterating arrays in forward yields a 10% higher throughput compared to iterating arrays in reverse. |
Whether caching fields or not, it will not affect performance. |
In a benchmark focused on array iteration, I observed a substantial performance improvement of nearly four times when using foreach or storing the array in a local variable. I noticed that in certain array iteration scenarios within fastutil, arrays are not stored in local variables (e.g.,
ArrayList.drv
,ArrayMap.drv
). I'm uncertain whether this should be considered as an issue. Below is my benchmark code:Benchmark result:
I am seeking guidance on whether the decision not to store arrays in local variables is intentional or if it might be worth considering as an optimization opportunity. Your insights into this matter would be greatly appreciated.
The text was updated successfully, but these errors were encountered: