This thread made me realize there's quite a bit of low hanging
compatibility fruit between Clojure & ClojureScript, so as of this
changeset
http://github.com/clojure/clojurescript/compare/4652498035...3c0fb6ef5f
We can now compile Christophe's example code in ClojureScript w/ exactly 4
minor changes. We could imagine handling these differences with feature
expressions:
(deftype F [^:unsynchronized-mutable ^ints curr
^:unsynchronized-mutable ^ints prev]
IFn ; <===== CHANGED
(invoke [_ a1 a2]
(let [^objects a1 a1
^objects a2 a2]
(areduce a1 i max-len 0
(let [m (areduce a2 j max-len max-len ; <===== CHANGED
(let [match-len
(if (identical? (aget a1 i) (aget a2 j)) ; <=====
CHANGED
(unchecked-inc (aget prev j))
0)]
(aset curr (unchecked-inc j) match-len) ; <===== CHANGED
(if (> match-len max-len)
match-len
max-len)))
bak curr]
(set! curr prev)
(set! prev bak)
m)))))
(defn my-lcs2 [^objects a1 a2]
(let [n (inc (alength a1))
f (F. (int-array n) (int-array n))]
(f a1 a2)))
Running under the Google V8 JS engine I see an execution time of ~2.2
seconds using the same benchmark on the StackOverflow question. This is
compared to a ~1s running time for the JVM. This is nearly within 2X of the
JVM!
The pretty printed Closure advanced optimized code for the important bits:
Sg.prototype.call = function(a, b, c) {
for(var a = this, d = 0, f = 0;;) {
if(d < b.length) {
var h = d + 1, i;
i = g;
a: {
i = 0;
for(var k = f;;) {
if(i < c.length) {
var f = i + 1, p = b[d] === c[i] ? a.gb[i] + 1 : 0;
a.cb[i + 1] = p;
k = p > k ? p : k;
i = f
}else {
i = k;
break a
}
}
i = g
}
d = a.cb;
a.cb = a.gb;
a.gb = d;
d = h;
f = i
}else {
return f
}
}
};
That looks like some highly optimized JS to me ;)
I think I'll stick with writing my fast code in Clojure thank you very much.
David
On Thu, Feb 21, 2013 at 4:49 PM, David Nolen <[email protected]> wrote:
>
> On Thu, Feb 21, 2013 at 4:55 AM, Marko Topolnik
> <[email protected]>wrote:
>
>> Whatever the final performance achieved, the fact remains that the
>> original Java code was much cleaner, simpler, and more comprehensible than
>> the big ball of mud the performant Clojure version is turning into.
>
>
> To my eyes the deftype version is about as clean, simple, comprehensible,
> as the Java version. But I've been doing Clojure for a while now.
>
> Christophe's version also has the advantage that it can pretty much
> compile down to efficient JavaScript via ClojureScript and probably an
> efficient ClojureCLR program as well. This may or may not matter to you.
>
> David
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.