Re: Anyone want to take a crack at making the fasta shootout program faster?

2011-02-10 Thread Bill James
On Feb 10, 2:47 pm, Isaac Gouy  wrote:
> On Feb 10, 1:17 am, Bill James  wrote:
>
> >http://shootout.alioth.debian.org/u32/benchmark.php?test=fasta〈=all
>
> > The fastest program shown here is in Java and runs in 1.72 seconds.
> > However, at the bottom of the page (under "interesting alternative"
> > programs) there is a C++ program that runs in 0.25 seconds; it seems
> > to work basically the same way that my program does.
>
> > So this Clojure program will probably be relegated to the bottom of
> > the page.
>
> Should it be relegated to the bottom of the page?

The C++ program was evidently deprecated because it did not go through
the process of generating a random character for each character that
it output. The author realized that the pseudo-random-number-generator
had a cycle-length less than 200_000.

Both the C++ program and the Clojure program use a much more efficient
algorithm than the other programs.  That may violate the rules.

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


Re: Anyone want to take a crack at making the fasta shootout program faster?

2011-02-10 Thread Bill James
http://shootout.alioth.debian.org/u32/benchmark.php?test=fasta&lang=all

The fastest program shown here is in Java and runs in 1.72 seconds.
However, at the bottom of the page (under "interesting alternative"
programs) there is a C++ program that runs in 0.25 seconds; it seems
to work basically the same way that my program does.

So this Clojure program will probably be relegated to the bottom of
the page.

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


Re: Anyone want to take a crack at making the fasta shootout program faster?

2011-02-07 Thread Bill James
On Feb 7, 3:47 pm, Andy Fingerhut  wrote:

> (defn find-index [f coll]
>    (loop [i (int 0)
>           s (seq coll)]
>      (if (f (first s))
>        i
>        (recur (unchecked-inc i) (rest s)
>


I didn't realize how slow my version of this was.
This change alone shaves off about half a second
on my computer.

This function needs to be built into the language.

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


Re: Anyone want to take a crack at making the fasta shootout program faster?

2011-02-07 Thread Bill James
Since this program eliminates so much of the work, it's disappointing
that
there wasn't more of a speedup.

Can you (or anyone) speed it up even further?

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


Re: Anyone want to take a crack at making the fasta shootout program faster?

2011-02-06 Thread Bill James
On Feb 3, 12:22 am, Andy Fingerhut  wrote:
> I've done a pass through most of the Clojure programs on the shootout  
> web site recently, making some of them faster, and choosing -Xmx  
> command line arguments when running them to keep the memory usage down  
> to a reasonable level -- not always the smallest heap size that works,  
> mind you -- just one that avoids exorbitantly large memory usage.
>
> http://shootout.alioth.debian.org
>
> The Clojure program for the "fasta" problem, with source code, AOT  
> compilation command, and execution command given on this web page:
>
> http://shootout.alioth.debian.org/u32/program.php?test=fasta&lang=clo...
>
> still takes about 6x to 8x more time than the best Java 6 -server  
> program here, depending upon which of the four machines it is run on:
>
> http://shootout.alioth.debian.org/u32/program.php?test=fasta&lang=jav...
>
> I'm sure the Clojure program can be made faster, e.g. by doing fewer  
> calls to write to the output file, with more bytes per call.  Most of  
> the time seems to be file writing and generating random numbers in gen-
> random!, at least on my systems where I've done testing and  
> profiling.  I'm also seeing a fair amount of time spent in calls to  
> java.lang.Double.valueOf, according to the built-in profiler that  
> comes with the Hotspot JVM.
>
> Note: The web site is Clojure 1.2 only right now, so don't expect a  
> tweaked-out program using things that only work in Clojure 1.3 to work  
> there yet.


This program is considerably faster on my computer:


(set! *warn-on-reflection* true)


(def *width* 60)
(def *lookup-size* 222000)


(def *alu* (str "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"
"CCAGCCTGGCCAACATGGTGAAAGTCTCTACTAT"
"ACATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCA"))

(def *codes* "acgtBDHKMNRSVWY")

(def *iub* [0.27 0.12 0.12 0.27 0.02 0.02 0.02 0.02
0.02 0.02 0.02 0.02 0.02 0.02 0.02])

(def *homosapiens* [0.3029549426680 0.1979883004921
0.1975473066391 0.3015094502008])



(defn find-index [f coll]
  (first (keep-indexed #(if (f %2) %1) coll)))




(def random-seed (int-array [42]))
(let [ IM (int 139968)
   IA (int 3877)
   IC (int 29573)
   scale (double (/ *lookup-size* IM))
 ]
  (defn gen-random-fast []
(let [ new-seed (unchecked-remainder (unchecked-add (unchecked-
multiply
 (aget (ints random-seed) 0) IA) IC) IM) ]
  (aset (ints random-seed) 0 new-seed)
  (int (* new-seed scale)



;; Takes a vector of probabilities.
(defn make-cumulative [v]
  (vec (map #(reduce + (subvec v 0 %))  (range 1 (inc (count v))

;; Takes a vector of cumulative probabilities.
(defn make-lookup-table [v]
  (let [ lookup-scale (- *lookup-size* 0.0001)
 tmp (map
   (fn [n] (find-index #(<= (/ n lookup-scale) %) v))
   (range *lookup-size*)) ]
(int-array tmp)))



(defn cycle-bytes [source source-size n  ^java.io.BufferedOutputStream
ostream]
  (let [ source-size (int source-size)
 width (int *width*)
 width+1 (int (inc width))
 buffer-size (int (* width+1 4096))
 buffer (byte-array buffer-size (byte 10))
 next-i (fn[i]
   (unchecked-remainder (unchecked-add (int i) width) source-
size))
 next-j (fn[j]
(let [j (+ j width+1)]
  (if (= j buffer-size)
(do  (.write ostream buffer)  0)
j)))
   ]
(loop [i (int 0)  j (int 0)  n (int n)]
  (System/arraycopy  source i buffer j width)
  (if (> n width)
(recur (int (next-i i)) (int (next-j j)) (- n width))
(do
  (aset buffer (+ j n) (byte 10))
  (.write ostream buffer 0 (+ j n 1)))



(defn fasta-repeat [n  ^java.io.BufferedOutputStream ostream]
  (let [ source (.getBytes (str *alu* *alu*)) ]
(cycle-bytes source (count *alu*) n ostream)))



(defn fasta-random [probs n  ^java.io.BufferedOutputStream ostream]
  (let [ codes (.getBytes (str *codes*))
 lookup-table (ints (make-lookup-table (make-cumulative
probs)))
 width (int *width*)
 buffer (byte-array 222000)
 seeds  (int-array  222000)
 first-seed (aget (ints random-seed) 0)
   ]
(loop [i (int 0)]
  (aset seeds i (aget (ints random-seed) 0))
  (aset buffer i
(aget codes
  (aget lookup-table
(gen-random-fast
  (if (= (aget (ints random-seed) 0) first-seed)
(do
  (System/arraycopy  buffer 0  buffer (inc i)  *width*)
  (cycle-bytes buffer (inc i) n ostream)
  (aset (ints random-seed) 0 (aget seeds (mod n (inc i)
(recur (unchecked-inc i))



(defn write-line [s 

Re: Time/size bounded cache?

2011-02-06 Thread Bill James
On Feb 6, 2:08 pm, Seth  wrote:

> (filter #(= % 2)  a)

Another way:

(filter #{2}  a)

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


Re: Why take-last of empty collection is nil?

2011-02-03 Thread Bill James
On Feb 3, 1:04 am, Petr Gladkikh  wrote:

>
> And another question. I have written this function
> (defn index-by
>   "Make map (f x) -> x"
>   [f coll]
>   (reduce #(assoc %1 (f %2) %2) {} coll))
>
> I wonder, is there already such function somewhere in Clojure libraries?

Somewhat shorter:

(defn index-by [f coll]
  (zipmap (map f coll) coll))

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


Re: Ridiculously massive slowdown when working with images

2011-01-31 Thread Bill James
On Jan 31, 1:33 pm, Bill James  wrote:
> On Jan 31, 5:46 am, B Smith-Mannschott  wrote:
>
> > > Yes, that speeds it up considerably.  If I can get Java server
> > > installed
> > > on my machine, I'll post some timings.
>
> > What is this "java server" of which you speak.  In JVMs I'm familiar
> > with (Sun/Oracle, OpenJDK) it's just a matter of passing the option
> > -server to java when starting it.
>
> > // Ben
>
> Usually Windoze doesn't have the Java server executable.
> You have to download the Java JDK.

More precisely, the missing file is
C:\Program Files\Java\jre6\bin\server\jvm.dll

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


Re: Ridiculously massive slowdown when working with images

2011-01-31 Thread Bill James
On Jan 31, 1:40 pm, Bill James  wrote:
> (set! *warn-on-reflection* true)
>
> (def buffer-size 192)
> (def array (byte-array buffer-size))
>
> (defmacro add [m n] `(unchecked-add (int ~m) (int ~n)))
> (defmacro uinc [m] `(unchecked-inc ~m))
> (defmacro amove[a i j] `(aset ~a ~j (aget ~a ~i)))
>
> (defn java-like0 [^bytes cpu_array]
>   (loop [i (int 0)]
>     (if (< i (int buffer-size))
>       (let [b (byte (aget cpu_array i))
>             g (byte (aget cpu_array (unchecked-add i (int 1
>             r (byte (aget cpu_array (unchecked-add i (int 2
>             a (byte (aget cpu_array (unchecked-add i (int 3]
>         (aset cpu_array i a)
>         (aset cpu_array (unchecked-add i (int 1)) b)
>         (aset cpu_array (unchecked-add i (int 2)) g)
>         (aset cpu_array (unchecked-add i (int 3)) r)
>         (recur (unchecked-add i (int 4)))
>
> ;;;  Make buffer-size a local.
> (defn java-like1 [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
>     (loop [i (int 0)]
>       (if (< i (int buffer-size))
>         (let [b (byte (aget cpu_array i))
>               g (byte (aget cpu_array (unchecked-add i (int 1
>               r (byte (aget cpu_array (unchecked-add i (int 2
>               a (byte (aget cpu_array (unchecked-add i (int 3]
>           (aset cpu_array i a)
>           (aset cpu_array (unchecked-add i (int 1)) b)
>           (aset cpu_array (unchecked-add i (int 2)) g)
>           (aset cpu_array (unchecked-add i (int 3)) r)
>           (recur (unchecked-add i (int 4
>
> ;;;  Move byte directly from array[i] to array[j].
> ;;;  Assign index values to local variables.
> ;;;  Eliminate "byte" hint.
> (defn java-like2 [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
>     (loop [i (int 0)]
>       (if (< i buffer-size)
>         (let [ i2 (add i 1)
>                i3 (add i 2)
>                i4 (add i 3)
>                a (aget cpu_array i4)
>              ]
>           (amove cpu_array i3 i4)
>           (amove cpu_array i2 i3)
>           (amove cpu_array i i2)
>           (aset cpu_array i a)
>           (recur (add i 4)))
>
> ;;;  Use unchecked-inc instead of unchecked-add.
> (defn java-like3 [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
>     (loop [i (int 0)]
>       (if (< i buffer-size)
>         (let [ i2 (uinc i)
>                i3 (uinc i2)
>                i4 (uinc i3)
>                a (aget cpu_array i4)
>              ]
>           (amove cpu_array i3 i4)
>           (amove cpu_array i2 i3)
>           (amove cpu_array i i2)
>           (aset cpu_array i a)
>           (recur (uinc i4)))
>
> (dotimes [i 4]
>   (let [func-name (str "java-like" i)
>         func (resolve (symbol func-name))]
>     (println func-name)
>     (dotimes [_ 7]
>       (time
>         (func array)
>
> I think it will be faster on my laptop, but for now here are
> the results on a rather old desktop.
>
> Clojure 1.2; Java 1.5.0_07 (server); Pentium 4 3.2GHz (2 cores)
>
> java-like0
> "Elapsed time: 81.954298 msecs"
> "Elapsed time: 24.381966 msecs"
> "Elapsed time: 16.528463 msecs"
> "Elapsed time: 12.656007 msecs"
> "Elapsed time: 12.666522 msecs"
> "Elapsed time: 13.240584 msecs"
> "Elapsed time: 12.625828 msecs"
> java-like1
> "Elapsed time: 56.773426 msecs"
> "Elapsed time: 19.099019 msecs"
> "Elapsed time: 16.669669 msecs"
> "Elapsed time: 6.325035 msecs"
> "Elapsed time: 6.400674 msecs"
> "Elapsed time: 6.374463 msecs"
> "Elapsed time: 6.266154 msecs"
> java-like2
> "Elapsed time: 46.15 msecs"
> "Elapsed time: 11.840942 msecs"
> "Elapsed time: 10.842774 msecs"
> "Elapsed time: 5.56714 msecs"
> "Elapsed time: 5.655566 msecs"
> "Elapsed time: 5.611824 msecs"
> "Elapsed time: 5.509589 msecs"
> java-like3
> "Elapsed time: 37.785107 msecs"
> "Elapsed time: 8.711968 msecs"
> "Elapsed time: 8.377959 msecs"
> "Elapsed time: 7.094104 msecs"
> "Elapsed time: 3.637105 msecs"
> "Elapsed time: 3.757499 msecs"
> "Elapsed time: 3.589582 msecs"


Much faster on my laptop.
Core2 Duo P8600 2.4GHz
Clojure 1.2
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)

java-like0
"Elapsed time: 40.007319 msecs"
"Elapsed time: 11.8015 msecs"
"Elapsed time: 10.262477 

Re: Ridiculously massive slowdown when working with images

2011-01-31 Thread Bill James
(set! *warn-on-reflection* true)

(def buffer-size 192)
(def array (byte-array buffer-size))

(defmacro add [m n] `(unchecked-add (int ~m) (int ~n)))
(defmacro uinc [m] `(unchecked-inc ~m))
(defmacro amove[a i j] `(aset ~a ~j (aget ~a ~i)))


(defn java-like0 [^bytes cpu_array]
  (loop [i (int 0)]
(if (< i (int buffer-size))
  (let [b (byte (aget cpu_array i))
g (byte (aget cpu_array (unchecked-add i (int 1
r (byte (aget cpu_array (unchecked-add i (int 2
a (byte (aget cpu_array (unchecked-add i (int 3]
(aset cpu_array i a)
(aset cpu_array (unchecked-add i (int 1)) b)
(aset cpu_array (unchecked-add i (int 2)) g)
(aset cpu_array (unchecked-add i (int 3)) r)
(recur (unchecked-add i (int 4)))


;;;  Make buffer-size a local.
(defn java-like1 [^bytes cpu_array]
  (let [buffer-size (int buffer-size)]
(loop [i (int 0)]
  (if (< i (int buffer-size))
(let [b (byte (aget cpu_array i))
  g (byte (aget cpu_array (unchecked-add i (int 1
  r (byte (aget cpu_array (unchecked-add i (int 2
  a (byte (aget cpu_array (unchecked-add i (int 3]
  (aset cpu_array i a)
  (aset cpu_array (unchecked-add i (int 1)) b)
  (aset cpu_array (unchecked-add i (int 2)) g)
  (aset cpu_array (unchecked-add i (int 3)) r)
  (recur (unchecked-add i (int 4


;;;  Move byte directly from array[i] to array[j].
;;;  Assign index values to local variables.
;;;  Eliminate "byte" hint.
(defn java-like2 [^bytes cpu_array]
  (let [buffer-size (int buffer-size)]
(loop [i (int 0)]
  (if (< i buffer-size)
(let [ i2 (add i 1)
   i3 (add i 2)
   i4 (add i 3)
   a (aget cpu_array i4)
 ]
  (amove cpu_array i3 i4)
  (amove cpu_array i2 i3)
  (amove cpu_array i i2)
  (aset cpu_array i a)
  (recur (add i 4)))


;;;  Use unchecked-inc instead of unchecked-add.
(defn java-like3 [^bytes cpu_array]
  (let [buffer-size (int buffer-size)]
(loop [i (int 0)]
  (if (< i buffer-size)
(let [ i2 (uinc i)
   i3 (uinc i2)
   i4 (uinc i3)
   a (aget cpu_array i4)
 ]
  (amove cpu_array i3 i4)
  (amove cpu_array i2 i3)
  (amove cpu_array i i2)
  (aset cpu_array i a)
  (recur (uinc i4)))



(dotimes [i 4]
  (let [func-name (str "java-like" i)
func (resolve (symbol func-name))]
(println func-name)
(dotimes [_ 7]
  (time
(func array)


I think it will be faster on my laptop, but for now here are
the results on a rather old desktop.

Clojure 1.2; Java 1.5.0_07 (server); Pentium 4 3.2GHz (2 cores)

java-like0
"Elapsed time: 81.954298 msecs"
"Elapsed time: 24.381966 msecs"
"Elapsed time: 16.528463 msecs"
"Elapsed time: 12.656007 msecs"
"Elapsed time: 12.666522 msecs"
"Elapsed time: 13.240584 msecs"
"Elapsed time: 12.625828 msecs"
java-like1
"Elapsed time: 56.773426 msecs"
"Elapsed time: 19.099019 msecs"
"Elapsed time: 16.669669 msecs"
"Elapsed time: 6.325035 msecs"
"Elapsed time: 6.400674 msecs"
"Elapsed time: 6.374463 msecs"
"Elapsed time: 6.266154 msecs"
java-like2
"Elapsed time: 46.15 msecs"
"Elapsed time: 11.840942 msecs"
"Elapsed time: 10.842774 msecs"
"Elapsed time: 5.56714 msecs"
"Elapsed time: 5.655566 msecs"
"Elapsed time: 5.611824 msecs"
"Elapsed time: 5.509589 msecs"
java-like3
"Elapsed time: 37.785107 msecs"
"Elapsed time: 8.711968 msecs"
"Elapsed time: 8.377959 msecs"
"Elapsed time: 7.094104 msecs"
"Elapsed time: 3.637105 msecs"
"Elapsed time: 3.757499 msecs"
"Elapsed time: 3.589582 msecs"

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


Re: Ridiculously massive slowdown when working with images

2011-01-31 Thread Bill James


On Jan 31, 5:46 am, B Smith-Mannschott  wrote:

> > Yes, that speeds it up considerably.  If I can get Java server
> > installed
> > on my machine, I'll post some timings.
>
> What is this "java server" of which you speak.  In JVMs I'm familiar
> with (Sun/Oracle, OpenJDK) it's just a matter of passing the option
> -server to java when starting it.
>
> // Ben

Usually Windoze doesn't have the Java server executable.
You have to download the Java JDK.

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


Re: Ridiculously massive slowdown when working with images

2011-01-30 Thread Bill James
Benny Tsai wrote:
> Nice!  That version runs in 6 milliseconds on my machine, fastest of
> all the code posted so far.  One more idea: use 'unchecked-inc'
> instead of 'unchecked-add'.  This takes it under 3 milliseconds in my
> tests.
>
> (defn java-like [^bytes cpu_array]
>   (let [buffer-size (int buffer-size)]
> (loop [i (int 0)]
>   (if (< i buffer-size)
> (let [i2 (unchecked-inc i)
> i3 (unchecked-inc i2)
> i4 (unchecked-inc i3)
> a (aget cpu_array i4)]
>   (amove cpu_array i3 i4)
>   (amove cpu_array i2 i3)
>   (amove cpu_array i i2)
>   (aset cpu_array i a)
>   (recur (unchecked-inc i4)))
>

Yes, that speeds it up considerably.  If I can get Java server
installed
on my machine, I'll post some timings.

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


Re: How to disallow unlisted optional argument keys?

2011-01-30 Thread Bill James
Alexander Yakushev wrote:
> Why not use a constraint? It looks much cleaner.
>
> (defn hello [& {:keys [a b] :as input}]
>  {:pre [(= (set (keys input)) #{:a :b})]}
>  "hello")
>
> You can learn more about constraints here: 
> http://vimeo.com/channels/fulldisclojure#8399758


{:pre [(clojure.set/subset? (set (keys input)) #{:a :b})]}

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


Re: Ridiculously massive slowdown when working with images

2011-01-30 Thread Bill James
GrumpyLittleTed wrote:
> Part of the difference (under 1.2) is due to the (substantial)
> overhead of accessing the buffer-size var on every iteration.
>
> I ran a quick check and using David's version of the code result
> averaged 17.2ms. Just changing buffer-size to a local with using (let
> [buffer-size (int 192)]...) the time dropped to an average 3.4ms.
>

Great!  Now I remember that that same technique is used in Lua.
See long this version takes.

(set! *warn-on-reflection* true)

(def buffer-size 192)
(def array (byte-array buffer-size))

(defmacro add [m n] `(unchecked-add (int ~m) (int ~n)))
(defmacro amove[a i j] `(aset ~a ~j (aget ~a ~i)))

(defn java-like [^bytes cpu_array]
  (let [buffer-size (int buffer-size)]
(loop [i (int 0)]
  (if (< i buffer-size)
(let [ i2 (add i 1)
   i3 (add i 2)
   i4 (add i 3)
   a (aget cpu_array i4)
 ]
  (amove cpu_array i3 i4)
  (amove cpu_array i2 i3)
  (amove cpu_array i i2)
  (aset cpu_array i a)
  (recur (add i 4)))


(dotimes [_ 10]
  (time
   (dotimes [_ 1]
 (java-like array

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


Re: Ridiculously massive slowdown when working with images

2011-01-30 Thread Bill James
David Nolen wrote:
> (ns atest)
>
> (set! *warn-on-reflection* true)
>
> (def buffer-size 192)
> (def array (byte-array buffer-size))
> (defn java-like [^bytes cpuArray]
>   (loop [i (int 0)]
> (if (< i (int buffer-size))
>   (let [b (byte (aget cpuArray i))
> g (byte (aget cpuArray (unchecked-add i (int 1
> r (byte (aget cpuArray (unchecked-add i (int 2
> a (byte (aget cpuArray (unchecked-add i (int 3]
> (aset cpuArray i a)
> (aset cpuArray (unchecked-add i (int 1)) b)
> (aset cpuArray (unchecked-add i (int 2)) g)
> (aset cpuArray (unchecked-add i (int 3)) r)
> (recur (unchecked-add i (int 4)))
>
> (dotimes [_ 10]
>   (time
>(dotimes [_ 1]
>  (java-like array
>
> On my machine this takes 9ms.
>
> As a comparison, the following accomplishes the same thing in 1.3.0.
>
> (ns test)
>
> (set! *unchecked-math* true)
> (set! *warn-on-reflection* true)
>
> (def buffer-size 192)
> (def array (byte-array buffer-size))
> (defn java-like [^bytes cpuArray]
>  (loop [i (int 0)]
>(if (< i buffer-size)
>  (let [b (aget cpuArray i)
>g (aget cpuArray (+ i 1))
>r (aget cpuArray (+ i 2))
>a (aget cpuArray (+ i 3))]
>(aset cpuArray i a)
>(aset cpuArray (+ i 1) b)
>(aset cpuArray (+ i 2) g)
>(aset cpuArray (+ i 3) r)
>(recur (+ i 4))
>
> (dotimes [_ 10]
>   (time
>(dotimes [_ 1]
>  (java-like array

Another slight speed-up:

(set! *warn-on-reflection* true)

(def buffer-size 192)
(def array (byte-array buffer-size))

(defmacro add [m n] `(unchecked-add (int ~m) (int ~n)))
(defmacro amove[a i j] `(aset ~a ~j (aget ~a ~i)))

(defn java-like [^bytes cpu_array]
  (loop [i (int 0)]
(if (< i (int buffer-size))
  (let [ i2 (add i 1)
 i3 (add i 2)
 i4 (add i 3)
 a (aget cpu_array i4)
   ]
(amove cpu_array i3 i4)
(amove cpu_array i2 i3)
(amove cpu_array i i2)
(aset cpu_array i a)
(recur (add i 4))


(dotimes [_ 10]
  (time
   (dotimes [_ 1]
 (java-like array

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


Re: Ridiculously massive slowdown when working with images

2011-01-30 Thread Bill James


On Jan 28, 12:55 pm, David Nolen  wrote:
> (ns atest)
>
> (set! *warn-on-reflection* true)
>
> (def buffer-size 192)
> (def array (byte-array buffer-size))
> (defn java-like [^bytes cpuArray]
>   (loop [i (int 0)]
>     (if (< i (int buffer-size))
>       (let [b (byte (aget cpuArray i))
>             g (byte (aget cpuArray (unchecked-add i (int 1
>             r (byte (aget cpuArray (unchecked-add i (int 2
>             a (byte (aget cpuArray (unchecked-add i (int 3]
>         (aset cpuArray i a)
>         (aset cpuArray (unchecked-add i (int 1)) b)
>         (aset cpuArray (unchecked-add i (int 2)) g)
>         (aset cpuArray (unchecked-add i (int 3)) r)
>         (recur (unchecked-add i (int 4)))
>
> (dotimes [_ 10]
>   (time
>    (dotimes [_ 1]
>      (java-like array
>
> On my machine this takes 9ms.
>
> As a comparison, the following accomplishes the same thing in 1.3.0.
>
> (ns test)
>
> (set! *unchecked-math* true)
> (set! *warn-on-reflection* true)
>
> (def buffer-size 192)
> (def array (byte-array buffer-size))
> (defn java-like [^bytes cpuArray]
>  (loop [i (int 0)]
>    (if (< i buffer-size)
>      (let [b (aget cpuArray i)
>            g (aget cpuArray (+ i 1))
>            r (aget cpuArray (+ i 2))
>            a (aget cpuArray (+ i 3))]
>        (aset cpuArray i a)
>        (aset cpuArray (+ i 1) b)
>        (aset cpuArray (+ i 2) g)
>        (aset cpuArray (+ i 3) r)
>        (recur (+ i 4))
>
> (dotimes [_ 10]
>   (time
>    (dotimes [_ 1]
>      (java-like array



Faster than my previous post:

(set! *warn-on-reflection* true)

(def buffer-size 192)
(def array (byte-array buffer-size))

(defmacro add [m n] (list `unchecked-add `(int ~m) `(int ~n)))

(defn java-like [^bytes cpu_array]
  (loop [i (int 0)]
(if (< i (int buffer-size))
  (let [ i2 (add i 1)
 i3 (add i 2)
 i4 (add i 3)
 b (aget cpu_array i)
 g (aget cpu_array i2)
 r (aget cpu_array i3)
 a (aget cpu_array i4)
   ]
(aset cpu_array i a)
(aset cpu_array i2 b)
(aset cpu_array i3 g)
(aset cpu_array i4 r)
(recur (add i 4))


(dotimes [_ 10]
  (time
   (dotimes [_ 1]
 (java-like array

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


Re: Ridiculously massive slowdown when working with images

2011-01-30 Thread Bill James


On Jan 28, 12:55 pm, David Nolen  wrote:
> (ns atest)
>
> (set! *warn-on-reflection* true)
>
> (def buffer-size 192)
> (def array (byte-array buffer-size))
> (defn java-like [^bytes cpuArray]
>   (loop [i (int 0)]
>     (if (< i (int buffer-size))
>       (let [b (byte (aget cpuArray i))
>             g (byte (aget cpuArray (unchecked-add i (int 1
>             r (byte (aget cpuArray (unchecked-add i (int 2
>             a (byte (aget cpuArray (unchecked-add i (int 3]
>         (aset cpuArray i a)
>         (aset cpuArray (unchecked-add i (int 1)) b)
>         (aset cpuArray (unchecked-add i (int 2)) g)
>         (aset cpuArray (unchecked-add i (int 3)) r)
>         (recur (unchecked-add i (int 4)))
>
> (dotimes [_ 10]
>   (time
>    (dotimes [_ 1]
>      (java-like array
>

On my laptop, removing the "byte" hint speeds it up.
(I don't have the server version of Java.)


(set! *warn-on-reflection* true)

(def buffer-size 192)
(def array (byte-array buffer-size))

(defmacro add [m n] (list 'unchecked-add `(int ~m) `(int ~n)))

(defn java-like [^bytes cpu_array]
  (loop [i (int 0)]
(if (< i (int buffer-size))
  (let [b (aget cpu_array i)
g (aget cpu_array (add i 1))
r (aget cpu_array (add i 2))
a (aget cpu_array (add i 3))]
(aset cpu_array i a)
(aset cpu_array (add i 1) b)
(aset cpu_array (add i 2) g)
(aset cpu_array (add i 3) r)
(recur (int (add i 4)))


(dotimes [_ 10]
  (time
   (dotimes [_ 1]
 (java-like array

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