Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object SynteticTest {
- implicit val ec: ExecutionContext = ExecutionContext.global
- def createFuture(dummy: Int): Int => Future[Int] = x => Future.successful(x + dummy)
- def testWithFutures(futures: Seq[Int => Future[Int]]): Future[Int] ={
- futures.foldLeft(Future.successful(0)) {
- (result, op) => {
- result.flatMap{ rs =>
- op(rs)
- }
- }
- }
- }
- def testPlain(): Unit = {
- def simpleClojure(arg: Int): Int => Int = x => x + arg
- val listoffuns: Seq[Int => Int] = (1 to 20000).map(simpleClojure)
- listoffuns.foldLeft(0){ (o1, o2) =>
- o1 + o2.apply(o1)
- }
- }
- def main(args: Array[String]): Unit = {
- val ts = System.currentTimeMillis()
- testWithFutures(Seq.fill(20000)(1).map(createFuture)).andThen {
- case Success(rs) => println(s"Async took = ${System.currentTimeMillis() - ts}")
- }
- val ts2 = System.currentTimeMillis()
- testPlain()
- println(s"Sync took ${System.currentTimeMillis() - ts2}")
- Thread.sleep(10000)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement