Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They solve different problems . Actors are good at solving <a href="http://en.wikipedia.org/wiki/Task_parallelism">task parallel problems</a>. While parallel collections are good at solving <a href="http://en.wikipedia.org/wiki/Data_parallelism">data parallel problems</a>. I don't think they are mutually exclusive - you can use parallel collections in actors and parallel collections containing actors.</p> <hr> <p>Edit - quick test: Even something simple like a actor notification loop benefits.</p> <p>In the following code we register a million actors with an actor registry which has to notify them of an event. </p> <p>The non-parallel notification loop ( <code>registry foreach {}</code> ) takes an average of 2.8 seconds on my machine (4 core 2.5 GHz notebook). When the parallel collection loop ( <code>registry.par.foreach {}</code> ) is used it takes 1.2 seconds and uses all four cores.</p> <pre><code>import actors.Actor case class Register(actor: Actor) case class Unregister(actor: Actor) case class Message( contents: String ) object ActorRegistry extends Actor{ var registry: Set[Actor] = Set.empty def act() { loop{ react{ case reg: Register =&gt; register( reg.actor ) case unreg: Unregister =&gt; unregister( unreg.actor ) case message: Message =&gt; fire( message ) } } } def register(reg: Actor) { registry += reg } def unregister(unreg: Actor) { registry -= unreg } def fire(msg: Message){ val starttime = System.currentTimeMillis() registry.par.foreach { client =&gt; client ! msg } //swap registry foreach for single th val endtime = System.currentTimeMillis() println("elapsed: " + (endtime - starttime) + " ms") } } class Client(id: Long) extends Actor{ var lastmsg = "" def act() { loop{ react{ case msg: Message =&gt; got(msg.contents) } } } def got(msg: String) { lastmsg = msg } } object Main extends App { ActorRegistry.start for (i &lt;- 1 to 1000000) { var client = new Client(i) client.start ActorRegistry ! Register( client ) } ActorRegistry ! Message("One") Thread.sleep(6000) ActorRegistry ! Message("Two") Thread.sleep(6000) ActorRegistry ! Message("Three") } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload