Re: map vs. pmap

2011-08-28 Thread artg
Thanks. I looked at Java doc for random which says
This method is properly synchronized to allow correct use by more
than one thread. However, if many threads need to generate
pseudorandom numbers at a great rate, it may reduce contention for
each thread to have its own pseudorandom-number generator.

art


Returns:


On Aug 27, 9:22 pm, Lee Spector lspec...@hampshire.edu wrote:
 On Aug 27, 2011, at 10:41 PM, Andy Fingerhut wrote:

  I suspect that (rand), which calls java.lang.Math.random(), is a 
  synchronized method, meaning that if you try to call it from many parallel 
  threads, even those on physically separate cores, will execute one at a 
  time.  Things could even take more time if you try to execute them in 
  parallel than sequentially, due to lock contention overhead that does not 
  occur if you try to execute them sequentially.

 http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#...

  Try doing something besides (rand) in your function t that does not use 
  locking or synchronized methods.  You might also experiment with having no 
  Thread/sleep call in there, but that might be part of what you are trying 
  to learn the behavior of.  I'm not sure.

 Or if you really want to be getting random numbers in all of those threads 
 you could arrange for all of them to use thread-local Random objects -- 
 created with something like (new java.util.Random) -- and then get your 
 random numbers with something like (. thread-local-random-generator 
 (nextFloat)). I did something similar in my project 
 athttps://github.com/lspector/Clojushalthough there I use agents rather than 
 pmap to initiate the concurrent processes.

  -Lee

-- 
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: map vs. pmap

2011-08-28 Thread artg
fixed it

(defn t [m]
  (let [r (java.util.Random.)]
(dotimes [i m] (. r nextDouble)))
  (Thread/sleep 1000))

(defn testmap [f n m]
  (time (doall (f t (repeat n m)

user= (testmap map 8 200)
Elapsed time: 8869.942784 msecs
(nil nil nil nil nil nil nil nil)
user= (testmap pmap 8 200)
Elapsed time: 1178.490827 msecs
(nil nil nil nil nil nil nil nil)

thanks
art

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


map vs. pmap

2011-08-27 Thread artg
I understand that pmap needs to have big chunks to overcome the
overhead but I don't understand my results

(defn t [m]
  (dotimes [i m] (rand))
  (Thread/sleep 1000))

(defn testmap [f n m]
  (time (doall (f t (repeat n m)

user= (testmap map 8 100)
Elapsed time: 8108.174484 msecs
(nil nil nil nil nil nil nil nil)
user= (testmap pmap 8 100)
Elapsed time: 1006.797146 msecs
(nil nil nil nil nil nil nil nil)
user= (testmap pmap 8 100)
Elapsed time: 3781.967909 msecs
(nil nil nil nil nil nil nil nil)
user= (testmap map 8 100)
Elapsed time: 8544.896008 msecs
(nil nil nil nil nil nil nil nil)
user= (testmap map 8 1000)
Elapsed time: 10663.057874 msecs
(nil nil nil nil nil nil nil nil)
user= (testmap pmap 8 1000)
Elapsed time: 29525.579612 msecs
(nil nil nil nil nil nil nil nil)

I'm using an i7 with 8 processors so the Thread alone mostly gives an
8-fold speedup. Calling (rand) more times impacts pmap even though I
map and pmap on a list of size 8 to spawn eight functions each with a
lot of work.

-- 
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: Clojure Speed performance test

2011-08-22 Thread artg
Below is a Clojure solution that runs in 8.58 microseconds on my
machine. I only did it for the 40, 3 case. Records are linked. The
list reduction solution runs in 23.4 and the element recursion in 27.3

--art

(defrecord Soldier [val r])
(def v (vec (map #(Soldier. % (inc %)) (range 40
(def v2 (assoc v 39 (Soldier. 39 0)))
(defn move  [v i]
(let [r1 (v i)  r2 (:r (v (:r r1)))]
   [(:r (v r2))  (assoc v  (:r r1) (Soldier. (:val r1) (:r (v
r2]
))
(defn joe [v]
   (loop [v v i 0 count 39]
  (if  (zero? count)
  (inc i)
  (let [[a b] (move v i)]
 (recur b a (dec count))

(prn (joe v2))

(let [start (System/currentTimeMillis) iterations 10]
(doseq [i (range 10)] (joe v2))
(let [end (System/currentTimeMillis)]
(println (/ (* (- end start) 1000.00) iterations) 
microseconds)))


 On Aug 18, 12:40 am, Roberto Mannai roberm...@gmail.com wrote:
 Hello,
 I recently stumbled upon this 
 page:http://java.dzone.com/articles/contrasting-performance
 They are comparing several languages (Java, Scala, Python, Erlang,
 Clojure, Ruby, Groovy, Javascript), and Clojure rated very badly:

                          Object Oriented      List Reduction
 Element Recursion
 Java 1.6                             0.637            1.435                   
 2.816
 Clojure 1.2.1                     -               25.966                 
 28.753

 Maybe the clojure's scripts were not very idiomatic? If some guru
 wants to check them, here's the source code:
  -https://github.com/dnene/josephus/blob/master/element-recursion/josep...
 -  https://github.com/dnene/josephus/blob/master/list-reduction/josephus...

 All the best,
 Roberto

-- 
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: college courses

2009-10-25 Thread artg

I'm going to start the Clojure unit in the senior class in a few
weeks. They have done some F# and I will repeat the F# assignments in
Clojure for comparison. They all have some Java and some C++ but the
unit here is just some functional programming without Java interop so
I don't think it matters. The grad class has been going well and I get
some interesting programs. Here Java is handy for GUI so it does help
to have some Java.

On Oct 22, 1:57 am, Daniel Werner daniel.d.wer...@googlemail.com
wrote:
 On Oct 19, 5:03 pm, artg artgittle...@gmail.com wrote:

  I'm using Programming Clojure in a grad course and doing a short
  Clojure unit in a senior programming languages course at Calif State
  Univ Long Beach.

 Art, what kind of pre-existing knowledge do you expect or require in
 your students? In your experience, do they need a background in Java
 to use Clojure effectively for real-world tasks?

 And how fast do they absorb Clojure's FP style, provided they haven't
 learned a functional language before?
--~--~-~--~~~---~--~~
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: college courses

2009-10-19 Thread artg


I'm using Programming Clojure in a grad course and doing a short
Clojure unit in a senior programming languages course at Calif State
Univ Long Beach.

On Oct 19, 2:19 am, Andreas Wenger andi.xeno...@googlemail.com
wrote:
 At the Technische Universität München (Germany), I know of two courses
 where Clojure was at least mentioned.

 This year there was an advanced Java seminar with one talk about
 Clojure (http://www2.in.tum.de/hp/Main?nid=59).
 Next year there is a programming models and code generation seminar
 with a talk about the concurrency features of Clojure 
 (http://www.lrr.in.tum.de/public/HauptseminarCodegenerierungWS09- not
 mentioned yet).

 But I know of no lecture that uses Clojure as its main language (yet).
 In the second semester, our students are teached OCaml or a self-
 constructed functional-like Java when they learn functional
 programming.
--~--~-~--~~~---~--~~
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: Uncle Bob: bowling meets Clojure

2009-07-21 Thread artg

What is group-frames?

--art

On Jul 21, 12:00 am, Mark Triggs mark.h.tri...@gmail.com wrote:
 Hi Stu,

 Stuart Halloway stuart.hallo...@gmail.com writes:
  Uncle Bob Martin, a very well-respected OO and agile guy, is learning  
  Clojure. He has posted an example [1] and asked for feedback from the  
  Clojure community. I have made my suggestions in code [2] and will be  
  writing them up shortly.

  Would love to see what other folks on this list have to say.

 My first version came out rather similar to yours, and then I started
 thinking about turning the problem on its head and making the concept of
 types of rolls more explicit.  I'm still not sure how I feel about
 this, but the line of thinking led me to code like this:

   (ns bowling-game
     (:use clojure.contrib.seq-utils))

   (def *roll-types*
        [{:name strike
          :satisfies #(= (first %) 10)
          :consumes 3
          :advances 1}

         {:name spare
          :satisfies #(= (apply + (take 2 %))
                         10)
          :consumes 3
          :advances 2}

         {:name regular (underachieving?)
          :satisfies (constantly true)
          :consumes 2
          :advances 2}])

   (defn roll-type [rolls]
     (find-first #((:satisfies %) rolls)
                 *roll-types*))

   (defn frames [rolls]
     (when (seq rolls)
       (let [{:keys [consumes advances]} (roll-type rolls)]
         (cons (take consumes rolls)
               (frames (drop advances rolls))

   (defn score-game [rolls]
     (reduce + (map #(reduce + %)
                    (take 10 (group-frames rolls)

 Cheers,

 Mark

 --
 Mark Triggs
 mark.h.tri...@gmail.com
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



timing for map

2009-06-07 Thread artg

user= (time (map answer '(50 50)))
Elapsed time: 0.032826 msecs
(392330 392711)
user= (time (answer 50))
Elapsed time: 6357.131423 msecs
392849

When I try to time 'map' it seems to return right away.  How do I time
the full two executions of 'answer'?

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