Performance on arrays
in
Programming Questions
•
1 year ago
Hello,
I've got a performance related question. I've made a test program in Processing and Java, but I don't unterstand the result of the Processing sketch. There are three types of functions:
1) swaps two array values -> swap()
2 ) 1 + returns the array -> swap2()
3) 2 + assigns the result to the old array -> a = swap2()
The result in Java ( as expected from fastest to slowest): swap() -> swap2() -> a = swap2()
The result in Processing: a = swap2() -> swap2() -> swap() ( = reversed)
Can anyone explain me this behaviour or did I made a silly mistake?
Thanks in advance.
Results in Processing (10 tries):
Java:
Processing:
I've got a performance related question. I've made a test program in Processing and Java, but I don't unterstand the result of the Processing sketch. There are three types of functions:
1) swaps two array values -> swap()
2 ) 1 + returns the array -> swap2()
3) 2 + assigns the result to the old array -> a = swap2()
The result in Java ( as expected from fastest to slowest): swap() -> swap2() -> a = swap2()
The result in Processing: a = swap2() -> swap2() -> swap() ( = reversed)
Can anyone explain me this behaviour or did I made a silly mistake?
Thanks in advance.
Results in Processing (10 tries):
Java:
public class Test {
static int n = 100000000;
static int[] a = {
1, 2
};
static long std;
static void swap(int[] vals) {
int tmp = vals[0];
vals[0] = vals[1];
vals[1] = tmp;
}
static int[] swap2(int[] vals) {
int tmp = vals[0];
vals[0] = vals[1];
vals[1] = tmp;
return vals;
}
public static void main(String[] args) {
std = System.currentTimeMillis();
for (int i = 0; i < n; i++)
a = swap2(a);
System.out.println(System.currentTimeMillis() - std);
std = System.currentTimeMillis();
for (int i = 0; i < n; i++)
swap2(a);
System.out.println(System.currentTimeMillis() - std);
std = System.currentTimeMillis();
for (int i = 0; i < n; i++)
swap(a);
System.out.println(System.currentTimeMillis() - std);
System.exit(0);
}
}
Processing:
- int n = 100000000;
int[] a = {
1, 2
};
long std;
void swap(int[] vals) {
int tmp = vals[0];
vals[0] = vals[1];
vals[1] = tmp;
}
int[] swap2(int[] vals) {
int tmp = vals[0];
vals[0] = vals[1];
vals[1] = tmp;
return vals;
}
void setup() {
std = millis();
for (int i = 0; i < n; i++)
a = swap2(a);
println(millis() - std);
std = millis();
for (int i = 0; i < n; i++)
swap2(a);
println(millis() - std);
std = millis();
for (int i = 0; i < n; i++)
swap(a);
println(millis() - std);
exit();
}
1