My question is both specific (see example) and general. I am looking for best practices for writing fast-running python code. Of two or more ways of evaluating an expression or constructing a statement, I want to have an educated guess as to which will run faster before I do timeit
comparison tests.
For example:
xx = [1,2,3,4,5]
x_bar = int(sum(xx)/len(xx)) # or = np.mean(xx)
x_c = [int(x_i - x_bar) for x_i in xx]
TSS = sum([x_i*x_i for x_i in x_c])
I thought I might speed things up by changing x_i*x_i
to x_i**2
. But if I am reading the results correctly, it was actually ~3 times slower:
%%timeit
sum([x_i*x_i for x_i in x_c])
# 670 ns ± 19 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%%timeit
sum([x_i**2 for x_i in x_c])
# 1.8 µs ± 120 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
An explanation of why this is true for the *
operator vs. the **
operator will be helpful. Even more helpful will be advice on principles, rules of thumb, and resources that will help me recognize opportunities to improve my code. Any suggestions?