# How speed up this script?

I am using a script to calculate the vorticity at each particle using the surrounding particles and set the angular velocity to be used by the shaders… I iterate through each particle, and then filter a list of particles to those that are within a certain distance of the given particle, into a new list. Then use some cross products with all the particles in that list to get my angular velocity. Is there some better way to be doing this? When I have a lot of particles it gets very slow. Here is the script:

``````import bpyimport mathutils
import math

def calcav(p,ps):
psorted = []
M = p.size*5
psl = list(filter(lambda q: (q.alive_state == 'ALIVE' and math.fabs(q.location-p.location) &lt; M and math.fabs(q.location-p.location) &lt; M and math.fabs(q.location-p.location) &lt; M), ps))
for q in psl:
dist = (mathutils.Vector(q.location)- mathutils.Vector(p.location)).length
if  dist &lt; M and dist &gt; 0 and len(psorted)&lt;50:
psorted.append(q)
currentdist = dist

angularvel = mathutils.Vector((0,0,0))
if len(psorted)&gt;0:
for i in range(len(psorted)):
angularvel +=  (mathutils.Vector(psorted[-1-i].location)- mathutils.Vector(p.location)).cross(mathutils.Vector(psorted[-1-i].velocity)- mathutils.Vector(p.velocity))/((mathutils.Vector(psorted[-1-i].location)- mathutils.Vector(p.location)).length**2)
angularvel/= len(psorted)
p.angular_velocity = (angularvel.x,angularvel.y,angularvel.z)

del psorted[:]
del psl[:]

def calclist(ps):
print('Start')
for p in ps:
if p.alive_state == 'ALIVE':
calcav(p,ps)
print('Done')

def my_handler(scene):
ps1 = bpy.context.scene.objects['SPH'].particle_systems

ts = []
print(lb)
ts[-1].start()

for t in ts:
t.join()
del ts[:]

def register():
bpy.app.handlers.render_pre.append(my_handler)

def unregister():
bpy.app.handlers.render_pre.remove(my_handler)

try:
unregister()
except ValueError:
pass

register()
``````