Can The Order Of Code Make This Program Faster?
Solution 1:
The function definition must be located above the function call, but the distance between the function definition and the function call does not matter (as the function code would already be loaded).
One way to make your program faster is to avoid repeated calculations that produce the same result, by performing the calculation once, storing the result in a variable and using that variable whenever you need the result.
For example, suppose your program handles a very large tuple. If len(veryLargeTuple)
occurs many times in the code, Python will repeatedly try to determine the length of the very large tuple. This is a waste of time because a tuple is immutable, so its length would not change, and finding the length of a very large tuple may take some time. It would be faster to store the result of len(veryLargeTuple)
in a variable veryLargeTupleLength
and use veryLargeTupleLength
whenever you need the length of the very large tuple (looking up the value of a variable is a very fast operation).
Solution 2:
It doesn't make the slightest bit of difference. Do whatever is most readable.
Post a Comment for "Can The Order Of Code Make This Program Faster?"