简体   繁体   中英

Speed up the mullite operator cross product and dot product in numpy python

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:

import numpy as np
import numba as nb

CONST = 1*0.0001*1*1000
a0 = np.array([2.,1.])
a1 = np.array([4.,3.])
a2 = np.array([1.,2.])
b0 = np.array([0., 0., 0.5])
b1 = np.array([1.,2.,3.])
b2 = np.array([1.,0.,0.])
c0 = np.array([0.,0.,-0.5])
c1 = np.array([2.,4.,1.])
c2 = np.array([0.,1.,0.])

@nb.jit()
def op1(iters):
    for i in range(iters):
        op = CONST * (1 * np.dot(a0-a1,a2)
                      + 1 * np.dot(np.cross(b0,b1),b2)
                      - 1 * np.dot(np.cross(c0,c1),c2))
op1(1) # Initial compilation

def op2(iters):
    for i in range(iters):
        op = CONST * (1 * np.dot(a0-a1,a2)
                      + 1 * np.dot(np.cross(b0,b1),b2)
                      - 1 * np.dot(np.cross(c0,c1),c2))

%timeit -n 100 op1(100)
# 54 µs ± 2.49 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit -n 100 op2(100)
# 15.5 ms ± 313 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Seems like it'll be multiple order of magnitudes faster, which should easily bring your time down to a fraction of a second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM