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

Share 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 would 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 in order to minimize the amount of recalculation. The biggest cost is typically just a small loss in code simplicity, although some extra overhead may be inflicted by passing 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.

主站蜘蛛池模板: 富蕴县| 建始县| 乌拉特后旗| 航空| 扬州市| 凌云县| 麦盖提县| 玉树县| 涿鹿县| 寿阳县| 农安县| 准格尔旗| 延庆县| 吴堡县| 定安县| 丰宁| 加查县| 威远县| 铅山县| 晋州市| 卢氏县| 桦甸市| 雷波县| 景德镇市| 迁安市| 连平县| 土默特左旗| 大港区| 义马市| 怀远县| 兰坪| 上饶县| 咸宁市| 博湖县| 扎赉特旗| 岱山县| 明水县| 平度市| 木兰县| 都兰县| 海原县|