I have a loop in one part of my code. I tried to change all vectors into an example to make it simple as see in below sample. I have to try it this loop 230000 inside the other loop. this part take about 26.36. is there any way to speed up or tune the speed to get optimized speed.
trr=time.time()
for i in range (230000):
print(+1 *0.0001 * 1 * 1000 * (
1 * np.dot(np.subtract([2,1], [4,3]), [1,2]) + 1
* np.dot(
np.cross(np.array([0, 0, 0.5]),
np.array([1,2,3])),
np.array([1,0,0]))
- 1 * np.dot((np.cross(
np.array([0,0,-0.5]),
np.array([2,4,1]))), np.array(
[0,1,0]))) )
print(time.time()-trr)
the code with variable:
For i in range (23000):
.......
.....
else:
delta_fs = +1 * dt * 1 * ks_smoot * A_2d * (
np.dot(np.subtract(grains[p].v, grains[w].v), vti) * sign +
np.dot(np.cross(np.array([0, 0, grains[p].rotational_speed]),
np.array(np.array(xj_c) - np.array(xj_p))),
np.array([vti[0], vti[1], 0])) * sign
- np.dot((np.cross( np.array([0, 0, grains[w].rotational_speed]),
np.array(np.array(xj_c) - np.array(xj_w)))), np.array(
[vti[0], vti[1], 0])) * sign)
It would've been better if you kept your examples in variables, since your code is very difficult to read. Ignoring the fact that the loop in your example just computes the same constant value over and over again, I am working under the assumption that you need to run a specific set of numpy operations many times on various numpy arrays/vectors. You may find it useful to spend some time looking into the documentation for numba. Here's a very basic example:
Seems like it'll be multiple order of magnitudes faster, which should easily bring your time down to a fraction of a second.