Sunday, July 13, 2014
Now using this to simplify conditions : So everything is working fine, and "oups" was not printed on the console.
But one of the best usage I found of this pattern, is when we want to get some information that may be available from several sources : This time we have a piece of code that is very easy to read !
Try to do the same job using only traditional "if-then-else" blocks, or pattern matching.
Traditional approach examples : Using if block suffer from a bug, the first fromX function returning a non empty result will be evaluated twice (we should have use a temporary value to keep the result of from evaluation). The pattern matching variant is very difficult to read.
Sunday, April 27, 2014
Sunday, March 2, 2014
Actors programming is an interesting solution to take benefits of all your server CPU core to compute primes number while still being able to compute their order positions.
Here the principle is to create an actor router, which will load balance primes computation, CheckerActors. The load balancing is based on the size of each load balanced actors, a new task will be given to the actor with the lowest mailbox size.
All checked values are then sent to a single actor which will reorder received values and attribute them their respective positions. This actor, the ValuesManagerActor, is also responsible of managing the load, and check that final destination of all results can manage the result flow.
Here all results are sent to PrinterActor, this actor output results on the standard ouput, if you try here to print all results this actor may not as fast as the received result flow, so its mailbox will grow, up to the jvm maximum heap size thus generating OutOfMemory exception. That's why here I've added an acknowledge mechanism that verify we do not have too many results sent and not taken into account. If we have too many results not taken into account then ValuesManagerActor suspend the load balancing to CheckerActors.
My primes project at github for experiments and tests.
How long time to get the 500000th prime (sbt console) (first results, jvm 220.127.116.11, Intel Core i7 2.3 Ghz, 4 CPUs) :
|Actor (ack on each response)||38s||with fork-join-executor|
|Actor (ack every 5000 responses)||35s||with fork-join-executor|
|Actor (ack on each response)||33s||with thread-pool-executor|
|Actor (ack every 5000 responses)||30s||with thread-pool-executor|
Actors based algorithm is quite faster than a classic sequential one, but not as fast as using a parallel collection in order to parallelize the classic algorithm.
Although my implementation is not as fast as the parallel solution (I'll have to investigate that point), it has a major benefits, it can be distributed across several computers. It'll be the subject of a new article.
Saturday, February 1, 2014
An example of how to implement a generic primes generator that you can use with any numerical type. Of course, performances will be impacted by your choice :
- howlongfor(intPrimes.drop(25000).head) // res0: (String, Int) = (459ms,287137)
- howlongfor(longPrimes.drop(25000).head) // res1: (String, Long) = (1349ms,287137)
- howlongfor(bigIntPrimes.drop(25000).head) // res2: (String, BigInt) = (4361ms,287137)
Simplified source code
Sunday, November 24, 2013
Situations when you must not forget to use the scala blocking statement when working with futures...
Tuesday, October 1, 2013
$ sbt "run 500"It will generate a PNG image file named "ulam-spiral-500.png"
$ sbt console" scala> sexyPrimeStream(4) res3: primes.Primes.PInteger = 17 scala> primeStream.take(15).toList res3: List[primes.Primes.PInteger] = List(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47) scala> sexyPrimeStream.take(5).toList res5: List[primes.Primes.PInteger] = List(5, 7, 11, 13, 17) scala> isolatedPrimeStream.take(10).toList res2: List[primes.Primes.PInteger] = List(23, 37, 47, 53, 67, 79, 83, 89, 97, 113)