Advertisement
Guest User

Class Member Access Performance Test Unity 3D

a guest
May 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using BB.Utility;
  6. using System.Diagnostics;
  7.  
  8. public class ClassAccessTest : MonoBehaviour
  9. {
  10.     public Text iterationTxt;
  11.  
  12.     public Text resultTxt;
  13.  
  14.     public Text timeTxt;
  15.  
  16.     public PlayerClass somePlayerClass;
  17.  
  18.     public int iterationCount;
  19.  
  20.     public int step = 1000;
  21.  
  22.     private Stopwatch stopwatch = new Stopwatch();
  23.  
  24.     private void Update()
  25.     {
  26.         if (Input.GetKeyDown(KeyCode.Escape))
  27.             this.iterationCount += step;
  28.  
  29.         this.stopwatch.Reset();
  30.         this.stopwatch.Start();
  31.  
  32.         int healthValue = 0;
  33.  
  34.         for (int i = 0; i < iterationCount; i++)
  35.         {
  36.             healthValue = this.SomeAction();
  37.         }
  38.  
  39.         this.stopwatch.Stop();        
  40.  
  41.         this.iterationTxt.text = iterationCount.ToStringNonAlloc();
  42.         this.resultTxt.text = healthValue.ToStringNonAlloc();
  43.         this.timeTxt.text = ((int)stopwatch.ElapsedMilliseconds).ToStringNonAlloc();
  44.  
  45.     }
  46.  
  47.     private int SomeAction()
  48.     {
  49.         this.somePlayerClass.health = 10;
  50.         this.somePlayerClass.ExampleFunction();
  51.         int nHealth = this.somePlayerClass.health;      
  52.  
  53.         return nHealth;
  54.     }
  55. }
  56.  
  57.  
  58. [System.Serializable]
  59. public class PlayerClass
  60. {
  61.     public int health;
  62.  
  63.     public void ExampleFunction()
  64.     {
  65.         this.health = 0;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement