if len(str(a)) == len(str(r)) and isMult(a, r): faster if isMult is slow?

Chris cwitts at gmail.com
Mon Aug 11 09:17:49 EDT 2008


On Aug 11, 3:03 pm, maestro <notnorweg... at yahoo.se> wrote:
> If isMult is slow then:
>
> if len(str(a)) == len(str(r)) and isMult(a, r):
>                 trues.append((a, r))
>
> will be much faster than:
>
> if isMult(a, r) and len(str(a)) == len(str(r)):
>                 trues.append((a, r))
>
> right? seems obvious  but there is no magic going on that wouldn't
> make this true right?

Once a failed condition is met the rest are skipped for evaluation.
Whether or not that saves you any reasonable amount of time would be
up to the various tests being used with your conditions.

If your function isMult is just doing the same work as "if not a % r"
then that would be faster than casting your integers/floats to a
string and testing their length imo.

You could always go through the trouble of profiling your code with
separate if statements to see how much time is spent on each, also
note your mean failure rate for each conditional test and then decide
for yourself which to place first.

Hope that helps,
Chris



More information about the Python-list mailing list