Question: Why is map slower than `~`?

For a list containing at least one element, 

F~(list0); # element-wise

is (almost) equivalent to 

map(F, list0);

However, it seems that in practice, using `~` can be faster than its equivalent map version.
Here is a typical test: 

 

 

restart;

w := [`$`](0 .. 1e4):
x := [`$`](0 .. 2e3):
y := [`$`](0 .. 3e2):
z := [`$`](0 .. 4e1):

undefine(f)

NULL

 

 

# Compute a generalized outer product of [w, x] and [y, z] without using the inefficient `ArrayTools:-GeneralOuterProduct`.

""(* Note that we cannot use the ""unapply"" command here. (Use ""->"" instead.) *)""

p2 := CodeTools[Usage](`~`[proc (s4) options operator, arrow; `~`[proc (s3) options operator, arrow; `~`[proc (s2) options operator, arrow; `~`[proc (s1) options operator, arrow; f(s3, s1) end proc](s2) end proc]([y, z]) end proc](s4) end proc]([w, x]), iterations = 10)

memory used=282.53MiB, alloc change=0.59GiB, cpu time=21.67s, real time=7.41s, gc time=17.33s

p4 := CodeTools[Usage](map(proc (s4) options operator, arrow; map(proc (s3) options operator, arrow; map(proc (s2) options operator, arrow; map(proc (s1) options operator, arrow; f(s3, s1) end proc, s2) end proc, [y, z]) end proc, s4) end proc, [w, x]), iterations = 10)

memory used=230.48MiB, alloc change=-4.00MiB, cpu time=23.26s, real time=8.67s, gc time=17.71s

evalb(p2 = p4);

true

NULL


 

Download `~`_and_map.mw

But I cannot find any explanations in Comparison of element-wise operators and map function or Difference between 'map' and '~' (element-wise operation). Does anyone know why?

Please Wait...