Wolfram Language Speed Trivia
Here are some WolframLang speed trivia. They compare different ways to write code that does the same thing. All the cases on this page are trivial, in the sense that after million iterations, only a trivial speed difference shows.
For more critical issues on speed, see Wolfram Language Speed Guide
Select vs Cases
Cases is slightly slower when using the same function test.
$HistoryLength = 1; Module[{bigdata,rs1,rs2}, bigdata = RandomInteger[ {0,1}, 1000000 ]; Print@ First@ Timing[ rs1 = Select[ bigdata, OddQ ]; ]; Print@ First@ Timing[ rs2 = Cases[ bigdata, _?OddQ ]; ]; Print@ (rs1 === rs2); ] (* 0.20 0.23 True *)
Pattern matching, _ vs _Integer
the addition of checking head _Integer
does not seems to effect speed.
Module[{bigdata,rs1,rs2,tm1,tm2}, bigdata = RandomInteger[ {0,100}, 10000000 ]; tm1 = First@ Timing[ rs1 = Cases[ bigdata, _ ]; ]; tm2 = First@ Timing[ rs2 = Cases[ bigdata, _Integer ]; ]; Print@ tm1; Print@ tm2; Print@ (rs1 === rs2); ] (* 0.53 0.73 True *)
Apply Plus vs Total
using code construct such as
Function[Plus@@#]
is a bit slower than using builtin function such as
Function[Total[#]]
(* speed of Function[Plus@@#] vs Function[Total[#]] 2024-02-03 *) $HistoryLength = 1; Module[{bigdata,rs1,rs2,tm1,tm2,f1,f2,xVecLength,xCount}, f1 = Function[Plus@@#]; f2 = Function[Total[#]]; xVecLength = 4; xCount = 1000000; bigdata = RandomReal[{-10,10}, {xCount, xVecLength}]; tm1 = Timing[ rs1 = Map[ f1 , bigdata ]; ]; tm2 = Timing[ rs2 = Map[ f2 , bigdata ]; ]; Print@ First@ tm1; Print@ First@ tm2; Print@ SameQ[ rs1, rs2 ]; ] (* 0.09 0.06 True *)
Total vs Norm
same speed.
(* speed. Total vs Norm. 2024-02-03 *) $HistoryLength = 1; Module[{bigdata,rs1,rs2,tm1,tm2,f1,f2,xVecLength,xCount}, xVecLength = 3; xCount = 1000000; bigdata = RandomReal[{-10,10}, {xCount, xVecLength}]; f1 = Function[#/Norm[#]^2]; f2 = Function[#/Total[#^2]]; tm1 = Timing[ rs1 = Map[ f1 , bigdata]; ]; tm2 = Timing[ rs2 = Map[ f2 , bigdata]; ]; Print@ First@ tm1; Print@ First@ tm2; Print@ ( rs1 == rs2 ) ; ] (* 0.25 0.28 True *)
Total[v^2] vs Dot
the Dot
function v.v
is faster than Total[v^2]
, not perceivable.
At least if the vector components are real numbers.
Module[{bigdata,rs1,rs2,tm1,tm2}, bigdata = RandomReal[{-10, 10}, {1000000,3}]; f1 = Function[Total[#^2]]; f2 = Function[# . #] ; tm1 = First@ Timing[ rs1 = f1 /@ bigdata; ]; tm2 = First@ Timing[ rs2 = f2 /@ bigdata; ]; Print@ tm1; Print@ tm2; Print@ (rs1 == rs2); ] (* 0.09 0.10 True *)
when the vector length is millions,
# . # &
is 10 times faster than
Total[#^2] &
Module[{bigdata,rs1,rs2,tm1,tm2}, bigdata = RandomReal[{-10, 10}, 100000000]; f1 = Function[Total[#^2]]; f2 = Function[# . #] ; tm1 = First@ Timing[ rs1 = f1 @ bigdata; ]; tm2 = First@ Timing[ rs2 = f2 @ bigdata; ]; Print@ tm1; Print@ tm2; Print@ (rs1 == rs2); ] (* 0.18 0.04 True *) (* same speed, but big diff when vector length is over 10000000 *)
Function Args #1 #2 #3 vs Arg Sequence ##
Using ##
is slower than using explicit #1,#2,#3
Module[{dimen,rs1,rs2,tm1,tm2}, dimen=100; tm1 = First@ Timing[ rs1 = Array[ff[{##}, 1]&, {dimen, dimen, dimen }, {-dimen,-dimen,-dimen}];]; tm2 = First@ Timing[ rs2 = Array[ff[{#1, #2, #3}, 1]&, {dimen, dimen, dimen }, {-dimen,-dimen,-dimen}];]; Print@ tm1; Print@ tm2; Print@ (rs1 === rs2); ] (* 0.5 0.4 True *)
Nesting Functions vs With
(* 2024-02-01 *) Module[{bigdata,rs1,rs2,tm1,tm2}, bigdata = RandomReal[ {-10,10}, {100000, 3} ]; tm1 = First@ Timing[ rs1= Map[ Function[ With[{pt=Normalize[ # ]}, {-Last[pt], First[pt]} ] ], bigdata]; ]; tm2 = First@ Timing[ rs2= Map[ Function[ Function[{-Last[#], First[#]}][Normalize[ # ]] ], bigdata]; ]; Print@ tm1; Print@ tm2; Print@ (rs1 === rs2); ] (* 0.20 0.17 True *)
Table vs Array
Table
may be faster than using Array
,
in some cases, and barely, for huge data.
$HistoryLength = 1; Module[{dsize, bigdata, rs1, rs2, tm1, tm2}, dsize=50; tm1= Timing[ rs1 = Table[Cube[{x,y,z},1], {x,-dsize,dsize}, {y,-dsize,dsize}, {z,-dsize,dsize}]; ]; tm2= Timing[ rs2 = Array[ Function[Cube[{#1,#2,#3},1]], {2 dsize+1,2 dsize+1,2 dsize+1}, {-dsize,-dsize,-dsize}]]; Print@ First@ tm1; Print@ First@ tm2; Print@ (rs1 === rs2); ] (* 0.34 0.40 True *)
$HistoryLength = 1; Module[{dsize, bigdata, rs1, rs2, tm1, tm2}, dsize=50; ClearSystemCache[ ]; tm1= Timing[ rs1 = Table[Cube[{x,y,z},1], {x,0,dsize}, {y,0,dsize}, {z,0,dsize}]; ]; ClearSystemCache[ ]; tm2 = Timing[ rs2 = Array[ Function[Cube[{#1, #2, #3}, 1]], {dsize + 1, dsize + 1, dsize + 1}, {0, 0, 0}]]; Print@ First@ tm1; Print@ First@ tm2; Print@ (rs1 === rs2); ] (* 0.04 0.06 True *) (* inconclusive. often same speed *)