Unity C# Job System - 2

安装

1.

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

// Job adding two floating point values together
public struct MyJob : IJob
{
    public float a;
    public float b;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = a + b;
    }
}

public class CreateJobTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // Create a native array of a single float to store the result. This example waits for the job to complete for illustration purposes
        NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);

        // Set up the job data
        MyJob jobData = new MyJob();
        jobData.a = 10;
        jobData.b = 10;
        jobData.result = result;

        // Schedule the job
        JobHandle handle = jobData.Schedule();

        // Wait for the job to complete
        handle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];
        Debug.Log("Result:" + aPlusB);  // 20
        // Free the memory allocated by the result array
        result.Dispose();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

2.

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

// Job adding two floating point values together
public struct AddJob : IJob
{
    public float a;
    public float b;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = a + b;
    }
}

// Job multiply one float value by another
public struct MulResultJob : IJob
{
    public float value;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = result[0] * value;
    }
}

// Job adding two floating point values together
public struct AddResultJob : IJob
{
    public float value;
    public NativeArray<float> result;

    public void Execute()
    {
        result[0] = result[0] + value;
    }
}

public class JobDependencyTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // Create a native array of a single float to store the result in. This example waits for the job to complete
        NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob);

        // Setup the data for job #1
        AddJob jobData = new AddJob();
        jobData.a = 10;
        jobData.b = 10;
        jobData.result = result;

        // Schedule job #1
        JobHandle firstHandle = jobData.Schedule();

        // Setup the data for job #2
        MulResultJob mulJobData = new MulResultJob();
        mulJobData.value = 5;
        mulJobData.result = result;

        // Schedule job #2
        JobHandle secondHandle = mulJobData.Schedule(firstHandle);//firstHandle

        // job #3
        AddResultJob addJobData = new AddResultJob();
        addJobData.value = 9;
        addJobData.result = result;
        JobHandle thirdHandle = addJobData.Schedule(secondHandle);//secondHandle

        // Wait for job #3 to complete
        thirdHandle.Complete();

        // All copies of the NativeArray point to the same memory, you can access the result in "your" copy of the NativeArray
        float aPlusB = result[0];
        Debug.Log("Result:" + aPlusB);  // 109

        // Free the memory allocated by the result array
        result.Dispose();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

3.

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

// Job adding two floating point values together
[Unity.Burst.BurstCompile]
public struct MyParallelJob : IJobParallelFor
{
    [ReadOnly]
    public NativeArray<float> a;
    [ReadOnly]
    public NativeArray<float> b;

    public NativeArray<float> result;

    public void Execute(int i)
    {
        //result[i] = Mathf.Pow(a[i],10) * Mathf.Pow(b[i],10);
        result[i] = Mathf.Sqrt(Mathf.Abs(Mathf.Sin(a[i]) * Mathf.Cos(b[i])));
    }
}

public class ParallelForJobTest : MonoBehaviour
{
    public int calcSize = 100000000;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(10, 20, 100, 40), "Parallel Calc"))
        {
            ParallelCalc();
        }

        if (GUI.Button(new Rect(10, 80, 100, 40), "Direct Calc"))
        {
            DirectCalc();
        }
    }

    private void ParallelCalc()
    {
        // record start time
        float startTime = Time.realtimeSinceStartup;
        NativeArray<float> a = new NativeArray<float>(calcSize, Allocator.TempJob);

        NativeArray<float> b = new NativeArray<float>(calcSize, Allocator.TempJob);

        NativeArray<float> result = new NativeArray<float>(calcSize, Allocator.TempJob);

        float startTime1 = Time.realtimeSinceStartup;
        for (int i = 0; i < calcSize; i++)
        {
            a[i] = 0.005f * i;
            b[i] = 0.007f * i;
        }

        MyParallelJob jobData = new MyParallelJob();
        jobData.a = a;
        jobData.b = b;
        jobData.result = result;

        // Schedule the job with one Execute per index in the results array and only 1 item per processing batch
        JobHandle handle = jobData.Schedule(result.Length, 100);
        // Wait for the job to complete
        handle.Complete();
        // record end time
        float endTime = Time.realtimeSinceStartup;
        //Debug.Log("TotalCount:" + result.Length);
        float temp = result[calcSize - 1];
        Debug.Log("The last Value:" + temp);
        Debug.Log("TotalCount:" + result.Length + ",Parallel Use Time:" + (endTime - startTime) + ",Init Use Time:" + (startTime1 - startTime));

        // Free the memory allocated by the arrays
        a.Dispose();
        b.Dispose();
        result.Dispose();
    }

    private void DirectCalc()
    {
        List<float> result = new List<float>();
        float tempA, tempB;
        float startTime = Time.realtimeSinceStartup;
        for (int i = 0; i < calcSize; i++)
        {
            tempA = 0.005f * i;
            tempB = 0.007f * i;
            result.Add(Mathf.Sqrt(Mathf.Abs(Mathf.Sin(tempA) * Mathf.Cos(tempB))));
        }
        float endTime = Time.realtimeSinceStartup;
        float temp = result[calcSize - 1];
        Debug.Log("The last Value:" + temp);
        Debug.Log("TotalCount:" + result.Count + ",Direct Use Time:" + (endTime - startTime));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值