Is it me or has Fantom performance dropped? I remember it being almost on par with Scala and Java but recent quicksort test I did seem to indicate it is between 9 to 14 times slower. Just for the record here is my implementation (please correct if there is something wrong):
class QuicksortFantom
{
static Void swap (Int[] a, Int i, Int j)
{
temp := a[i]
a[i] = a[j]
a[j] = temp
}
static Void quicksort(Int[] a, Int L, Int R) {
m := a[(L + R) / 2];
i := L;
j := R;
while (i <= j) {
while (a[i] < m)
i+=1;
while (a[j] > m)
j-=1;
if (i <= j) {
swap(a, i, j);
i+=1;
j-=1;
}
}
if (L < j)
quicksort(a, L, j);
if (R > i)
quicksort(a, i, R);
}
static Void quicksortArray(Int[] a) {
quicksort(a, 0, a.size - 1);
}
static Void main(Str[] args) {
// Sample data
a := Int[,]
1000000.times |i|
{
a.add(i * 3 / 2 + 1)
if(i%3==0) {a[i]=-a[i]}
}
t1 := Duration.now
quicksortArray(a);
t2 := Duration.now
echo((t2-t1).toMillis)
}
}
Are the reasons for the slower execution known (is it the recursive calling or using Int instead of Java's int?)?
brianThu 30 Jun 2011
Don't know what your Java program is like, but Fantom performance should be pretty much exactly the same if you are comparing apples to apples. Remember that Int in Fantom is long in Java. And Int[] is like ArrayList<Long> in Java, not an actual array.
DanielFathThu 30 Jun 2011
Ok, here is my modified Quicksort for java and it reached similar performance as you suggested (Fantom takes ~1100ms while Java needs ~815ms).
import java.util.ArrayList;
public class QuicksortJava {
public static void swap(ArrayList<Long> a, long i, long j) {
long temp = a.get((int)i);
a.set((int)i, a.get((int)j));
a.set((int)j, temp);
}
public static void quicksort(ArrayList<Long> a, long L, long R) {
long m = a.get((int)(L + R) / 2);
long i = L;
long j = R;
while (i <= j) {
while (a.get((int)i) < m)
i++;
while (a.get((int)j) > m)
j--;
if (i <= j) {
swap(a, i, j);
i++;
j--;
}
}
if (L < j)
quicksort(a, L, j);
if (R > i)
quicksort(a, i, R);
}
public static void quicksort(ArrayList<Long> a) {
quicksort(a, 0, a.size() - 1);
}
public static void main(String[] args) {
// Sample data
long size = 1000000;
ArrayList<Long> a = new ArrayList<Long>((int)size);
for (long i = 0; i < size; i++) {
a.add(i * 3 / 2 + 1);
if (i % 3 == 0)
a.set((int)i, -a.get((int)i));
}
long t1 = System.currentTimeMillis();
quicksort(a);
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);
}
}
DanielFath Wed 29 Jun 2011
Is it me or has Fantom performance dropped? I remember it being almost on par with Scala and Java but recent quicksort test I did seem to indicate it is between 9 to 14 times slower. Just for the record here is my implementation (please correct if there is something wrong):
Are the reasons for the slower execution known (is it the recursive calling or using Int instead of Java's int?)?
brian Thu 30 Jun 2011
Don't know what your Java program is like, but Fantom performance should be pretty much exactly the same if you are comparing apples to apples. Remember that
Int
in Fantom islong
in Java. AndInt[]
is likeArrayList<Long>
in Java, not an actual array.DanielFath Thu 30 Jun 2011
Ok, here is my modified Quicksort for java and it reached similar performance as you suggested (Fantom takes ~1100ms while Java needs ~815ms).