Vector2 slower than Vector3 (ignore solved, my mistake)

This is my current bottleneck (without function ~150fps, with func ~50fps so huge bottleneck)
so I thought I’ld improve it by going from 3d to 2d (as 3d is not necessary)

but it runs ~20% slower, Im guessing SIMD, but I would of assumed they also done this with Vector2?

So why is 2d so much slower than 3d?

TLDR its the same math but from vec3 → vec2, yet it runs slower

Cheers zed

Vector2 steer = Vector2.zero;
        int num = 0;
       
        for (int j = App.instance.theGame.actorMan.actorList.Count - 1; j >= 0; --j)
        {
            Vector2 OP = App.instance.theGame.actorMan.actorList[j].pos2D;
            float d = Vector3.SqrMagnitude(pos2D - OP);
            if ( d < desiredseparation && d > 0 )
            {
                Vector2 diff = (pos2D - OP) * (1f / d);

                steer += diff;
                num++;
            }
        }
        if (num > 0)
        {
            steer /= ((float)num);
            goto_pos.x += steer.x * 50;
            goto_pos.y += steer.y * 50;
        }
Vector3 steer = Vector3.zero;
        int num = 0;

        for (int j = App.instance.theGame.actorMan.actorList.Count - 1; j >= 0; --j)
        {
            Vector3 OP = App.instance.theGame.actorMan.actorList[j].pos;
            float d = Vector3.SqrMagnitude(pos - OP);
            if ( d < desiredseparation && d > 0 )
            {
                Vector3 diff = (pos - OP) * (1f / d);

                steer += diff;
                num++;
            }
        }
        if (num > 0)
        {
            steer /= ((float)num);
            goto_pos += steer * 50;
        }

Well, I don’t know much about optimization, but looking at line 7 you still use Vector3.SqrMagnitude for both of them, it could be having to create a new struct every time when casting makes it slower.

1 Like