Oh also, the default Leiningen settings are optimized for reducing startup, 
not for speed. Use:

  ^:jvm-opts ^:replace ["-server"]  ;; maybe also set max heap 
with  "-Xmx1g" in there

Or just don't use lein when perf testing. 

On Thursday, May 14, 2015 at 2:44:38 PM UTC-5, Alex Miller wrote:
>
> The major problem here is that you are using boxed math for everything 
> instead of primitives.
>
> 0) Switch to Clojure 1.7.0-beta3 - it's faster and some things below are 
> dependent on it for best performance. And use Java 1.8.
> 1) Parse the lines you're reading directly into longs (Clojure focuses on 
> 64-bit primitives - longs and doubles)
> 2) Put the longs first into a data structure that preserves the primitive 
> type. The two best options for that here are records (which can have 
> primitive fields) and arrays. I would create a Canvas defrecord with ^long 
> width and height and a Paper defrecord with all ^long fields for example. 
> 3) Store the papers in a vector (using transient to create it)
> 4) I suspect visible-color and covered? could probably be tightened up 
> into a reduce over papers or a single loop-recur over papers - can't say I 
> totally get what's happening there.
> 5) In visible-color-frequencies, you could use "update" instead of get + 
> transient assoc! on the acc map, but this is never going to be terribly 
> fast. Another option here would be to create an array with the max color 
> (you could track that while reading if it's not a well-known answer) and 
> bash the array. That can retain int or long counters and will be *way* 
> faster.
> 6) You can use (set! *unchecked-math* :warn-on-boxed) to get faster math 
> (no overflow checks) and also issue warnings (added in 1.7) if you happened 
> to use boxed math by accident. 
>
>
>
> On Thursday, May 14, 2015 at 3:02:42 AM UTC-5, Amith George wrote:
>>
>> I wrote the following code to solve this challenge - 
>> https://www.reddit.com/r/dailyprogrammer/comments/35s2ds/20150513_challenge_214_intermediate_pile_of_paper/
>> .
>>
>> Code - 
>> https://github.com/amithgeorge/reddit-dailyprogrammer-clojure/blob/56ce1dbb6a08e96150dc85934caecfeb68108a53/src/rdp/214_intermediate.clj
>>
>> I executed the -main function using `lein run 1`. 
>>
>> Output
>>
>>     ;; lein run 1
>>
>>     0 12605919
>>     1 3578145
>>     2 15356894
>>     3 19134293
>>     4 2394558
>>     5 15030409
>>     6 6424953
>>     7 14893444
>>     8 1592254
>>     9 1914025
>>     10 7075106
>>     "Elapsed time: 501168.972435 msecs"
>>
>> The code originally used an immutable hashmap, but I lost patience 
>> waiting for the computation to end. With mutable hashmap, it still takes 
>> around 8 mins.
>>
>> I wrote a C# version of the above code - 
>> https://gist.github.com/amithgeorge/766b8220f39d48221e58. It finishes 
>> under 40secs. The C# exe was built under Release mode and executed directly 
>> from the commandline. I expected the Clojure version to perform similarly.
>>
>> Any tips on what I am doing wrong?
>>
>> -----
>> Explanation of the code - Create a vector of all paper sheets, such that 
>> the sheet placed last is the first element of the vector and the last 
>> element is the canvas. To compute the frequency of each visible color - for 
>> each point in the canvas find the first sheet in the vector that covers the 
>> point. Store/increment its count in the hashmap. I understand there might 
>> be better more efficient ways to solve this, but currently I am interested 
>> in why the Clojure versions is so slow vis-a-vis the C# version.
>>
>>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to