I love the dot syntax, but Clojure doesn't have any of the intra-symbol syntax's that arc has and I really struggle getting back into using them :)... even [ _ ] is foreign to me now, but just for you ^_^:
(def sort-by-commonest5 (seq)
(mappend [do _]
(sort (fn (x y)(> len.x len.y))
((afn (xs cxs ys)
(if empty.xs ys
(withs (x car.xs
f (testify [isnt _ x])
r (reclist [if (f:car _) _] xs)
cr len.r)
(self r cr (cons (firstn (- cxs cr) xs) ys)))))
(sort > seq) len.seq nil))))
arc> (time10 (do (sort-by-commonest5 data) nil))
time: 1073 msec.
For sortby-commonest0, freshly loaded, here are the first 10 runs:
(3081 2102 1803 1585 1564 1529 1596 1813 1571 1580)
As you can see with the JVM it warms up and running the same code gets faster...
For example the second run (and my 340 must have been down the road?):
(581 553 539 540 543 550 541 542 544 536)
Then I tried a comparable succinct approach in Clojure
(no counts, so I used frequencies):
(defn sort-by-commonest01 [xs]
(let [h (frequencies xs)]
(sort (comparitor > #(h %)) xs)))
first 10: (4568 2750 2495 2125 2399 2131 2123 2133 2056 1087)
second 10: (1064 1074 1070 1069 1070 1101 1058 1062 1090 1066)
It's really hard to make decent comparisons (pun intended)... i.e. I mean should I only compare the first attempts, freshly loaded to make valid comparisons?
Not sure what I gain from this... I think it's fair to say the succinct version is slower than the longer version. Also I haven't even considered what the idiomatic approach would be in Clojure and I'm guessing Clojure has a built in function that's written by some code wizard :)
c) Not that it'll really save anything when it comes to the overall computational complexity, but this version of list length comparison will only traverse the lists up to the end of the shorter one:
(def longer-list (a b)
(if a
(if b
(longer-list cdr.a cdr.b)
t)
nil))
d) Where you say "(if empty.xs ys <else>)", you can probably squeeze out more performance with "(if xs <else> ys)". You can actually use "(iflet (x) xs <else> ys)" here too, but I don't know what that'll do to the performance.
e) There's no need to call 'testify.
After incorporating a bunch of this feedback--I didn't pit '< against '> --the code might look like this:
I don't follow which of those results is for clojure vs arc. sort-by-commonest0 ranges from 3081 to 536ms, while the clojure sort-by-commonest01 ranges from 4568 to 1066ms? I'm surprised that clojure is so consistently slower.
Can you show the first twenty runs for the arc sort-by-commonest2 vs the clojure sort-by-commonest01?
1. I made a big mistake on my time calculator for Clojure... it was using nanoTime with rounding errors. I had to re-write it. This is now correct with no rounding:
Yes, .... But, Clojure being faster was not really the part I cared about. What I feel this shows is that the straight forward idiomatic approach is, while concise, not always the optimal solution. The investigation was an attempt to consider other approaches, gain a better understanding of the languages and find some optimal paths. Comparing Arc to Clojure shows both languages have a similar ratio in performance metrics for the two cases, which allows me to normalize my approaches and not assume that an approach for one is equally applicable to the other.
I'm not surprised that the readable way is less performant; I'm reassured that the price for readability is quite low. I was totally unsurprised that both require similar optimizations given that both are fairly mature toolchains. Compilers by and large get their performance the same way.
The one thing that can cause the optimal optimizations to shift is different machines with different capacities and hierarchies of caches and RAM. But this is a compute-bound program so even that's not a concern.
Update: I should add that I enjoyed this summary of yours. The previous comment just gave data and left your conclusions unspoken.
I agree, and my apologies, I did give only data - at the time of the writing I was trying not to draw conclusions and leave the door open for other options to come forward, but I should have followed up.
I'm not so sure I can agree with the price for readability being quite low. One could call it premature optimization, but a ~50% gain is pretty significant in my mind. Had it been 20% or less I would probably go forward and spend less of my time attempting alternate approaches, but at ~50% I think playing around and learning the boundaries and their benefits can yield positive results for me.
It's 50% if you do just that. In a larger app it's a difference of 1ms.
Now you could argue that everything in an arc program will be slower so I should consider it to be 50%. Lately I've been mulling the tradeoff of creating optimized versions vs just switching languages. When I built keyword extraction for readwarp.com I was shocked how easy it was to add a C library using the FFI. Why bother with a less readable, hard-to-maintain arc version for 2x or 5x if you can get 50-100x using an idiomatic C version?
The whole point of a HLL is to trade off performance for readability; I'd rather back off that tradeoff in a key function or two.
---
Shameless plug section
Wart currently has an utterly simple FFI: create a new C file, write functions that take in Cells and return Cells, run:
$ wart
and they get automatically included and made available.
But I want to do more than just an FFI. I have this hazy idea of being able to specify optimization hints for my code without actually needing to change it or affect its correctness. They might go into a separate file like the tests currently do.
I dream of being able to go from simple interpreter to optimized JIT using just HLL lisp code (and a minimum of LLVM bindings). So far the prospect of attempting this has been so terrifying that I've done nothing for weeks..
I think that's generalizing too much. Using that one function with our nominal data set may only cost some ms, but what if you're dealing with hundreds of millions records? It may then, even though only representing .001% of your code base, account for 90% of your operating time - which is when someone normally kicks in with the "premature optimization" argument, which I can't really argue against, other than to say optimizing code is a skill that is generally done well by those who take it into account to begin with.
"Now you could argue that everything in an arc program will be slower so I should consider it to be 50%"
I wouldn't think this to be the case. I'm sure there's a tonne of juice squeezing one can do, but as a general statement, having played around with a lot of arc's code, I would guess most optimizing would yield less than 10%, but these 50+%, while they are few and far between are still worth the effort (to me).
The key, in all this, is to understand these languages well enough to make good judgment calls on where best to invest ones time.
I can't say much about wart and the rest (you're much deeper into language design than I am). :)
"what if you're dealing with hundreds of millions records?"
Then just write it in C :)
Let me try to rephrase my argument. Sometimes you care about every last cycle, most of the time you care about making it a little bit faster and then you're done. Sometimes your program needs large scale changes to global data structures that change the work done, sometimes it needs core inner loops to just go faster. Sometimes your program is still evolving, and sometimes you know what you want and don't expect changes.
just a little faster + work smarter => rearchitect within arc.
just a little faster + faster inner loops => rewrite them in C
every last cycle + rigid requirements => gradually rewrite the whole thing in C and then do micro-optimizations on the C sources. You could do arc optimizations here, but this is my bias.
every last cycle + still evolving => ask lkml :)
If you're doing optimizations for fun, more power to you. It's pretty clear I'm more enamored with the readability problem than the performance problem.
I don't want to sound more certain than I am about all this. Lately I spend all my time in the 'just a little faster' quadrant, and I like it that way.
"The key, in all this, is to understand these languages well enough to make good judgment calls on where best to invest ones time."
I don't disagree with your thought's, however I don't think they account for the TCQ aspect of everyone's situation.
Let's put put it another way. In my situation, if I have to look a using C then it's already game over[1]. However I can, with my limited amount of time (lunches, evenings and weekends) become proficient enough in Clojure (with a continual thanks to Arc).
What I am suggesting is that knowing the language well enough to easily identify these 50%+ hitters is a matter of finding low hanging fruit and at the same time becoming a better Arc/Clojure programmer. It does not mean I want to change my direction in the heavily-optimized/low-level to high-level/low-maintenance/readable code continuum.
[1] I'm not a programmer[2], I am a business analyst that works with software that simulates oil & gas production from reservoirs that sit several hundred kilometers underground. Simulation scenario's are run for 20 year production periods across hundreds of interdependent wells. The current software tools run the simulations across many cores, they take 8 days to run to completion and they cost about a half a million dollars per seat (+18% per year maintenance fees). This cost can be attributed to the R&D that occurred 8-10 years ago (i.e they required a team(s?) of P.Engs writing software in C to maximize the performance). Eight years ago you couldn't do (pmap[3] (sortby-commonest or whatever.... )) so easily. Nowadays I have the opportunity to create a 70% solution all by my lonesome, costing only a portion of my time. Hence why understanding the language well enough to find the low hanging fruit and not having to use C, is probably bigger deal to me.
[2] Well, maybe I shouldn't say this... rather I should say it's not my day job. I have no formal education in the field. I'm pretty much self taught.
[3] pmap is the same as map only it distributes the load across multiple processors in parallel.