开发者

Should Clojure arrays be as fast as Java arrays

开发者 https://www.devze.com 2023-03-09 11:58 出处:网络
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 开发者_如何学Cthen you can use arrays but between the

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 开发者_如何学Cthen 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))))


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消