Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-24 Thread Alex Miller
That seems like a decent list. Also sounds like most of the Clojure Alioth contributions. :) -- 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

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-24 Thread Didier
Here's my findings: Speed increase from most increase to least: * Pre-sizing the HashSet - from 4.7ms to 3.7ms * Inlining - from 4.7ms to 3.9ms * Using point. constructor instead of ->point - from 2.4ms to 2ms * Using non relfective HashSet init - from 2.36ms to 2.17ms * Using iterator instead of

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-22 Thread Didier
@Marczyk, I did try your improvements, and it shaved off 2 seconds, from 4s for the nth5 to 2s for your implementation. I'm curious to try it one change at a time to see if any one of the changes was responsible for a bigger part, or if its an its of equal improvements that total up to a big

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-22 Thread Andy Fingerhut
More likely, records being just as slow with Clojure 1.9.0-alpha14 probably mean that recalculating of record hashes was not a significant amount of the time your program was taking. Thanks for trying it out. Andy On Mon, Nov 21, 2016 at 5:03 PM, Didier wrote: > I tried it

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-22 Thread Steve Miner
> On Nov 21, 2016, at 8:03 PM, Didier wrote: > > @miner: Doesn't using the flag (set! *unchecked-math* :warn-on-boxed) gives > me unchecked math automatically? I was under the impression that +, -, /, * > etc. would all now perform in an equal way to unchecked-add, etc. If

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-21 Thread Michał Marczyk
PS. Results for the original input on my box. Going by the the timings posted above, yours is rather beefier, so this is probably faster than the current F# version. (c/quick-bench (nth-shell 2000 (point. 0 0))) Evaluation count : 6 in 6 samples of 1 calls. Execution time mean :

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-21 Thread Michał Marczyk
Some further optimizations for a factor of ~2.3 speed-up over nth5 as copy & pasted from upthread (6.713742 ms → 2.897630 ms) in (c/quick-bench (nth-shell 100 (point. 0 0))) (1) java.util.HashSet has a ctor that takes initial capacity of the set as an int. Passing in (* 4 (.size s1)) when

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-21 Thread Didier
I tried it with the safe equals, and it is slightly slower, but still faster then all others at 4.5ms. The non safe equals gives me 4s. Though this is now within my error margin. If ire-run quick-bench, I sometime get a mean equal for each, so I don't think the instance check adds that much

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-21 Thread Steve Miner
> On Nov 21, 2016, at 3:05 PM, Didier wrote: > > I experimented with this a lot, and took everyone's advice, and this is the > fastest I got, even faster then Michael Gardner's answer. > > (deftype point5 [^long i ^long j] > Object > (equals [this that] (and (= (.i

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-21 Thread Andy Fingerhut
Not sure which version of Clojure you are using, but in all versions up to the latest 'official' release, Clojure 1.8.0, records have their hash value calculated every time they are needed, with no caching of the calculated value. The hash value is needed every time an operation is done in a

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-21 Thread Didier
I experimented with this a lot, and took everyone's advice, and this is the fastest I got, even faster then Michael Gardner's answer. (deftype point5 [^long i ^long j] Object (equals [this that] (and (= (.i this) (.i ^point5 that)) (= (.j this) (.j ^point5 that

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-16 Thread Michael Gardner
Below is the fastest version I tested, using ideas from the various responses in this thread. It runs in ~4s on my machine, compared with ~27s for the original version. The biggest win by far was from James Reeves' suggestion of switching to Java's mutable HashSet. I'm not sure why; I'd

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-16 Thread Alex Miller
In general, any benchmark code using math should be aware of boxing (old post here: http://insideclojure.org/2014/12/15/warn-on-boxed/). I would recommend doing the work to leverage primitive longs and unchecked math. Generally this makes numeric code like this about 2 orders of magnitude

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-16 Thread Alex Miller
On Wednesday, November 16, 2016 at 8:10:27 AM UTC-6, Jason Felice wrote: > > I'll bet the atom accesses dominate the computation. They are a part of > Clojures software transactional memory (STM) and have a cost. > Atoms don't use the STM and if used in a single-threaded context like this,

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-16 Thread Jason Felice
I'll bet the atom accesses dominate the computation. They are a part of Clojures software transactional memory (STM) and have a cost. Something like this (untested) should be faster: (->> (iterate neighbors #{p}) (drop 1999) first) neighbors could be: (into #{} (for [[id jd] [[-1 0] [+1 0]

Re: Help me understand what part of this code is slow, and how to make it faster?

2016-11-16 Thread James Reeves
On 16 November 2016 at 03:39, Didier wrote: > > Currently, this takes about 30s in Clojure, while it only takes around 3s > for OCaml, Rust and F#. > > From what I see, the differences between my code and theirs are: > >- Lack of a Point struct, I'm just using a vector. >

Help me understand what part of this code is slow, and how to make it faster?

2016-11-15 Thread Didier
Hey all, I came upon a benchmark of F#, Rust and OCaml, where F# performs much faster then the other two. I decided for fun to try and port it to Clojure to see how Clojure does. Benchmark link: https://github.com/c-cube/hashset_benchs This is my code for it: