Mathematica PrograMing Constructs Timing Comparison
- To: mathgroup at smc.vnet.net
- Subject: [mg9738] Mathematica PrograMing Constructs Timing Comparison
- From: Xah Lee <xah at best.com>
- Date: Tue, 25 Nov 1997 00:06:39 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Attached below is the latest Mathematica PrograMing Construct Timing Comparison notebook. To use it, copy and paste into your Mathematica v.3 frontend. The notebook can also be retrieved from <http://www.best.com/~xah/MathematicaPrograming_dir/MathematicaTiming.nb> Xah, xah at best.com http://www.best.com/~xah/SpecialPlaneCurves_dir/specialPlaneCurves.html Mountain View, CA, USA "C -> C++ | Objective C -> Java. Sucks still!" ------------------------------ Notebook[{ Cell[CellGroupData[{ Cell[TextData[{ "Mathematica PrograMing Constructs Timing Comparison\n", StyleBox["Last updated: 1997/10/08", FontSize->14] }], "Title"], Cell[CellGroupData[{ Cell["Introduction", "Subsection"], Cell[TextData[{ "This notebook compares the speed of various programing constructs. Which \ is faster: N at Table[...] or Table[N at ...,iterator]? Map[f[i],Range[n]] or \ Table[f[i],{i,1,n}]? Map[f,{g...}] or. {g...}/.g->f[g]? 3+3 or 3*3? 1*2. or \ N[1*2]? Is Nest[N,1,100000] much slower than N[1]? In general, these are \ things serious Mathematica programers would be interested.\nThis notebook is \ an open project, and is in public domain. You can get this notebook at \ (http://www.best.com/~xah/MathematicaPrograming_dir/MathematicaTiming.nb) or send an \ email to (xah at best) to request it. Feel free to edit this notebook in any way \ and send the result notebook to Xah Lee (xah at best.com) and I will post the \ updated notebook. Again, edit and add to this notebook in ", StyleBox["any ", FontWeight->"Bold"], "way that you think better serves its goal. You may want to put your name \ and email address in parts you edited or written so people can contact you. \ Significant improvement of this notebook will be announced on MathGroup \ monthly or every two months. Let's hope this notebook become a rolling \ snowball and will become a significant Mathematica literature in the future.\n\ Xah Lee. (xah at best.com) 1997/08." }], "Text"] }, Open ]], Cell[CellGroupData[{ Cell["Argument variation on the same function", "Section"], Cell[CellGroupData[{ Cell["Transpose", "Subsubsection"], Cell[BoxData[ \(n = 10000; \nli1 = \(li2 = Range at 5\); \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[Transpose[{li1, li2}], {n}]\)], "Input"], Cell[BoxData[ \({2.76666666666687888`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[Transpose[{li1, li2}, {2, 1}], {n}]\)], "Input"], Cell[BoxData[ \({2.86666666666678793`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["RotateLeft[li] vs RotateLeft[li,1]", "Subsubsection"], Cell[BoxData[ \(n = 10000; \nli = Range at 5; \nli2 = Range at 500; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(RotateLeft[li]\), \(RotateLeft[li, 1]\)}], "Input"], Cell[BoxData[ \({2, 3, 4, 5, 1}\)], "Output"], Cell[BoxData[ \({2, 3, 4, 5, 1}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[RotateLeft[li], {n}]\)], "Input"], Cell[BoxData[ \({0.533333333333757764`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[RotateLeft[li, 1], {n}]\)], "Input"], Cell[BoxData[ \({0.583333333333030168`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["RotateLeft", "Subsubsection"], Cell["\<\ Does the time to RotateLeft a list depends on the length of the \ list? Result indicates yes. Time probably grow linearly with length of \ list.\ \>", "Text"], Cell[BoxData[ \(n = 1000; \nli = Range at 5; \nli2 = Range at 500; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[RotateLeft[li], {n}]\)], "Input"], Cell[BoxData[ \({0.0666666666666060336`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[RotateLeft[li2], {n}]\)], "Input"], Cell[BoxData[ \({1.31666666666660603`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["If[False,#] vs If[False,#,Null]", "Subsubsection"], Cell["\<\ Here we test whether an explicit False clause of If slows it down. \ That is: If[False,#] vs If[False,#,Null]. The result indicates practical no \ speed difference.\ \>", "Text"], Cell[BoxData[ \(\(li = Range at 10000; \)\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(First at \(Timing at \((\(If[False, #]&\)/@li)\)\)\)], "Input"], Cell[BoxData[ \(0.516666666666424134`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(First at \(Timing at \((\(If[False, #, Null]&\)/@li)\)\)\)], "Input"], Cell[BoxData[ \(0.549999999999727151`\ Second\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Prepend, Append, Insert", "Subsubsection"], Cell["\<\ Is there a timing difference between Prepend, Append, or Insert? \ Result indicates no.\ \>", "Text"], Cell[BoxData[ \(n = 1000; \nli = Table[1, {100}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Append[li, a], {n}]\), \(Timing at Do[Prepend[li, a], {n}]\), \(Timing at Do[Insert[li, a, 1], {n}]\), \(Timing at Do[Insert[li, a, \(-1\)], {n}]\)}], "Input"], Cell[BoxData[ \({0.233333333333291648`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.216666666666696983`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.233333333333348491`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.266666666666708351`\ Second, Null}\)], "Output"] }, Open ]], Cell["\<\ Does the timing of Append, Prepend, or Insert depends on the length \ of its argument? Result indicates they differ in micro seconds.\ \>", "Text"], Cell[BoxData[ \(Clear[li1, li2]; \nli1 = Table[1, {10}]; \nli2 = Table[1, {100000}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(First at \(Timing at Append[li1, a]\)\), \(First at \(Timing at Append[li2, a]\)\)}], "Input"], Cell[BoxData[ \(0.`\ Second\)], "Output"], Cell[BoxData[ \(0.166666666666666962`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(First at \(Timing at Prepend[li1, a]\)\), \(First at \(Timing at Prepend[li2, a]\)\)}], "Input"], Cell[BoxData[ \(0.`\ Second\)], "Output"], Cell[BoxData[ \(0.200000000000001065`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(First at \(Timing at Insert[li1, a, 1]\)\), \(First at \(Timing at Insert[li2, a, \(-1\)]\)\)}], "Input"], Cell[BoxData[ \(0.`\ Second\)], "Output"], Cell[BoxData[ \(0.18333333333333357`\ Second\)], "Output"] }, Open ]] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell["Basic Mathematica Commands", "Section"], Cell[CellGroupData[{ Cell["Length[shortExpr] vs Length[longExpr]", "Subsubsection"], Cell[BoxData[ \(n = 10000; \nli = Range at 1; \nli2 = Table[0, {10000}]; \n li3 = Nest[f, f, 1000]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Length@li1, {n}]\), \(Timing at Do[Length@li2, {n}]\), \(Timing at Do[Length@li3, {n}]\)}], "Input"], Cell[BoxData[ \({0.150000000000545696`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.300000000000181898`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.283333333333757764`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Plus vs Times vs Length", "Subsubsection"], Cell[BoxData[ \(n = 10000; \nli = Range at 50; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Length@li, {n}]\), \(Timing at Do[3*3, {n}]\), \(Timing at Do[3 + 3, {n}]\), \(Timing at Do[0 + 0, {n}]\)}], "Input"], Cell[BoxData[ \({0.233333333333348491`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.166666666666742457`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.166666666666742457`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.150000000000090949`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Module, With, and Block", "Subsubsection"], Cell[BoxData[ \(\(n = 50000; \)\)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Module[{}, 3], {n}]\), \(Timing at Do[With[{}, 3], {n}]\), \(Timing at Do[Block[{}, 3], {n}]\)}], "Input"], Cell[BoxData[ \({2.71666666666664013`\ Second, Null}\)], "Output"], Cell[BoxData[ \({2.25`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.899999999999977262`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Adding new head vs replaceing head", "Subsubsection"], Cell["\<\ In devising data stractures, is it better to have \ struc[{e1,e2,...}] or struc[e1,e2,...]? The former seems to be easier to \ manipulate both speedwise and program-wise.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(n = 1000; \nli = {1, 2}\)], "Input"], Cell[BoxData[ \({1, 2}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[f@li, {n}]\)], "Input"], Cell[BoxData[ \({0.0333333333328482694`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[f@@li, {n}]\)], "Input"], Cell[BoxData[ \({0.133333333333212067`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Pattern Matching Varieties", "Subsubsection"], Cell[BoxData[ \(Clear[n, li1, li2]; \nn = 1000; \nli1 = Table[Range at 3, {3}]; \n li2 = Table[Range at 10, {10}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[MatchQ[li, _], {n}]\), \(Timing at Do[MatchQ[li, _List], {n}]\), \(Timing at Do[MatchQ[li, x_List], {n}]\)}], "Input"], Cell[BoxData[ \({0.133333333333212067`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.149999999999636202`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.366666666666787932`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[MatchQ[li2, _], {n}]\), \(Timing at Do[MatchQ[li2, _List], {n}]\), \(Timing at Do[MatchQ[li2, x_List], {n}]\)}], "Input"], Cell[BoxData[ \({0.133333333333212067`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.149999999999636202`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.333333333333939663`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[MatchQ[li, x : {___}], {n}]\), \(Timing at Do[MatchQ[li, x_List /; MatrixQ], {n}]\), \(Timing at Do[MatchQ[li, x : {_List ... }], {n}]\), \(Timing at Do[MatchQ[li, x : {{_, _} ... }], {n}]\)}], "Input"], Cell[BoxData[ \({0.433333333332484471`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.533333333332848269`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.75`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.916666666666060336`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[MatchQ[li2, x : {___}], {n}]\), \(Timing at Do[MatchQ[li2, x_List /; MatrixQ], {n}]\), \(Timing at Do[MatchQ[li2, x : {_List ... }], {n}]\), \(Timing at Do[MatchQ[li2, x : {{_, _} ... }], {n}]\)}], "Input"], Cell[BoxData[ \({0.46666666666715173`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.550000000000181898`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.916666666666969831`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.849999999999454303`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Rule vs RuleDelayed", "Subsubsection"], Cell["\<\ There seems to be no speend difference. It appears that RuleDelayed \ should be much slower in certain cases, where it needs to calculate the \ replacement function for each match. This needs future testing.\ \>", "Text"], Cell[BoxData[ \(n = 50000; \nli = Table[Random[Integer, {1, 5}], {n}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(First at Timing[li /. {1 -> x, 2 -> x}]\)], "Input"], Cell[BoxData[ \(0.900000000000090949`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(First at Timing[li /. {1 :> x, 2 :> x}]\)], "Input"], Cell[BoxData[ \(0.883333333333212067`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(First at Timing[li /. \((1 | 2)\) -> x]\)], "Input"], Cell[BoxData[ \(1.53333333333330301`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(First at Timing[li /. \((1 | 2)\) :> x]\)], "Input"], Cell[BoxData[ \(1.55000000000018189`\ Second\)], "Output"] }, Open ]] }, Closed]], Cell["Part vs Extract", "Subsubsection"], Cell["First at expr vs expr[[1]]", "Subsubsection"], Cell[CellGroupData[{ Cell["Prepend vs Flatten[{a,li},1]", "Subsubsection"], Cell["\<\ Which one is faster, Prepend[li,a] or Flatten[{a,li},1]? Prepend \ seems to be twice as fast.\ \>", "Text"], Cell[BoxData[ \(n = 1000; \nli = Table[{1}, {10}]; \nli2 = Table[{1}, {100}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Prepend[li, a], {n}]\), \(Timing at Do[Flatten[{a, li}, 1], {n}]\)}], "Input"], Cell[BoxData[ \({0.0833333333334849157`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.266666666666651508`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Append[li, a], {n}]\), \(Timing at Do[Flatten[{li, a}, 1], {n}]\)}], "Input"], Cell[BoxData[ \({0.0833333333332575421`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.266666666666651508`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Prepend[li2, a], {n}]\), \(Timing at Do[Flatten[{a, li2}, 1], {n}]\)}], "Input"], Cell[BoxData[ \({0.533333333333303016`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.866666666666560558`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["PrependTo vs li=Insert[li,a,1] vs li=Flatten[{a,li},1]", "Subsubsection"], Cell["Which one is faster?", "Text"], Cell[BoxData[ \(n = 1000; \nli1 = \(li2 = \(li3 = \(li4 = {1}\)\)\); \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[PrependTo[li1, a], {n}]\), \(Timing at Do[li2 = Insert[li2, a, 1], {n}]\), \(Timing at Do[li3 = Flatten[{a, li3}, 1], {n}]\), \(Timing at Do[li4 = {a, li4}, {n}] (* result\ needs\ to\ be\ \(flattened . \)*) \)}], "Input"], Cell[BoxData[ \({1.21666666666666856`\ Second, Null}\)], "Output"], Cell[BoxData[ \({1.18333333333333712`\ Second, Null}\)], "Output"], Cell[BoxData[ \({2.31666666666666287`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.0499999999999971578`\ Second, Null}\)], "Output"] }, Closed]] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell["Similar commands", "Section"], Cell[CellGroupData[{ Cell["RotateLeft vs Reverse", "Subsubsection"], Cell[BoxData[ \(n = 10000; \nli = Range at 5; \nli2 = Range at 50; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[RotateLeft[li], {n}]\), \(Timing at Do[Reverse[li], {n}]\)}], "Input"], Cell[BoxData[ \({0.533333333332848269`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.5`\ Second, Null}\)], "Output"] }, Closed]], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[RotateLeft[li2], {n}]\), \(Timing at Do[Reverse[li2], {n}]\)}], "Input"], Cell[BoxData[ \({1.60000000000036379`\ Second, Null}\)], "Output"], Cell[BoxData[ \({1.56666666666660603`\ Second, Null}\)], "Output"] }, Closed]] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell["Composite Constructs with identical output", "Section"], Cell[CellGroupData[{ Cell["\<\ calling module vs in-place coding vs in-place and hard-wired coding\ \ \>", "Subsubsection"], Cell[BoxData[ \(n = 1000; \nvectorLength[v_] := Sqrt[v . v]; \n vectorLength2[{a1_, a2_}] := Sqrt[a1^2 + a2^2]; \n v = Table[Random[], {2}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[{ \(Timing at Do[Sqrt[v.v], {n}]\), \(Timing at Do[\((\(Sqrt[#.#]&\)@v)\), {n}]\), \(Timing at Do[vectorLength[v], {n}]\), \(Timing at Do[vectorLength2[v], {n}]\)}], "Input"], Cell[BoxData[ \({0.266666666666651508`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.316666666666606033`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.416666666666742457`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.516666666666651508`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Flatten vs Join", "Subsubsection"], Cell[CellGroupData[{ Cell[BoxData[{ \(n = 10000; \nTiming at Do[Join[{4}, {3}], {n}]\), \(Timing at Do[Flatten[{{4}, {3}}], {n}]\), \(Timing at Do[Flatten[{{4}, {3}}, 1], {n}]\)}], "Input"], Cell[BoxData[ \({0.5`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.566666666666606033`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.616666666666787932`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Insert vs. First and Last", "Subsubsection"], Cell[BoxData[ \(n = 10000; \nli = {1, 2}; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[Insert[li, 0, 2], {n}]\)], "Input"], Cell[BoxData[ \({0.816666666666606033`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[{First@li, 0, Last at li}, {n}]\)], "Input"], Cell[BoxData[ \({0.816666666666606033`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Take vs Part", "Subsubsection"], Cell[CellGroupData[{ Cell[BoxData[{ \(n = 10000; \nTiming at Do[Take[li, 2], {n}]\), \(Timing at Do[Part[li, {1, 2}], {n}]\)}], "Input"], Cell[BoxData[ \({0.516666666666424134`\ Second, Null}\)], "Output"], Cell[BoxData[ \({0.666666666666969831`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Partition Flatten vs Apply Sequence", "Subsubsection"], Cell[CellGroupData[{ Cell[BoxData[ \(Clear[n, li]; \nn = 1000; \nli = Table[{m, n}, {m, 1, 5}, {n, 1, 2}]; \)], "Input"], Cell[BoxData[ \({{{1, 1}, {1, 2}}, {{2, 1}, {2, 2}}, {{3, 1}, {3, 2}}, {{4, 1}, {4, 2}}, {{5, 1}, {5, 2}}}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(Partition[\ Flatten[\ li, 2\ ], 4\ ]\), \(Apply[Sequence, li, {2}]\)}], "Input"], Cell[BoxData[ \({{1, 1, 1, 2}, {2, 1, 2, 2}, {3, 1, 3, 2}, {4, 1, 4, 2}, {5, 1, 5, 2}} \)], "Output"], Cell[BoxData[ \({{1, 1, 1, 2}, {2, 1, 2, 2}, {3, 1, 3, 2}, {4, 1, 4, 2}, {5, 1, 5, 2}} \)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[Partition[\ Flatten[\ li, 2\ ], 4\ ], {n}]\)], "Input"], Cell[BoxData[ \({0.350000000000363797`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing at Do[Apply[Sequence, li, {2}], {n}]\)], "Input"], Cell[BoxData[ \({0.650000000000545696`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]], Cell[CellGroupData[{ Cell["Transpose, RotateRight vs Partition Append", "Subsubsection"], Cell["Speed seems equal", "Text"], Cell[BoxData[ \(n = 1000; \nli1 = \(li2 = Range at 50\); \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(Timing@ Do[Partition[\ Flatten[\ Transpose[{\ Partition[Append[li1, First at li1], 2, 1], Transpose[{RotateLeft[li2], li2}]}], 2\ ], 4\ ], {n}]\)], "Input"], Cell[BoxData[ \({3.06666666666666287`\ Second, Null}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Timing@ Do[Partition[\ Flatten[\ Transpose[{\ Transpose[{li1, RotateLeft[li1]}], Transpose[{RotateLeft[li2], li2}]}], 2\ ], 4\ ], {n}]\)], "Input"], Cell[BoxData[ \({3.11666666666667424`\ Second, Null}\)], "Output"] }, Open ]] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell["Same algorithm with different Mathematica constructs", "Section"], Cell[CellGroupData[{ Cell["Recursive Definition vs FoldList", "Subsubsection"], Cell["\<\ Finding and remembering the accumulated product of a list. \ i.e.Table[Times@@Take[li,n],{n,1,Length at li}]. Two constructs are compared. \ One uses FoldList, the other uses a recursive definition. The FoldList is an \ order faster.\ \>", "Text"], Cell[BoxData[ \(Clear[li, p]; \nli = Table[0, {1000}]; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(First at \(Timing at FoldList[#1*#2&, First at li, Rest at li]\)\)], "Input"], Cell[BoxData[ \(0.0666666666666060336`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Clear[p]; \np[1] = li[\([1]\)]; \n p[n_] := \(p[n] = p[n - 1]*li[\([n]\)]\); \n First at \(Timing at Table[p[i], {i, 1, Length at li, $RecursionLimit - 10}]\)\)], "Input"], Cell[BoxData[ \(0.983333333333348491`\ Second\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Clear[p]; \np[1] = li[\([1]\)]; \n p[n_] := \(p[n] = p[n - 1]*li[\([n]\)]\); \n First at \(Timing at Table[p[i], {i, 1, Length at li, 1}]\)\)], "Input"], Cell[BoxData[ \(1.20000000000004547`\ Second\)], "Output"] }, Open ]] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell["others", "Section"], Cell["N at Table[] vs Table[N,...]", "Subsubsection"] }, Open ]], Cell[CellGroupData[{ Cell["Compiled vs uncomplied", "Section"], Cell[CellGroupData[{ Cell["lots of arithemetics", "Subsubsection"], Cell[BoxData[ \(\(Clear[lineIntersectionPoint, lineIntersectionPoint2, lineIntersectionPointCompiled]; \)\)], "Input"], Cell[BoxData[ \(\(lineIntersectionPoint[{{a1_, a2_}, {b1_, b2_}}, {{c1_, c2_}, {d1_, d2_}}] := { a2*b1*c1 - a1*b2*c1 - a2*b1*d1 + a1*b2*d1 - a1*c2*d1 + b1*c2*d1 + a1*c1*d2 - b1*c1*d2, a2*b1*c2 - a1*b2*c2 - a2*c2*d1 + b2*c2*d1 - a2*b1*d2 + a1*b2*d2 + a2*c1*d2 - b2*c1*d2}/ \((a2*c1 - b2*c1 - a1*c2 + b1*c2 - a2*d1 + b2*d1 + a1*d2 - b1*d2)\); \)\)], "Input"], Cell[BoxData[ \(lineIntersectionPointCompiled = Compile[{a1, a2, b1, b2, c1, c2, d1, d2}, {a2*b1*c1 - a1*b2*c1 - a2*b1*d1 + a1*b2*d1 - a1*c2*d1 + b1*c2*d1 + a1*c1*d2 - b1*c1*d2, a2*b1*c2 - a1*b2*c2 - a2*c2*d1 + b2*c2*d1 - a2*b1*d2 + a1*b2*d2 + a2*c1*d2 - b2*c1*d2}/ \((a2*c1 - b2*c1 - a1*c2 + b1*c2 - a2*d1 + b2*d1 + a1*d2 - b1*d2) \)]; \nlineIntersectionPoint2[{{a1_, a2_}, {b1_, b2_}}, {{c1_, c2_}, {d1_, d2_}}] := lineIntersectionPointCompiled[a1, a2, b1, b2, c1, c2, d1, d2]; \)], "Input"], Cell[BoxData[ \(Clear[solutions, pts, n, result]; \n solutions = {lineIntersectionPoint, lineIntersectionPoint2}; \n pts = Table[Random[], {4}, {2}]; \nn = 2000; \)], "Input"], Cell[CellGroupData[{ Cell[BoxData[ \(Transpose@ Map[Timing at Do[\((#@@Partition[pts, 2])\), {n}]&, solutions]\)], "Input"], Cell[BoxData[ \({{5.48333333333334849`\ Second, 1.79999999999995452`\ Second}, {Null, Null}}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Transpose at Map[Timing@\((#@@Partition[pts, 2])\)&, solutions]\)], "Input"], Cell[BoxData[ \({{0.0166666666666515084`\ Second, 0.`\ Second}, {{\(-2.73449489339338169`\), 7.90802803387283415`}, { \(-2.73449489339338169`\), 7.90802803387283415`}}}\)], "Output"] }, Open ]] }, Closed]] }, Open ]] }, Open ]] }, FrontEndVersion->"Macintosh 3.0", ScreenRectangle->{{0, 832}, {0, 604}}, WindowSize->{730, 526}, WindowMargins->{{4, Automatic}, {Automatic, 1}}, MacintoshSystemPageSetup->"\<\ 00l0001804P000000_02 at ?oaonh32@9BH`85:0?l0 at 00005X0FP000003[0;@001 0 at 00I00100000@0200000BL?00400000000000000000010000000000000 at 0?ne ojH?;@^J00P1000000000000000001T1\>" ]