On Feb 3, 3:54 pm, "Shen, Feng" <[email protected]> wrote:
> > I was able to shave off some more by writing (not= 0 (rem i d)) as (<
>
> 0 (rem i d)).
>
> the running time is about 4787ms on my computer, compare to *2759ms *of the
> java version, a bit slower, but not much.
>
> 沈锋
> 美味书签 :http://meiweisq.com
> 博客:http://shenfeng.me
>
> On Sun, Feb 3, 2013 at 6:42 PM, Shantanu Kumar 
> <[email protected]>wrote:
>
>
>
>
>
>
>
>
>
> > On Feb 3, 12:54 pm, Curtis Gagliardi <[email protected]>
> > wrote:
> > > I took your version Feng and used rem instead of mod and added a type
> > hint
> > > and got down from:
> > > 23217.321626 => 11398.389942
>
> > > No idea where to go from here though.  I'm surprised there's such a
> > > difference even not using any sort of collection.
>
> > > (defn smallest-multiple-of-1-to-n-hinted-rem
> > >   [^long n]
> > >   (loop [i n]
> > >     (if (loop [d 2]
> > >           (cond (> d n) true
> > >                 (not= 0 (rem i d)) false
> > >                 :else (recur (inc d))))
> > >       i
> > >       (recur (inc i))))
>
> > I was able to shave off some more by writing (not= 0 (rem i d)) as (<
> > 0 (rem i d)).

For some reason, by splitting out the inner loop into a function
shaved 6 more seconds (from 34s to 28s) on my low-config 32-bit
laptop:

(defn every-d?
  [^long x ^long n]
  (loop [d 2]
    (cond (> d n) true
          (< 0 (rem x d)) false
          :otherwise (recur (inc d)))))

(defn smallest-multiple-of-1-to-n
  [n]
  (let [n (long n)]
    (loop [x n]
      (if (every-d? x n)
        x
        (recur (inc x))))))

Changing (< 0 ...) to (pos? ..) had no noticeable delay. This is of
course only a micro-benchmark and numbers would probably change when
the JIT compiler kicks in.

With *unchecked-math* set to true, the Clojure version above takes
28s. Interestingly, when I modified the Java version to use Long, the
time went up from 18s to 40s and for unboxed long, the time went up
from 18s to 37s.

Shantanu

-- 
-- 
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.


Reply via email to