找回密码
 立即注册
查看: 566|回复: 3

[笔记] 使用Unity的Job System性能为什么没有明显提升?

[复制链接]
发表于 2021-4-22 09:14 | 显示全部楼层 |阅读模式
最近在看Job System,做了一下性能对比,当计算数量在100000以内时,使用Job System反而不如正常的处理。
发表于 2021-4-22 09:18 | 显示全部楼层
最近也在使用和研究jobsystem,简单回答一下
首先jobsystem为了保证无锁多线程,内部做了很多job编排工作,所以在数据量很小的时候,这种编排操作就会造成比单线程耗时还长的情况出现
其次jobsystem可以用burst compiler优化,使用unity特定的数学库中的数据类型可以获取simd优化,比如vector3就可以换成float3
下面给出我的测试代码和结果
  1. using Unity.Burst;
  2. using Unity.Collections;
  3. using Unity.Jobs;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using UnityEngine.Profiling;
  7. public class TestJob : MonoBehaviour
  8. {
  9.     public int DataCount;
  10.     private NativeArray<float3> m_JobDatas;
  11.     private NativeArray<float> m_JobResults;
  12.     private Vector3[] m_NormalDatas;
  13.    
  14.     private float[] m_NormalResults;
  15.    
  16. // Job adding two floating point values together
  17.     [BurstCompile]
  18.     public struct MyParallelJob : IJobParallelFor
  19.     {
  20.         [ReadOnly] public NativeArray<float3> data;
  21.         public NativeArray<float> result;
  22.         public void Execute(int i)
  23.         {
  24.             Vector3 item = data[i];
  25.             result[i] = Mathf.Sqrt(item.x * item.x + item.y * item.y + item.z * item.z);
  26.         }
  27.     }
  28.     private void Awake()
  29.     {
  30.         m_JobDatas = new NativeArray<float3>(DataCount, Allocator.Persistent);
  31.         m_JobResults = new NativeArray<float>(DataCount,Allocator.Persistent);
  32.         
  33.         m_NormalDatas = new Vector3[DataCount];
  34.         m_NormalResults = new float[DataCount];
  35.         
  36.         for (int i = 0; i < DataCount; i++)
  37.         {
  38.             m_JobDatas[i] = new float3(1, 1, 1);
  39.             m_NormalDatas[i] = new Vector3(1, 1, 1);
  40.         }
  41.     }
  42.     // Update is called once per frame
  43.     void Update()
  44.     {
  45.         //Job部分
  46.         MyParallelJob jobData = new MyParallelJob();
  47.         jobData.data = m_JobDatas;
  48.         jobData.result = m_JobResults;
  49. // Schedule the job with one Execute per index in the results array and only 1 item per processing batch
  50.         JobHandle handle = jobData.Schedule(DataCount, 64);
  51. // Wait for the job to complete
  52.         handle.Complete();
  53.         
  54.         Profiler.BeginSample("NormalCalculate");
  55.         
  56.         //正常数据运算
  57.         for(var i = 0; i < DataCount; i++)
  58.         {
  59.             var item = m_NormalDatas[i];
  60.             m_NormalResults[i] = Mathf.Sqrt(item.x * item.x + item.y * item.y + item.z * item.z);
  61.         }
  62.         
  63.         Profiler.EndSample();
  64.     }
  65.     public void OnDestroy()
  66.     {
  67.         m_JobDatas.Dispose();
  68.         m_JobResults.Dispose();
  69.         m_NormalDatas = null;
  70.         m_NormalResults = null;
  71.     }
  72. }
复制代码
Job运算结果
正常运算结果

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2021-4-22 09:26 | 显示全部楼层
数学计算换上mathematics,job加上Burst Attribute。速度可以成百倍提升。
发表于 2021-4-22 09:30 | 显示全部楼层
更新一下测试结果。之前的测试只在Start方法中计算了一次,但在Update方法中发现,前期(程序开始几帧内)的消耗较大,但之后的消耗变得极小(10^7规模下,由180ms变为7ms),猜测JobSystem开始时做了初始化。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-20 05:50 , Processed in 0.101830 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表