I am writing a large numerical code and I have set up many functions that take several arguments as input. In the functions I am using several built in Mathematica functions, such as Sum[], Part[], ReplacePart[], Select[], FromDigits[], IntegerDigits[] and more. I know in advance that all the input arguments are small integers (or nested lists of small integers), definitely smaller than 100. Also, I do care about numeric performance a lot, so I want to make sure that my functions are as fast as possible. I know about Compile[], but sometimes the compiled version of my functions are outperformed by the non compiled version, and sometimes Compile[] complains about the presence of things like ReplacePart, so I never know in advance whether it is worth to rewrite my functions in a compiled version or not.
My question: Is it numerically faster to specify the argument input types? If yes, how can I do that efficiently avoiding Compile[], which conflicts with ReplacePart[] and other functions? I've tried something like Typed[], but honestly I couldn't see any relevant speed up...
Example: here's a decontextualized example of one function: L, f, sigma, orb are all very small integers, while state is a nested list of small integers. Can I use this information to speed this up?
cdg[L_, f_, \[Sigma]_, orb_, state_]:=Module[
{binarystate, index},
index = f*(orb-1)+\[Sigma];
binarystate = IntegerDigits[#,2,L]&@state;
If[binarystate[[index,1]]==0,
binarystate = ReplacePart[binarystate,{index,1}->1];
Return[FromDigits[#,2]&/@binarystate],
(*else*)
Return[0]
];
];
Thank you for any help!
index
is set (and setting it if not). This can be done way more quickly using bitwise operations, no need to ever convert the state to a list of digits. Same with your return statement: Why are you convertingstate
to digits and then back again, rather than simply returning the appropriate part ofstate
? Finally, note thatReturn
is not needed here $\endgroup$