优化脚本性能 Optimizing Script Performance

This page gives some general hints for improving script performance on iOS.

此页面提供了一些一般的技巧,提高了在iOS上的脚本性能。

Reduce Fixed Delta Time 减少固定的增量时间

Use a fixed delta time value between 0.04 and 0.067 seconds (ie, 15-25 frames per second). You can change this in Edit->Project Settings->Time. This reduces the frequency with which FixedUpdate is called and how often the physics engine has to perform collision detection and rigidbody updates. If you are using a rigidbody for the main character, you can enable interpolation in the Rigidbody Component to smooth out low fixed delta time steps.

使用fixedDeltaTime值在0.04~0.067秒之间(例如,每秒15~25帧之间)。可以在Edit->Project Settings->Time修改这个。这降低了FixedUpdate被调用和物理引擎执行碰撞检测和刚体更新的频率。如果为主角色使用刚体,你可以在刚体组件启用插值来平滑降低固定增量时间步。

Reduce GetComponent Calls 减少GetComponent调用

Using GetComponent or built-in component accessors can have a noticeable overhead. You can avoid this by getting a reference to the component once and assigning it to a variable (sometimes referred to as "caching" the reference). For example, if you are using something like:-

使用GetComponent或内置的组件访问器有明显的性能开销。可以通过获取一次组件的引用并指定给变量来避免这个(有时也被称为"缓存"的引用)。例如,如果你使用像这样:

function Update () {
    transform.Translate(0, 1, 0);
}

...you would get better performance by changing it to:-

......通过改变它会得到更好的性能: -

var myTransform : Transform;

function Awake () {
   myTransform = transform;
}

function Update () {
    myTransform.Translate(0, 1, 0);
}

Avoid Allocating Memory 避免分配内存

You should avoid allocating new objects unless you really need to, since they increase the garbage collection overhead when they are no longer in use. You can often reuse arrays and other objects rather than allocate new ones and doing so will help to minimise garbage collection. Also, you should use structs instead of classes where possible. Struct variables are allocated from the stack like simple types rather than from the heap like object types. Since stack allocation is faster and involves no garbage collection, structs will often improve performance if they are fairly small in size. While large structs will still avoid allocation/collection overhead, they will incur a separate overhead due to "pass-by-value" copying and may actually be less efficient than the equivalent object classes.

你应该避免分配新的对象,除非你真的需要,当他们不再使用时,因为它们增加了垃圾收集的开销。你经常可以重复使用数组和其他物体,而不是分配新的,这样做将有助于减少垃圾收集。此外,在可能的情况下你应该使用结构而取代类。从堆栈(stack)中分配结构变量像简单类型而不是从堆(heap)中,像对象类型。由于堆栈分配速度非常快,不涉及垃圾收集,如果它们的大小相当小,结构往往会提高性能。同时大的结构仍然将避免分配/收集的开销,由于pass-by-value的拷贝将产生一个单独的开销,实际上可能效率低于同等的对象类。

Minimise the GUI 尽量减少GUI

The GUILayout functions are very convenient for automatic spacing of GUI elements. However, this automation naturally comes with a processing overhead. You can avoid some of this overhead by handling the layout manually using the GUI functions. Additionally, you can set a script‘s useGUILayoutvariable to false in order to disable the layout phase completely:-

GUILayout功能对于GUI元素的自动间距都非常方便。然而这种自动化有一些处理性能开销。通过使用GUI功能手动处理布局可以避免这种开销。此外,您可以设置脚本的useGUILayout变量为false,以完全禁用此布局。

function Awake () {
    useGUILayout = false;
}

Use iOS Script Call Optimization 使用iOS脚本调用优化

Most of the functions in the UnityEngine namespace are implemented in C/C++. Calling a C/C++ function from a Mono script involves a performance overhead. You can use iOS Script Call optimization (menu: Edit->Project Settings->Player) to save about 1 to 4 milliseconds per frame. The options for this setting are:-

大多数在UnityEngine命名空间的功能是在C/C++实现。从Mono脚本调用C/C++功能涉及到性能开销。可以使用iOS脚本调用优化(菜单: Edit->Project Settings->Player),节省大约每帧1至4毫秒。

  • Slow and Safe - the default Mono internal call handling with exception support. 
    慢而安全 - 默认的Mono内部调用支持异常处理。
  • Fast and Exceptions Unsupported - a faster implementation of Mono internal call handling. However, this doesn‘t support exceptions and so should be used with caution. An app that doesn‘t explicitly handle exceptions (and doesn‘t need to deal with them gracefully) is an ideal candidate for this option. 
    快而不提供异常 - Mono内部调用处理快速执行。然而,并不提供异常,所以应慎用。应用程序并不需要明确的处理异常,用此选项。

Optimizing Garbage Collection 优化垃圾收集

As mentioned above, it is best to avoid allocations as far as possible. However, given that they can‘t be completely eliminated, there are two main strategies you can use to minimise their intrusion into gameplay:-

如上所述,最好是尽可能避免分配。然而,由于不能完全消除,主要有两种方法可以使用,以减少它们侵入到游戏: -

Small heap with fast and frequent garbage collection
小堆快而频繁的垃圾收集

This strategy is often best for games that have long periods of gameplay where a smooth framerate is the main concern. A game like this will typically allocate small blocks frequently but these blocks will be in use only briefly. The typical heap size when using this strategy on iOS is about 200KB and garbage collection will take about 5ms on an iPhone 3G. If the heap increases to 1MB, the collection will take about 7ms. It can therefore be advantageous sometimes to request a garbage collection at a regular frame interval. This will generally make collections happen more often than strictly necessary but they will be processed quickly and with minimal effect on gameplay:-

这种策略对于长时间的游戏是最好的,有平滑的帧率是主要的考量。像这样的游戏通常会频繁地分配小块,但这些块将只是简单使用。当在iOS上使用这一策略时,典型的堆大小是大约200KB,在iPhone 3G垃圾收集大于需要5ms,如果堆增加到1MB,收集大约需要7ms。因此这是很有利的,有时垃圾收集在一个规则的帧间隔。这通常会使收集发生很多时候绝对必要的,但他们将迅速处理并对游戏影响很小: -

if (Time.frameCount % 30 == 0)
{
   System.GC.Collect();
}

However, you should use this technique with caution and check the profiler statistics to make sure that it is really reducing collection time for your game.

但是,应该谨慎使用这项技术,并检查分析器统计以确保它真的为游戏减少了垃圾收集时间。

Large heap with slow but infrequent garbage collection
大堆慢,但不经常垃圾收集

This strategy works best for games where allocations (and therefore collections) are relatively infrequent and can be handled during pauses in gameplay. It is useful for the heap to be as large as possible without being so large as to get your app killed by the OS due to low system memory. However, the Mono runtime avoids expanding the heap automatically if at all possible. You can expand the heap manually by preallocating some placeholder space during startup (ie, you instantiate a "useless" object that is allocated purely for its effect on the memory manager):-

这一策略最适合于分配是相对偶发并可在游戏暂停期间处理的游戏。对于堆需要尽可能的大而由于系统内存不足没有那么大,操作系统将杀死应用,这是很有用的。然而,如果可能,在Mono运行时避免自动扩大堆。在启动期间通过预留占位空间可以手动扩展堆(例如,实例化一个无用的对象来分配,纯粹为其在内存管理器的效果)。

function Start() {
	var tmp = new System.Object[1024];

	// make allocations in smaller blocks to avoid them to be treated in a special way, which is designed for large blocks
        for (var i : int = 0; i < 1024; i++)
		tmp[i] = new byte[1024];

	// release reference
        tmp = null;
}

A sufficiently large heap should not get completely filled up between those pauses in gameplay that would accommodate a collection. When such a pause occurs, you can request a collection explicitly:-

一个足够大的堆,在游戏容纳一个收集,暂停之前不应该完全被填满。当这样暂停发生时,应该明确要求收集:

System.GC.Collect();

Again, you should take care when using this strategy and pay attention to the profiler statistics rather than just assuming it is having the desired effect.

再次,使用此策略,应该注意分析器统计情况,而不是仅仅假设它是有预期的效果。

页面最后更新: 2011-10-11

标签: 优化脚本 iOS Unity3D

分类:Manual| 翻译: U_鹰

时间: 2024-08-24 19:22:27

优化脚本性能 Optimizing Script Performance的相关文章

Unity3D Optimizing Graphics Performance for iOS

原地址:http://blog.sina.com.cn/s/blog_72b936d801013ptr.html icense Comparisons http://unity3d.com/unity/licenses#iphone Optimizing Graphics Performance http://unity3d.com/support/documentation/Manual/Optimizing Graphics Performance.html iOS A useful bac

[转]Oracle DB 通过SQL 优化管理性能

? 将SQL 优化指导用于: – 确定使用资源最多的 SQL 语句 – 优化使用资源最多的 SQL 语句 ? 使用SQL 访问指导优化工作量 SQL 优化 SQL 优化进程 ? 确定没有很好地优化的SQL 语句. ? 优化各条语句. ? 优化整个应用程序. 一般情况下,效果最明显的优化工作是SQL 优化.没有很好地优化的SQL 会不必要地使用过多资源.这种低效率会降低可伸缩性.使用更多的OS 和数据库资源并增加响应时间.要对没有很好地优化的SQL 语句进行优化,必须先确定这些语句,然后再进行优化

14个优化网站性能提高网站访问速度技巧

相信互联网已经越来越成为人们生活中不可或缺的一部分.ajax,flex等等富客户端的应用使得人们越加“幸福”地体验着许多原先只能在C/S实 现的功能.比如Google机会已经把最基本的office应用都搬到了互联网上.当然便利的同时毫无疑问的也使页面的速度越来越慢.自己是做前端开发的,在性能方面,根据yahoo的调查,后台只占5%,而前端高达95%之多,其中有88%的东西是可以优化的. 以上是一张web2.0页面的生命周期图.工程师很形象地讲它分成了“怀孕,出生,毕业,结婚”四个阶段.如果在我们

雅虎十四条 - 14个优化网站性能提高网站访问速度的技巧

14个优化网站性能提高网站访问速度的技巧 又叫“雅虎十四条”,想起一年前那个懵懂的我,大四傻乎乎的跑到大学城面试前端,那个时候以为寒暑假看了两套CSS的视频,就很牛B了,出发先还把视频温了一下,嗯嗯,这是滑动门,嗯嗯这是绝对定位,嗯嗯这是浮动清除…… 当时是彪叔面试我的,当时我还不知道那个人,全身黑漆漆的,黑色T-shirt,黑色皮肤,黑色帽子,黑色墨镜,还有点黑色胡渣的人,就是彪叔,补做了试题后支支吾吾的跟他谈了一下,发现完全不行,第一个问题是“雅虎十四条”是什么?然后我蒙了,pardon?

页面性能监控之performance

页面性能监测之performance author: @TiffanysBear 最近,需要对业务上的一些性能做一些优化,比如降低首屏时间.减少核心按钮可操作时间等的一些操作:在这之前,需要建立的就是数据监控的准线,也就是说一开始的页面首屏数据是怎样的,优化之后的数据是怎样,需要有一个对比效果.此时,performance 这个API就非常合适了. performance Performance 接口可以获取到当前页面中与性能相关的信息.它是 High Resolution Time API 的

Oracle 优化和性能调整

分析评价Oracle数据库性能主要有数据库吞吐量.数据库用户响应时间两项指标.数据库用户响应时间又可以分为系统服务时间和用户等待时间两项,即:  数据库用户响应时间=系统服务时间+用户等待时间  因此,获得满意的用户响应时间有两个途径:一是减少系统服务时间,即提高数据库的吞吐量:二是减少用户等待时间,即减少用户访问同一数据库资源的冲突率.  数据库性能优化包括如下几个部分:  调整数据结构的设计 这一部分在开发信息系统之前完成,程序员需要考虑是否使用Oracle数据库的分区功能,对于经常访问的数

深入oracle 12c数据库备份与恢复(优化RMAN性能、Oracle flashback技术)

课程讲师:小流老师 课程分类:Oracle 适合人群:高级 课时数量:15课时 更新程度:完成 服务类型:C类(普通服务类课程) 用到技术:oracle 涉及项目:oracle 12c数据库备份与恢复 需要更多相关资料可以联系 Q2748165793 课程提纲 第一讲:介绍备份和恢复相关操作 备份和恢复的目标 备份和恢复的解决方案 Oracle flashback技术 第二讲:开始RMAN操作和RMAN体系架构 RMAN的环境 RMAN Channels RMAN Repository RMAN

Oracle DBA优化数据库性能心得

如今的优化己经向优化等待(waits)转型了,实际中性能优化最根本的出现点也都集中在IO,这是影响性能最主要的方面,由系统中的等待去发现Oracle库中的不足.操作系统某些资源利用的不合理是一个比较好的办法. 一.通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲这也可能不是一个正常的状态,因为cpu可能正等待IO的完成.除此之外我们还应观注那些占用系统资源(cpu.内存)的进程. 1.如何检查操作系统是否存

DB2日常维护——REORG TABLE命令优化数据库性能

[转]DB2日常维护——REORG TABLE命令优化数据库性能 一个完整的日常维护规范可以帮助 DBA 理顺每天需要的操作,以便更好的监控和维护数据库,保证数据库的正常.安全.高效运行,防止一些错误重复发生. 由于DB2使用CBO作为数据库的优化器,数据库对象的状态信息对数据库使用合理的 ACCESS PLAN至关重要.DB2 优化器使用目录统计信息来确定任何给定查询的最佳访问方案.如果有关表或索引的统计信息已过时或者不完整,则会导致优化器选择不是最佳的方案,并且会降低 执行查询的速度.当数据