It seems that the result of list of Timing
s and AbsoluteTiming
s depends on the order, while RepeatedTiming
is free from this problem. How to understand this behaviour of timing functions?
The related codes are as follows. (The original aim is to compare the performance of similar functions on different types of data.)
Tested function
types = {"integer", "letter", "symbolLetter", "word", "symbolWord"}
functions = {complementVerbatim, Complement}
argnumber = 2;
(*`argnumber` specifies the number of arguments*)
complementVerbatim[list1_List, list2_List] :=
DeleteCases[list1, Alternatives @@ Verbatim /@ list2];
Sample space
Choose portion
of the samples as test data and save them into downvalues of data
.
size = 2 10^4;
portion = 0.6;
sampleSpace["integer"] = RandomInteger[2 size, size];
sampleSpace["letter"] = RandomChoice[Alphabet[], size];
sampleSpace["word"] = RandomWord[size];
sampleSpace["symbolLetter"] = ToExpression /@ sampleSpace["letter"];
sampleSpace["symbolWord"] = ToExpression /@ sampleSpace["word"];
Table[data[type, argnumber] =
Table[RandomSample[sampleSpace[type], portion size // Floor], {i, argnumber}],
{type, types}];
Timing
Now evaluate the functions on data with different order and timing functions.
Table[timing[Sequence @@ exception] := "/", {exception, exceptions}];
timing[function_, type_, argnumber, timingFunction_] :=
First@timingFunction[function @@ data[type, argnumber];]
timing[abs, fun] =
Outer[timing[#1, #2, argnumber, AbsoluteTiming] &, functions, types];
timing[abs, type] =
Outer[timing[#2, #1, argnumber, AbsoluteTiming] &, types, functions] // Transpose;
timing[re, fun] =
Outer[timing[#1, #2, argnumber, RepeatedTiming] &, functions, types];
timing[re, type] =
Outer[timing[#2, #1, argnumber, RepeatedTiming] &, types, functions] // Transpose;
timing[fun] =
Outer[timing[#1, #2, argnumber, Timing] &, functions, types];
timing[type] =
Outer[timing[#2, #1, argnumber, Timing] &, types, functions] // Transpose;
timingShow[x__] := SparseArray`SparseBlockMatrix[{
{1, 1} -> {{""}},
{1, 2} -> {types},
{2, 1} -> List /@ functions,
{2, 2} -> timing[x]
}] // TableForm
Result
Interestingly the three timing functions give different results.
The results of RepeatedTiming
don't depend on the order:
while the results of Timing
s and AbsoluteTiming
s do depend, and they are different from single runs like
timing[Complement, "integer", argnumber, Timing]
(*0.011819*)
(*comparing with 0.004266 in the table.*)
hence are untrustable.
timeAvg[]
was invented and thenRepeatedTiming
was added to Mma.Timing
is said to be unreliable in a parallelized/hyperthreaded environment because not everything is counted the same on every system.AbsoluteTiming
measures elapsed time according to the "wall clock" and depends on how busy your computer is. $\endgroup$timeAvg[]
do you mean this answer? mathematica.stackexchange.com/a/22427/86893 $\endgroup$