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

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -