Some would also argue that any parallelism system is going to slow down
code as trivial as this. Never underestimate the power of properly
optimized single threaded code :-).

But yes, futures are the way to go if you want to execute a given
expression on a different thread.


On Thu, Apr 10, 2014 at 8:45 AM, Andy Fingerhut <andy.finger...@gmail.com>wrote:

> Forcing small bits of computation to be done in parallel using the tools
> Clojure and the JVM have at hand, e.g. pmap, future, etc., which rely on
> creating JVM Thread objects, tends to slow things down rather than speed
> things up, because the extra overhead of creating threads and waiting for
> them to finish is higher than the cost of sequentially doing the small bits
> of computation.
>
> If you want to force parallelism in your example anyway, to compare the
> most-likely-slower-performance against what you have now, you can try this:
>
> (let [val (Math/sqrt i)
>        a1 (future (Math/pow val 2))
>        a2 (future (* val val))
>        diff (Math/abs (- @a1 @a2))]
>
> Andy
>
>
> On Thu, Apr 10, 2014 at 7:35 AM, Cecil Westerhof 
> <cldwester...@gmail.com>wrote:
>
>> I have the following in my code:
>>        (let [val (Math/sqrt i)
>>             diff (Math/abs (- (Math/pow val 2) (* val val)))]
>>
>> I expect that calculating the difference is the most expensive part of my
>> code. I understood that Clojure is very good in parallellisation. How would
>> I let (Math/pow val 2) and (* val val) be calculated in parallel?
>>
>> --
>> Cecil Westerhof
>>
>> --
>> 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
>> ---
>> 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 clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> 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
> ---
> 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 clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
"One of the main causes of the fall of the Roman Empire was that-lacking
zero-they had no way to indicate successful termination of their C
programs."
(Robert Firth)

-- 
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
--- 
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to