Unity3D中使用Profiler精确定位性能热点的优化技巧
本文由博主(SunboyL)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/BeginProfiler.html
简介
在使用Profiler定位代码的性能热点时,很多同学往往忽略Profiler的提供接口,当发现某个Update函数特别耗时时,没有有效的手段进一步定位热点出自该Update函数的哪一个模块或哪一段代码。
使用Profiler评估客户端性能时,推荐使用Profiler提供的性能采样接口,来更精确地分析定位客户端存在的性能问题。
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class TestProfiler : MonoBehaviour {
int t = ; // 每帧Update都会进行校验和运行
void Update () {
Check(t); // 校验
Run(); // 运行
} void Check(int n) {
ProfilerSample.BeginSample("Check");
CheckA(); // 校验模块A ProfilerSample.BeginSample("Calculate b");
// 数值运算
int b = n - ;
if (b < )
b = ;
ProfilerSample.EndSample(); CheckB(b); // 校验模块B
ProfilerSample.EndSample();
} void CheckA() {
ProfilerSample.BeginSample("CheckA");
Debug.Log("校验模块A");
ProfilerSample.EndSample();
} void CheckB(int loopCount) {
ProfilerSample.BeginSample("CheckB(loopCount={0})", loopCount);
Debug.Log("校验模块B"); ProfilerSample.BeginSample("new List<string>");
List<string> strList = new List<string>();
ProfilerSample.EndSample(); for (int i = ; i < loopCount; ++i) {
ProfilerSample.BeginSample("Add str to list");
string str = string.Format("CheckB:{0}", i);
strList.Add(str);
ProfilerSample.EndSample();
} Debug.Log(string.Format("list count: {0}", strList.Count));
ProfilerSample.EndSample();
} void Run() {
ProfilerSample.BeginSample("Run");
Debug.Log("开始运行");
DoSomething();
ProfilerSample.EndSample();
} void DoSomething() {
} void OnGUI() {
GUILayout.BeginVertical();
if (GUILayout.Button("Enable/Disable ProfierSample.")) {
ProfilerSample.EnableProfilerSample = !ProfilerSample.EnableProfilerSample;
} if (GUILayout.Button("Enable/Disable Profier sting format.")) {
ProfilerSample.EnableFormatStringOutput = !ProfilerSample.EnableFormatStringOutput;
}
}
}
关闭和开启Profiler性能采样接口的对比截图:


using UnityEngine;
using System; public class ProfilerSample { // by SunboyL
public static bool EnableProfilerSample = true;
public static bool EnableFormatStringOutput = true;// 是否允许BeginSample的代码段名字使用格式化字符串(格式化字符串本身会带来内存开销) public static void BeginSample(string name) {
#if ENABLE_PROFILER
if (EnableProfilerSample){
Profiler.BeginSample(name);
}
#endif
} public static void BeginSample(string formatName, params object[] args) {
#if ENABLE_PROFILER
if (EnableProfilerSample) {
// 必要时很有用,但string.Format本身会产生GC Alloc,需要慎用
if (EnableFormatStringOutput)
Profiler.BeginSample(string.Format(formatName, args));
else
Profiler.BeginSample(formatName);
}
#endif
} public static void EndSample() {
#if ENABLE_PROFILER
if (EnableProfilerSample) {
Profiler.EndSample();
}
#endif
}
}
Unity3D中使用Profiler精确定位性能热点的优化技巧的更多相关文章
- ASP.NET 性能监控工具和优化技巧
转载自:http://blog.haoitsoft.com/index.php/archives/657 ASP.NET 性能监控工具和优化技巧 发表回复 为了阐明准确甄别性能问题的重要性,下面列举了 ...
- JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码
本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 提升到 20 (提升了 7 倍) ...
- 使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码 (jvm性能调优)
技术交流群:233513714 本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 ...
- ifeve.com 南方《JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码》
https://blog.csdn.net/defonds/article/details/52598018 多次拉取 JStack,发现很多线程处于这个状态: at jrockit/vm/Al ...
- 15 个有用的 MySQL/MariaDB 性能调整和优化技巧(转载的一篇好文)
MySQL 是一个强大的开源关系数据库管理系统(简称 RDBMS).它发布于 1995 年(20年前).它采用结构化查询语言(SQL),这可能是数据库内容管理中最流行的选择.最新的 MySQL 版本是 ...
- 【MySQL】15个有用的MySQL/MariaDB性能调整和优化技巧
MySQL 是一个强大的开源关系数据库管理系统(简称 RDBMS).它发布于 1995 年(20年前).它采用结构化查询语言(SQL),这可能是数据库内容管理中最流行的选择.最新的 MySQL 版本是 ...
- 15 个有用的 MySQL/MariaDB 性能调整和优化技巧
MySQL 是一个强大的开源关系数据库管理系统(简称 RDBMS).它发布于 1995 年(20年前).它采用结构化查询语言(SQL),这可能是数据库内容管理中最流行的选择.最新的 MySQL 版本是 ...
- NDK(20)JNI的5大性能缺陷及优化技巧
转自 : http://www.ibm.com/developerworks/cn/java/j-jni/index.html JNI 编程缺陷可以分为两类: 性能:代码能执行所设计的功能,但运行缓慢 ...
- 提高Web性能的前端优化技巧总结
随机推荐
- mybatis 之resultType="Map"
Map map = new HashMap(); map.put("productTypeID", productTypeId); List<HashMap> prod ...
- error:please select android sdk
发现问题所在就是 在model iml文件中: 把<orderEntry type="inheritedJdk" /> 改成 <orderEntry type=& ...
- 在 Core Data 中存取 transformable 类型的数据
本文转载至 http://imenjoe.com/2015/04/10/CoreData-transformable-20150410/ 在开发过程中有一个需要在 Core Data 中存取 NSDi ...
- iOS 沙盒目录结构及正确使用
前言:处于安全考虑,iOS系统的沙盒机制规定每个应用都只能访问当前沙盒目录下面的文件(也有例外,比如在用户授权情况下访问通讯录,相册等),这个规则展示了iOS系统的封闭性.在开发中常常需要数据存储的功 ...
- IOS设计模式第二篇之单例设计模式
现在我们的组件已经有组织了.你需要从其他的地方得到数据,你也可以创建一个API类管理数据这个下个设计模式单例里面介绍. 这个单例设计模式确保这个类仅仅拥有一个实例,并且为这个实例提供一个全局的访问点. ...
- 如何修改 VIM 制表符的空格数?
想修改一下编辑器vi里的制表符(Tab)的空格数.因为它默认的太长(默认是8个空格). 在网上搜到了这篇文章http://my.oschina.net/captaintheron/blog/515 ...
- elementUI Message 独立引入的用法
同理,Alert,MessageBox, Notification, 也是这样引入 单组件单独引用: import { Message } from 'element-ui'; export defa ...
- Centos 7 系统操作
修改系统语言 https://blog.csdn.net/hanchao_h/article/details/72820999 修改后,马上查看man bash,发现已经变成了英文版.(中文版句子不通 ...
- Modelsim SE 仿真 ALTERA FPGA IP
Modelsim SE 仿真 ALTERA FPGA IP 最近,有几个朋友问过我是不是有新版本的Modelsim altera,其原因是 Qii 升级为新版本的,但是没配套的modelsim,没办法 ...
- FPGA时序约束的几种方法 (转)
FPGA时序约束的几种方法 对自己的设计的实现方式越了解,对自己的设计的时序要求越了解,对目标器件的资源分布和结构越了解,对EDA工具执行约束的效果越了解,那么对设计的时序约束目标就会越清晰,相应地, ...