By the way, I should have been using RepeatedTiming
, not AbsoluteTiming
, to test performance.
For completeness, here are the timings on my computer:
Clear[tripletList];
tripletList = {{1, 2, 5}, {1, 2, 6}, {2, 1, 4}, {2, 2, 3}, {2, 2, 4}};
(* (1) DeleteCases with pattern test *)
RepeatedTiming[DeleteCases[tripletList, _?(#[[2]] == 1 &)], 5]
(* (2) Replace with Condition *)
RepeatedTiming[
Replace[tripletList,
element_ /; (element[[2]] == 1) -> Nothing, {1}], 5]
(* (3) Delete *)
RepeatedTiming[Delete[#, Position[#, {_, 1, _}]] &@tripletList, 5]
(* (4) DeleteCases *)
RepeatedTiming[DeleteCases[tripletList, {_, 1, _}], 5]
(* (5) Select *)
RepeatedTiming[Select[tripletList, #[[2]] != 1 &], 5]
(* OUTPUT from (1), (2), (3), (4), and (5), respectively: *)
{2.55258*10^-6, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}}
{6.02583*10^-6, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}}
{4.44223*10^-6, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}}
{5.75446*10^-7, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}}
{1.89883*10^-6, {{1, 2, 5}, {1, 2, 6}, {2, 2, 3}, {2, 2, 4}}}
So, in terms of timing, I have:
(2) Replace with Condition
>
(3) Delete
>
(1) DeleteCases with pattern test
>
(5) Select
>
(4) DeleteCases
with (4) being about 10 times faster than (2).