Skip to content

Commit 1e98b80

Browse files
authored
Merge pull request #969 from NoahZuo/main
添加高精度时间的文档
2 parents ae25506 + b5f38d2 commit 1e98b80

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

.config/.vitepress/config.mts

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default defineConfig({
119119
{ text: "定制微信小游戏的 URP 管线", link: "/Design/CustomURP" },
120120
{ text: "WebGL2.0渲染支持说明", link: "/Design/WebGL2" },
121121
{ text: "性能深度分析工具", link: "/Design/DeepProfileTool" },
122+
{ text: "高精度时间", link: "/Design/HighPreciseTime" },
122123
],
123124
},
124125
],

Design/HighPreciseTime.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 使用高精度时间进行性能检测
2+
3+
## 背景
4+
5+
在浏览器默认环境中,开发者通常使用`performance.now()``Date.now()`获取时间戳,但这些API存在精度限制:
6+
7+
- `Date.now()`返回自1970年1月1日UTC至今的整型毫秒时间戳,精度固定为1ms
8+
- `performance.now()`理论上可返回浮点型微秒级数值(如1780.7999999988824),但实际精度受浏览器限制:
9+
- Chrome:0.1ms(100μs)
10+
- Safari:1ms
11+
12+
![开发者工具的Performance.now()](../image/high-precise-time/HighPreciseTime_Image_0.png)
13+
14+
相较之下,端游/手游APP普遍支持微秒级精度。浏览器环境的时间精度限制对微信小游戏的性能优化工作形成了显著制约。
15+
16+
## 高精度时间与性能深度分析工具
17+
18+
自基础库3.5.4版本起,微信小游戏为Android平台及iOS普通模式提供了底层高精度时间支持。该功能默认关闭,需同时满足以下条件方可启用:
19+
20+
1. 勾选`Development Build`
21+
2. 启用`Enable Perf Analysis`导出选项
22+
23+
![性能深度分析工具](../image/high-precise-time/image.png)
24+
25+
详细使用指南请参考:[性能深度分析工具文档](https://wechat-miniprogram.github.io/minigame-unity-webgl-transform/Design/DeepProfileTool.html)
26+
27+
## 手动开启高精度时间功能
28+
29+
通过修改JS代码可强制启用高精度时间:
30+
31+
1. 打开`weapp-adapter.js`
32+
2. 定位`const clientPerfAdapter = Object.assign`代码段:
33+
```javascript
34+
const clientPerfAdapter = Object.assign({}, {
35+
now: function now() {
36+
if (GameGlobal.unityNamespace.isDevelopmentBuild
37+
&& GameGlobal.unityNamespace.isProfilingBuild
38+
&& !GameGlobal.unityNamespace.isDevtools
39+
&& !GameGlobal.isIOSHighPerformanceMode) {
40+
// 由于wx.getPerformance()获取到的是微秒级,因此这里需要/1000.0,进行单位的统一
41+
return (ori_performance.now() - ori_initTime) * 0.001;
42+
}
43+
return (Date.now() - initTime);
44+
},
45+
});
46+
```
47+
3. 删除`GameGlobal.unityNamespace.isDevelopmentBuild``GameGlobal.unityNamespace.isProfilingBuild`条件判断。
48+
49+
50+
## 注意事项
51+
52+
1. 平台支持
53+
- 当前仅支持Android和iOS普通模式
54+
- 其他模式仍保持毫秒级精度
55+
56+
2. 性能影响
57+
- `performance.now()``Date.now()`存在性能开销
58+
- 不建议在正式版本中开启
59+
60+
3. 版本依赖
61+
- 需基础库版本≥3.5.4
62+
- 若手动配置未生效,请检查基础库版本
Loading

image/high-precise-time/image.png

115 KB
Loading

0 commit comments

Comments
 (0)