Here's my second attempt. I fixed one obvious bug but it was the
addition of the type "#^ByteBuffer" that made a *huge* difference.
The times now look something like:
;;;; Compile file /home/aim/src/clojure/foo.clj ...
"Elapsed time: 64.273962 msecs"
"Elapsed time: 59.607317 msecs"
"Elapsed time: 61.639253 msecs"
"Elapsed time: 61.495916 msecs"
"Elapsed time: 61.805888 msecs"
(import '(java.io File))
(import '(java.nio ByteBuffer))
(import '(java.nio MappedByteBuffer))
(import '(clojure.contrib))
(import '(clojure.contrib.mmap))
(require 'clojure.contrib.mmap)
(def my-file (File. "/home/aim/largelog.bin"))
(def my-buff (mmap my-file))
(defn my-iterate-map [f #^ByteBuffer buf]
(let [max (long (.length f))]
(.position buf 0)
(loop [i (long 1)]
;;(println i)
(if (< i (long max))
(do
(.get buf)
(recur (unchecked-inc i))))))
buf)
(dotimes [_ 5] (time (my-iterate-map my-file my-buff)))
On Nov 19, 1:14 pm, aim <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I was experimenting with using mmap from clojure but I see vastly
> different timings when compared to plain old Java and
> MappedByteBuffer.
>
> Here's my code and also represents the largest bit of clojure I have
> written:
>
> (import '(java.io File))
>
> (use 'clojure.contrib.mmap)
>
> (def my-file (File. "/home/aim/largelog.bin"))
>
> (defn my-iterate-map [buf]
> (let [max (.remaining buf)]
> (loop [i (long 1)]
> (if (< i max)
> (do (.get buf)
> (recur (unchecked-inc i))))))
> buf)
>
> (time (my-iterate-map (mmap my-file)))
>
> The results I see from clojure are:
>
> ;;;; (time (my-iterate-map (mmap my-file))) ...
> "Elapsed time: 113191.133057 msecs"
>
> But If I do the same thing in Java I get:
>
> gone in 0.0540 seconds!
>
> Is there something I can do to make the clojure version near or
> equivalent to the java code below?
>
> Thanks,
> Andy.
>
> --- Java code --
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.nio.ByteBuffer;
> import java.nio.channels.FileChannel;
>
> public class Main {
> public static void main(String[] args) throws Exception {
> File f = new File(args[0]);
> FileInputStream fis = new FileInputStream(f);
> ByteBuffer buf =
> fis.getChannel().map(FileChannel.MapMode.READ_ONLY,
> 0, f.length());
> long then = System.currentTimeMillis();
> long sum = 0;
> while (buf.hasRemaining()) {
> sum += (buf.get() & 0xFF);
> }
> long now = System.currentTimeMillis();
> System.out.printf("gone in %.4f seconds!\n", (((now - then) /
> 1000.0)));
> }
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---