Skip to main content
4 of 6
edited tags
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Functional programming Style & Performance

Hello i have been spending time to convert my R tools into Mathematica Packages mainly because i like the functional programming style in Mathematica. In doing so it seems that i run into one conundrum. On one had functional programming suggests to avoid intermediate variables on the other side, a simple example like below clearly shows the potentially significant performance difference when avoiding intermediate (pre-calculated) steps.

do i miss any better solution, or is this really a case where functional style, just looses out ?

  • dfm is simply a List of Lists with some syntactic sugar
  • dfm["SER"] is a vector of 25k real numbers
  • dfm["serial"] is a vector of 25k string factors

(* check out the add variation as function of model *)
uSerial  = DeleteDuplicates[dfm["serial"]];
tt       = Transpose@{dfm["SER"], dfm["serial"]};
foo[z_] := Select[tt, (Last[#] == z) &][[All, 1]];
serMhdd = Mean /@ Map[foo[#] &, dfm["serial"]] // N; // AbsoluteTiming

uSerial = DeleteDuplicates[dfm["serial"]];
foo[z_] := Select[Transpose@{dfm["detSER"], 
                dfm["serial"]}, (Last[#] == z) &][[All, 1]];
serMhdd = Mean /@ Map[foo[#] &, dfm["serial"]] // N; // AbsoluteTiming

happy holidays, Bernd

bernddude
  • 161
  • 1
  • 4