Unity JobSystem和Mathematics数学库性能测试

先说结果,在不使用Job做优化的前提下,Mathematics数学库着实惊到我了,矩阵乘法快了近十倍

1.测试结果

  下图的测试数据是在2019.3.6环境下,模拟100万次4x4矩阵运算得出的,后面两个使用了job进行了优化,可以看到,新数学库的float4x4在不使用jobsystem的情况下就可以达到和Matrix4x4使用jobsystem计算时相当的速度。Mathematics使用了SIMD并行计算,有兴趣可以自行查阅。
  这里仅仅只测试了4x4矩阵在进行乘法运算的情况,不代表在实际使用中也可以达到这样的效率。
在这里插入图片描述

2.测试用例

  使用Package Manager安装数学库Mathematics。注意Jobs是集成在unity中的,不需要额外安装,Package Manager里的Jobs是提供了一些额外的Job类型。
  下面的代码放入unity后可以直接运行看结果。

using System;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

public class Test : MonoBehaviour
{
    public int count = 100000;

    void Start()
    {
        Debug.Log("Matrix4x4: " + DebugActionRunTime(() =>
        {
            for (int i = 0; i < count; i++)
            {
                Matrix4x4 aa = new Matrix4x4();
                Matrix4x4 bb = new Matrix4x4();
                Matrix4x4 result = aa * bb;
            }
        }) + "ms");

        Debug.Log("float4x4: " + DebugActionRunTime(() =>
        {
            for (int i = 0; i < count; i++)
            {
                float4x4 aa = new float4x4();
                float4x4 bb = new float4x4();
                float4x4 result = aa * bb;
            }
        }) + "ms");

        Debug.Log("Matrix4x4 Job: " + DebugActionRunTime(() =>
        {
            NativeArray<Matrix4x4> aa = new NativeArray<Matrix4x4>(count, Allocator.TempJob);
            NativeArray<Matrix4x4> bb = new NativeArray<Matrix4x4>(count, Allocator.TempJob);
            NativeArray<Matrix4x4> results = new NativeArray<Matrix4x4>(count, Allocator.TempJob);

            Matrix4x4Mul matrix4X4 = new Matrix4x4Mul();
            matrix4X4.aa = aa;
            matrix4X4.bb = aa;
            matrix4X4.results = results;
            matrix4X4.Schedule(results.Length, 5).Complete();

            aa.Dispose();
            bb.Dispose();
            results.Dispose();
        }) + "ms");

        Debug.Log("float4x4 Job: " + DebugActionRunTime(() =>
        {
            NativeArray<float4x4> aa = new NativeArray<float4x4>(count, Allocator.TempJob);
            NativeArray<float4x4> bb = new NativeArray<float4x4>(count, Allocator.TempJob);
            NativeArray<float4x4> results = new NativeArray<float4x4>(count, Allocator.TempJob);

            float4x4Mul matrix4X4 = new float4x4Mul();
            matrix4X4.aa = aa;
            matrix4X4.bb = aa;
            matrix4X4.results = results;
            matrix4X4.Schedule(results.Length, 5).Complete();

            aa.Dispose();
            bb.Dispose();
            results.Dispose();
        }) + "ms");
    }

    /// <summary>
    /// 执行一个方法并返回它的执行时间
    /// </summary>
    /// <param name="action"></param>
    /// <returns></returns>
    public static float DebugActionRunTime(Action action)
    {
        float time = DateTime.Now.Millisecond + DateTime.Now.Second * 1000;
        action();
        return DateTime.Now.Millisecond + DateTime.Now.Second * 1000 - time;
    }

    public struct float4x4Mul : IJobParallelFor
    {
        [ReadOnly] public NativeArray<float4x4> aa;
        [ReadOnly] public NativeArray<float4x4> bb;
        public NativeArray<float4x4> results;

        public void Execute(int index)
        {
            results[index] = bb[index] * results[index];
        }
    }

    public struct Matrix4x4Mul : IJobParallelFor
    {
        [ReadOnly] public NativeArray<Matrix4x4> aa;
        [ReadOnly] public NativeArray<Matrix4x4> bb;
        public NativeArray<Matrix4x4> results;

        public void Execute(int index)
        {
            results[index] = bb[index] * results[index];
        }
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值