Inspired by Evil4Zerggin, I ran 100,000 iterations of various functions, focusing on vector and math functions (code is below).

+ I:Maths_AngleBetweenVectors(): 34ms

+ Semi-manual angle between vectors (math.acos(Vector3.Dot(a[i],b[i]) / (Vector3.Magnitude(a[i]) * Vector3.Magnitude(b[i])))): 6.7ms

+ Vector addition: highly variable (?), ~4ms

+ Vector multiplication by scalar: highly variable, ~3ms

+ Vector3.Magnitude: 1.1ms

+ Vector3.Normalized: 1.5ms

+ Vector3.Angle: Namespace error (!)

+ Vector3.Project: ~4ms

+ Manual vector addition (not reformed): 1.2ms

+ Manual vector addition (reformed): ~4ms

+ projection (a+b*2): ~4ms

+ Manual projection (not reformed): 1.2ms

+ Manual projection (reformed): ~4ms

+ Quaternion.LookRotation: ~45ms

+ Vector3.Distance: 1.2ms

+ Manual distance: 1.2ms

This definitely suggests that vectors carry with them the magnitude and normalized variables, and update them stupidly (in the case of multiplication by scalar). The extreme variability is odd, and unexplained (it should not be hammering the GC, I think...). For now, I think the lesson is to minimize vector code, and to do manual computations if you can get away without reforming them. Also, avoid I:Maths_AngleBetweenVectors and quaternions at all costs (although the manual angle computation is not all that bad; in either event, time to rewrite my missile code).

+ I:Maths_AngleBetweenVectors(): 34ms

+ Semi-manual angle between vectors (math.acos(Vector3.Dot(a[i],b[i]) / (Vector3.Magnitude(a[i]) * Vector3.Magnitude(b[i])))): 6.7ms

+ Vector addition: highly variable (?), ~4ms

+ Vector multiplication by scalar: highly variable, ~3ms

+ Vector3.Magnitude: 1.1ms

+ Vector3.Normalized: 1.5ms

+ Vector3.Angle: Namespace error (!)

+ Vector3.Project: ~4ms

+ Manual vector addition (not reformed): 1.2ms

+ Manual vector addition (reformed): ~4ms

+ projection (a+b*2): ~4ms

+ Manual projection (not reformed): 1.2ms

+ Manual projection (reformed): ~4ms

+ Quaternion.LookRotation: ~45ms

+ Vector3.Distance: 1.2ms

+ Manual distance: 1.2ms

This definitely suggests that vectors carry with them the magnitude and normalized variables, and update them stupidly (in the case of multiplication by scalar). The extreme variability is odd, and unexplained (it should not be hammering the GC, I think...). For now, I think the lesson is to minimize vector code, and to do manual computations if you can get away without reforming them. Also, avoid I:Maths_AngleBetweenVectors and quaternions at all costs (although the manual angle computation is not all that bad; in either event, time to rewrite my missile code).

Code:

`a = {}`

b = {}

N = 100000

for i = 1, N do

a[i] = Vector3(math.random(), math.random(), math.random())

b[i] = Vector3(math.random(), math.random(), math.random())

end

function Update(I)

for i = 1, N do

-- x = a[i].x + b[i].x * 2

-- y = a[i].y + b[i].y * 2

-- z = a[i].z + b[i].z * 2

-- c = Vector3(x,y,z)

c = math.sqrt(a[i].x^2 + a[i].y^2 + a[i].z^2)

end

end

Allr andask.