Search Feedback

57
votes

Vector3 and other structs optimization of operators

Profiling & Optimization

-

-

A lot of Unity's Structs' operators can have a massive performance increase, in a very simple way.
In my test script (available here https://dropb.in/ponda.nimrod), I reduce the Vector3 multiplication cost from 18.9 ms to 2.5 ms

If we take the * operator as an example, the current implementation does this:
{
return new Vector3(a.x * d, a.y * d, a.z * d);
}
The implementation I suggest would be like this:
{
Vector3 result;
result.x = a.x * d;
result.y = a.y * d;
result.z = a.z * d;
return result;
}
This avoids calling unnecessarily the Vector3 constructor.

What I just explained for the Vector3 struct it is also true for other Unity's structs (like Vector2)

Comments (2)

  1. 3e9ed461bfbb2bb331f686df5059d3bb?d=mm

    ehakram

    Jun 18, 2017 21:12

    I confirm that the optimization works for standalone applications in release mode ("Development Build" disabled)
    I used a heavier version of the script above, and used Fraps to get the FPS count (to rule out any potential Unity's profiler issue), and here are the results:
    - Optimized version: 53 FPS
    - Unoptimized version: 42 FPS

  2. F151c66a2cc7eeef6f209cda8195da21?d=mm

    ThomasTrenkwalder

    Jun 18, 2017 08:52

    Unitys operator is 30% slower for me, both in editor and in build on 5.6.1f1

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea

Categories

All

(8665)

2D

(211)

Ads

(34)

AI & Navigation

(53)

Analytics

(85)

Animation

(288)

Asset Store

(202)

Assets

(480)

Audio

(149)

Cloud Build

(92)

Collaborate

(27)

Docs & Tutorials

(185)

Editor

(2005)

Everyplay

(13)

Game Performance Reporting

(10)

General

(786)

Graphics

(729)

GUI

(345)

Input

(140)

Licensing

(72)

Networking

(150)

Physics

(319)

Platforms

(397)

Profiling & Optimization

(66)

Runtime

(149)

Scripting

(973)

Terrain

(148)

WebGL

(128)