scala - Performance comparison of strict collection vs view -


im doing performance comparison between executing multiple subsequent transformations on collections in scala strict (eagerly performed evaluation), , non-strict (lazily performed evaluation).

def time[r](block: => r): r = {     val t0 = system.nanotime()     val result = block // call-by-name     val t1 = system.nanotime()     println("elapsed time: " + (t1 - t0)/1e9 + "s")     result }  /* view on collection makes transformations lazy, makes    possible combine multiple transformations one. */  // non-lazy (eager) version: time {     (1 1e7.toint).map(_ + 2).map(x => {         if(x % 2 != 0) -x else x     }).sum }  // lazy version using view: time {     (1 1e7.toint).view.map(_ + 2).map(x => {         if(x % 2 != 0) -x else x     }).force.sum } 

on laptop, first run of eager version slower lazy version. see timings below.

eager version: 2.4 s

lazy verion: 0.7 s

however, starting second run, both of them take 0.7 seconds. why?

runtime environment:

  • scala 2.11.7
  • java 1.8
  • os x 10.10

skip cold run

when profiling code, first run/iteration called cold run not measured incur initial costs os and/or runtime environment.

in case of java , scala code executed on jvm, first execution of instructions may require loading , parsing of classes , methods memory, creating overhead.

the jvm replace, e.g., math operations native instructions during first iteration, causing speedup of execution subsequent iterations.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -