Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Created July 31, 2011 02:47
Show Gist options
  • Save fearofcode/1116319 to your computer and use it in GitHub Desktop.
Save fearofcode/1116319 to your computer and use it in GitHub Desktop.
Simple demonstration of speedup achievable with new parallel collection in Scala 2.9
package com.ign
import scala.collection.mutable.ArrayBuffer
object Main extends App {
var wordList = ArrayBuffer[String]()
val size = 10000
for(i <- 1 to size)
wordList += ("foo" + i)
def processWord(x: String) = {
// pretend this is a computationally intensive task
// (simulated here by a 1ms sleep, giving each call a run time on the order of 1ms)
Thread.sleep(1)
x.length
}
def seqProcess() = {
var sum = 0
for(i <- wordList) {
sum += processWord(i)
}
sum
}
def foldSeqProcess() = wordList.foldLeft(0)(_ + processWord(_))
def mapSeqProcess() = wordList.map(processWord).sum
def parProcess() = wordList.par.map(processWord).sum
def timeProcessing(processor: (() => Int), name: String) {
val start = System.currentTimeMillis()
val lengthSum = processor()
val elapsed = System.currentTimeMillis()-start
System.out.println(name + ": " + lengthSum + ", " + elapsed + " ms elapsed")
}
timeProcessing(seqProcess, "Sequential")
timeProcessing(foldSeqProcess, "Sequential (mapping)")
timeProcessing(mapSeqProcess, "Sequential (folding)")
timeProcessing(parProcess, "Parallel")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment