I'm writing a lot of print functions for different parts in a package and at the moment am using something like
myprint[ identifierString_String ][ data_ ] := ...
where the identifierString is some string that determines what data will be printed (and reminds me where I use that specific function).
A lot of the strings are of the form
"functionName_subpart_subsubpart"
If I have a huge amount of different strings for which the function myprint
is defined, would it be faster to use a definition as follows:
myprint[functionName_String][subpart_String][subsubpart_String][data_]
or does Mathematica use some built-in hashing functionality so that it doesn't have to loop over the whole list of { myprint[string1], myprint[string2], ... }
to find the correct match?
I wrote the following code to check which one would be faster and the timing for the code where the strings are split is worse than the code with full strings. One thing that bothers me, though, is that in this timing the functions that use split strings need to access a list of strings and this might add a significant amount of time. I wouldn't know how to get rid of this extra time though...
(* Generate 280 strings from some basic building blocks. *)
tuples = Tuples[
{
{ "FindZeroValues_", "SolveMonomialSystem_", "SolveFullSystem_",
"BreakSymmetry_", "CheckSolutions_", "RevertVariables",
"DeleteEquivalentSolutions" },
{"ProperSystem_", "NoVars_", "EmptyList_", "NumericAnalysis_",
"Substitution_"},
{"True", "False", "Invalid", "Trivial", "Ambiguous", "Error",
"Warning", "Failed"}
}
];
fullStrings = StringJoin @@@ tuples;
(* Define a print function using the full string as an argument *)
Do[
myPrint1[string][data_] := data,
{string, fullStrings }
];
(* Define a print function using sub-parts of strings as arguments *)
Do[
myPrint2[tp[[1]]][tp[[2]]][tp[[3]]][data_] := data,
{tp, tuples}
];
RepeatedTiming[
Table[ myPrint1[ string ][ 1 ] , {string, fullStrings} ];
] // First
(* Out: 0.00301837 *)
RepeatedTiming[
Table[ myPrint2[ tp[[1]] ][ tp[[2]] ][ tp[[3]] ][ 1 ], {tp, tuples} ]
] // First
(* Out: 0.0119834 *)
strings=fullStrings
somewhere? $\endgroup$fullstrings
. I usedstrings
in my own test but thought the word fullStrings would be clearer so changed it manually. Post is edited now. $\endgroup$