Advertisement
Guest User

Class Member Access Performance Test

a guest
May 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 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 Awake()
  25.     {
  26.         IntString.Init(0, 1000);
  27.     }
  28.  
  29.     private void Update()
  30.     {
  31.         if (Input.GetKeyDown(KeyCode.Escape))
  32.             this.iterationCount += step;
  33.  
  34.         this.stopwatch.Reset();
  35.         this.stopwatch.Start();
  36.  
  37.         int healthValue = 0;
  38.  
  39.         for (int i = 0; i < iterationCount; i++)
  40.         {
  41.             healthValue = this.SomeAction();
  42.         }
  43.  
  44.         this.stopwatch.Stop();        
  45.  
  46.         this.iterationTxt.text = iterationCount.ToStringNonAlloc();
  47.         this.resultTxt.text = healthValue.ToStringNonAlloc();
  48.         this.timeTxt.text = ((int)stopwatch.ElapsedMilliseconds).ToStringNonAlloc();
  49.  
  50.     }
  51.  
  52.     private int SomeAction()
  53.     {
  54.         this.somePlayerClass.health = 10;
  55.         this.somePlayerClass.ExampleFunction();
  56.         int nHealth = this.somePlayerClass.health;      
  57.  
  58.         return nHealth;
  59.     }
  60. }
  61.  
  62.  
  63. [System.Serializable]
  64. public class PlayerClass
  65. {
  66.     public int health;
  67.  
  68.     public void ExampleFunction()
  69.     {
  70.         this.health = 0;
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement