官术网_书友最值得收藏!

Sharing calculation output

Performance can be saved by having multiple objects share the result of some calculation; of course, this only works if all of them generate the same result. Such situations are often easy to spot but can be tricky to refactor, and so exploiting this would be very implementation-dependent. 

Some examples might include finding an object in a scene, reading data from a file, parsing data (such as XML or JSON), finding something in a big list or deep dictionary of information, calculating pathing for a group of Artificial Intelligence (AI) objects, complex mathematics-like trajectories, raycasting, and so on.

Think about each time an expensive operation is undertaken, and consider whether it is being called from multiple locations but always results in the same output. If this is the case, then it would be wise to restructure things so that the result is calculated once and then distributed to every object that needs it to minimize the amount of recalculation. The biggest cost is typically just a small loss in code simplicity, although we may inflict some extra overhead by moving the value around.

Note that it's often easy to get into the habit of hiding some big complex function in a base class, and then we define derived classes that make use of that function, completely forgetting how costly that function is because we rarely glance at that code again. It's best to use the Unity Profiler to tell us how many times that expensive function may be called, and as always, don't preoptimize those functions unless it's been proven to be a performance issue. No matter how expensive it may be, if it doesn't cause us to exceed performance restrictions (such as frame rate and memory consumption), then it's not really a performance problem.

主站蜘蛛池模板: 余干县| 玉龙| 金堂县| 景洪市| 安徽省| 河西区| 南雄市| 凤阳县| 弋阳县| 巴里| 土默特左旗| 洛扎县| 丰城市| 鹤峰县| 平遥县| 南陵县| 兴安县| 进贤县| 昌江| 大英县| 页游| 莲花县| 乐亭县| 乌拉特后旗| 喀喇沁旗| 宁乡县| 鹤峰县| 杂多县| 芜湖县| 巫山县| 秀山| 涡阳县| 商水县| 咸丰县| 兴安县| 凤山县| 五寨县| 高要市| 营山县| 府谷县| 大丰市|