Advertisement
Guest User

Untitled

a guest
May 20th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using Unity.Collections;
  2. using Unity.Jobs;
  3. using UnityEngine;
  4. using System.Diagnostics;
  5. using System.Collections.Generic;
  6. using Unity.Burst;
  7.  
  8. public struct JobNoBurst : IJobParallelFor {
  9.     [ReadOnly]
  10.     public NativeArray<float> a;
  11.     [ReadOnly]
  12.     public NativeArray<float> b;
  13.     public NativeArray<float> result;
  14.     public void Execute(int i) {
  15.         result[i] = a[i] * b[i];
  16.     }
  17. }
  18.  
  19. [BurstCompile]
  20. public struct JobWithBurst : IJobParallelFor {
  21.     [ReadOnly]
  22.     public NativeArray<float> a;
  23.     [ReadOnly]
  24.     public NativeArray<float> b;
  25.     public NativeArray<float> result;
  26.     public void Execute(int i) {
  27.         result[i] = a[i] * b[i];
  28.     }
  29. }
  30.  
  31. public class JobSystemTest : MonoBehaviour {
  32.     public int NumberOfTestsToRun = 10;
  33.     public int NumberOfArraysElements = 1000000;
  34.     public int BatchSize = 32;
  35.     public float ValueOfArrayA = 0.5f;
  36.     public float ValueOfArrayB = 1.5f;
  37.     private List<long> NoBurstResults = new List<long>();
  38.     private List<long> WithBurstResults = new List<long>();
  39.  
  40.     private void Awake() {
  41.         QualitySettings.vSyncCount = 0;
  42.     }
  43.  
  44.     private void Update() {
  45.         if (Input.GetKeyDown(KeyCode.Space)) {
  46.             ParallelArrayOperation();
  47.         }
  48.     }
  49.  
  50.     private void ParallelArrayOperation() {
  51.         UnityEngine.Debug.Log($"Summing two arrays of {NumberOfArraysElements} elements for {NumberOfTestsToRun} times");
  52.         NoBurstResults.Clear();
  53.         WithBurstResults.Clear();
  54.         for (int i = 0; i < NumberOfTestsToRun; i++) {
  55.             ExecuteJob<JobNoBurst>();
  56.         }
  57.         for (int i = 0; i < NumberOfTestsToRun; i++) {
  58.             ExecuteJob<JobWithBurst>();
  59.         }
  60.         long averageNoBurst = 0;
  61.         long averageWithBurst = 0;
  62.         for (int i = 0; i < NumberOfTestsToRun; i++) {
  63.             averageNoBurst += NoBurstResults[i];
  64.             averageWithBurst += WithBurstResults[i];
  65.         }
  66.         double averageMsNoBurst = (double)(averageNoBurst * 1000L) / (NumberOfTestsToRun * Stopwatch.Frequency);
  67.         double averageMsWithBurst = (double)(averageWithBurst * 1000L) / (NumberOfTestsToRun * Stopwatch.Frequency);
  68.  
  69.         UnityEngine.Debug.Log($"Average execution time - BURST OFF: {averageMsNoBurst}");
  70.         UnityEngine.Debug.Log($"Average execution time - BURST ON: {averageMsWithBurst}");
  71.  
  72.         float percentageDiff = (float)(((averageMsNoBurst / averageMsWithBurst) - 1) * 100);
  73.         UnityEngine.Debug.Log($"Burst compiling was {percentageDiff}% faster");
  74.     }
  75.  
  76.     private void ExecuteJob<T>() where T : IJobParallelFor {
  77.         var nativeA = new NativeArray<float>(NumberOfArraysElements, Allocator.TempJob);
  78.         var nativeB = new NativeArray<float>(NumberOfArraysElements, Allocator.TempJob);
  79.         var nativeResult = new NativeArray<float>(NumberOfArraysElements, Allocator.TempJob);
  80.         for (int i = 0; i < nativeA.Length; i++) {
  81.             nativeA[i] = ValueOfArrayA;
  82.             nativeB[i] = ValueOfArrayB;
  83.         }
  84.         if (typeof(T) == typeof(JobNoBurst)) {
  85.             var job = new JobNoBurst();
  86.             job.a = nativeA;
  87.             job.b = nativeB;
  88.             job.result = nativeResult;
  89.             Stopwatch sw = Stopwatch.StartNew();
  90.             var handle = job.Schedule(nativeResult.Length, BatchSize);
  91.             handle.Complete();
  92.             sw.Stop();
  93.             job.a.Dispose();
  94.             job.b.Dispose();
  95.             job.result.Dispose();
  96.             NoBurstResults.Add(sw.ElapsedTicks);
  97.         }
  98.         else {
  99.             var job = new JobWithBurst();
  100.             job.a = nativeA;
  101.             job.b = nativeB;
  102.             job.result = nativeResult;
  103.             Stopwatch sw = Stopwatch.StartNew();
  104.             var handle = job.Schedule(nativeResult.Length, BatchSize);
  105.             handle.Complete();
  106.             sw.Stop();
  107.             job.a.Dispose();
  108.             job.b.Dispose();
  109.             job.result.Dispose();
  110.             WithBurstResults.Add(sw.ElapsedTicks);
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement