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性能的前端优化技巧总结
随机推荐
- [SecureCRT] 解决 securecrt failed to open the host key database file 的问题
SecureCRT 在 Windows XP 和 Windows 7 中的个人应用数据路径是不同的,在 Windows 7 中,应用数据路径为:C:\Users\<username>\Ap ...
- nginx安装教程
一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.首先要安装 PCRE ...
- centos系统-java -jdk 环境配置
方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# c ...
- DLL注入之Appinit_Dlls
AppInit_DLLs is a mechanism that allows an arbitrary list of DLLs to be loaded into each user mode p ...
- postgreSQL连接 java接口
1.下载PostgreSQL JDBC驱动: http://jdbc.postgresql.org/download.html 2. 新建一个java项目,导入下载的jar包Add External ...
- 【大数据系列】hadoop命令指导官方文档翻译
Hadoop Commands Guide Overview Shell Options Generic Options User Commands archive checknative class ...
- 转载->C#事件的使用和讲解
C#事件的使用和讲解 事件的由来 在上一篇幅博客中http://www.cnblogs.com/JiYF/p/6867081.html 对委托讲解的比较细致 我们继续思考上面的程序:上面的三个方法都定 ...
- Android 屏幕适配:最全面的解决方案
转自:https://www.jianshu.com/p/ec5a1a30694b 前言 Android的屏幕适配一直以来都在折磨着我们Android开发者,本文将结合: Google的官方权威适配文 ...
- sencha touch 自定义事件
需要添加自定义事件可以如下: this.fireEvent('back', this); 此方法第一个参数为你想要监听的事件,之后的参数为你想要传递的参数一般来说第一个参数最好是控件本身. 同理这个方 ...
- VMware ESXI5.5 Memories limits resolved soluation.
在使用VMware ESXI5.5 的时候提示内存限制了,在网上找的了解决方案: 如下文: 1. Boot from VMware ESXi 5.5; 2. wait "Welcome to ...