Quantcast

map operation is very fast when coded with function literal but very slow for large matrices when coded as a function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

map operation is very fast when coded with function literal but very slow for large matrices when coded as a function

sterg
Hi all,


I run in ScalaLab this code:



var N=3000

var x = ones(N, N)

tic
var xx = x map ( _*1.23 + 0.13)

var tm=toc

it runs very fast, i.e. about 0.04 sec on iMac  with intel i5, 3.2 GHz



The same code with function def:

var N=3000

var x = ones(N, N)


def f(x: Double) =  x*1.23 + 0.13

tic
var xx = x map ( f )

var tm=toc


is very slow for large matrices, i.e. about 22.4 sec


If I run the code with function variable, i,.e,



var N=3000

var x = ones(N, N)


var f: Double=>Double =  x=>x*1.23 + 0.13

tic
var xx = x map ( f )

var tm=toc


the execution is very fast again.



Why the


def f(x: Double) =  x*1.23 + 0.13


results in slow execution for large matrices?


Also this version, runs well (i.e. is fast):


var N=3000

var x = ones(N, N)


def f(x: Double) =  x*1.23 + 0.13

tic
var xx = x map ( f(_))
var tm=toc



Why the version with:

var xx = x map f

runs much slower?



Best Regards


Stergios 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: map operation is very fast when coded with function literal but very slow for large matrices when coded as a function

Alec Zorab-2

What scala version are you using and what jvm were you testing on?


On Mon, 13 Feb 2017, 08:19 Stergios Papadimitriou, <[hidden email]> wrote:
Hi all,


I run in ScalaLab this code:



var N=3000

var x = ones(N, N)

tic
var xx = x map ( _*1.23 + 0.13)

var tm=toc

it runs very fast, i.e. about 0.04 sec on iMac  with intel i5, 3.2 GHz



The same code with function def:

var N=3000

var x = ones(N, N)


def f(x: Double) =  x*1.23 + 0.13

tic
var xx = x map ( f )

var tm=toc


is very slow for large matrices, i.e. about 22.4 sec


If I run the code with function variable, i,.e,



var N=3000

var x = ones(N, N)


var f: Double=>Double =  x=>x*1.23 + 0.13

tic
var xx = x map ( f )

var tm=toc


the execution is very fast again.



Why the


def f(x: Double) =  x*1.23 + 0.13


results in slow execution for large matrices?


Also this version, runs well (i.e. is fast):


var N=3000

var x = ones(N, N)


def f(x: Double) =  x*1.23 + 0.13

tic
var xx = x map ( f(_))
var tm=toc



Why the version with:

var xx = x map f

runs much slower?



Best Regards


Stergios 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: map operation is very fast when coded with function literal but very slow for large matrices when coded as a function

Oliver Ruebenacker
In reply to this post by sterg

     Hello,

  Very interesting. Would be nice to have something self-contained that showed the same behavior.

  The def declaration defines a method. Using a method as a function requires the compiler to wrap it in a function object (lifting), which may cost you an extra method call per invocation, and perhaps even a dynamic resolution, because the method may be overridden (though you could fix that by making it final). So, it may be expected to run a little bit slower - but not that much slower!

  The difference between the second and third example is truly puzzling - I would have thought map(f) and map(f(_)) would be perfectly equivalent. May be awesome if some one could decompile that to see how a difference could sneak in. Maybe an unexpected change in overload resolution? Perhaps a failure to specialize? Or maybe map is based on a macro that treats the two expressions differently?

     Best, Oliver

On Mon, Feb 13, 2017 at 3:19 AM, Stergios Papadimitriou <[hidden email]> wrote:
Hi all,


I run in ScalaLab this code:



var N=3000

var x = ones(N, N)

tic
var xx = x map ( _*1.23 + 0.13)

var tm=toc

it runs very fast, i.e. about 0.04 sec on iMac  with intel i5, 3.2 GHz



The same code with function def:

var N=3000

var x = ones(N, N)


def f(x: Double) =  x*1.23 + 0.13

tic
var xx = x map ( f )

var tm=toc


is very slow for large matrices, i.e. about 22.4 sec


If I run the code with function variable, i,.e,



var N=3000

var x = ones(N, N)


var f: Double=>Double =  x=>x*1.23 + 0.13

tic
var xx = x map ( f )

var tm=toc


the execution is very fast again.



Why the


def f(x: Double) =  x*1.23 + 0.13


results in slow execution for large matrices?


Also this version, runs well (i.e. is fast):


var N=3000

var x = ones(N, N)


def f(x: Double) =  x*1.23 + 0.13

tic
var xx = x map ( f(_))
var tm=toc



Why the version with:

var xx = x map f

runs much slower?



Best Regards


Stergios 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

map operation is very fast when coded with function literal but very slow for large matrices when coded as a function

Jasper-M
In reply to this post by sterg
Have you reproduced these results multiple times? Because the most obvious reason (to me) would be some external source of disturbance. Like memory of the computer or the JVM that is almost full. Causing a huge amount of swapping or garbage collecting.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...