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性能的前端优化技巧总结
随机推荐
- ARM入门最好的文章
一 首先说说arm的发展 可以用一片大好来形容,翻开各个公司的网站,招聘里面嵌入式占据了大半工程师职位.广义的嵌入式无非几种:传统的什么51.avr.pic称做嵌入式微控制器:arm是嵌入式微处理器 ...
- Material Design系列第一篇——Creating Apps with Material Design
Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...
- Esper学习之八:EPL语法(四)
关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...
- 【Spring源码深度解析学习系列】Bean的加载(六)
Bean的加载所涉及到的大致步骤: 1)转换对应beanName 为什么需要转换beanName呢?因为传入的参数可能是别名,也可能是FactoryBean,所以需要一系列的解析,这些解析内容包括如下 ...
- css案例 - 评分效果的星星✨外衣
纳尼?什么星星外衣?好,直接上图比较能说清楚: 仔细看会发现规律:可以根据百分比/分值动态改变高亮星星的个数. 分步骤图: 这种效果,如果遇到一分一个星,没有半星(或者有也可以,直接加一个半星的类名) ...
- 阅读代码工具:Visual Studio Code
打开一个文件夹,直接阅读,体验还不错 版本: 1.25.1提交: 1dfc5e557209371715f655691b1235b6b26a06be日期: 2018-07-11T15:43:11.471 ...
- ios-toolchain-based-on-clang-for-linux
https://github.com/tpoechtrager/cctools-port.git https://www.embtoolkit.org
- linux学习路线图
- Node.j中path模块对路径的操作
一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...
- 使用docker搭建公司redmine服务器
What is Redmine? Redmine is a flexible project management web application. Written using the Ruby on ...