Performance of for-comprehension in scala -



Performance of for-comprehension in scala -

i've question efficiency of for-comprehensions in scala.

this next code takes around 45 sec run when perm list of around 550 elements

perm = list for{ perm <- perms.withfilter(_.size > 0) wordlist = somefunction(perm) //expensive operation, wordlist list of strings sentencelist = somefunction1(perm) //very expensive operation, sentencelist list of list of strings word <- wordlist sentence <- sentencelist } yield { word::sentence}

when changed next code following, ran in 3 sec same perm list

perm = list for{ perm <- perms.withfilter(_.size > 0) word <- somefunction(perm) //expensive operation sentence <- somefunction1(perm) //very expensive operation } yield { word::sentence}

does difference in performance has lazy evaluation in scala?

let's desugar both for-comprehensions:

1.)

perms.withfilter(_.size > 0).flatmap { perm => val wordlist = somefunction(perm) //expensive operation val sentencelist = somefunction1(perm) //very expensive operation wordlist.flatmap { word => sentencelist.map { sentence => word::sentence } } }

2.)

perms.withfilter(_.size > 0).flatmap { perm => somefunction(perm).flatmap { word => somefunction1(perm).map { sentence => word :: sentence } } }

in first case, both expensive functions executed every time. in sec case, when somefunction(perm) returns empty result, somefunction1(perm) never executed.

performance scala for-comprehension

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -