Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Clojure arrays be as fast as Java arrays

I guess they're the same thing but Clojure uses the Array class to manipulate.

Anyway, I've been told that in Clojure if you really need speed then you can use arrays but between the following programs the Java version is much faster

(time
 (let [data (int-array 100000000)]
   (dotimes [q 100000000]
     (aset-int data q q))))

_

public class Array{
    public static void main(String[] args){
    long start = System.currentTimeMillis();
    int[] data = new int[100000000];
    for(int q = 0;q < data.length;q++){
        data[q] = q;
    }
    System.out.println(System.currentTimeMillis() - start);
    }
}

In contrast, this Clojure program that uses the IntBuffer class is almost as fast as the Java code

(time
 (let [data (IntBuffer/allocate 100000000)]
   (dotimes [q 100000000]
     (.put data q q))))
like image 674
HahaHortness Avatar asked Jun 01 '11 05:06

HahaHortness


1 Answers

Don't use aset-* functions. Just use aset: (aset data q q). Don't ask me why the aset-* functions are there. As long as I can remember their use was discouraged.

like image 190
kotarak Avatar answered Oct 02 '22 23:10

kotarak