Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4.  
  5. public class HuiPizda : MonoBehaviour {
  6.  
  7.     Bench b1 = new Bench();
  8.     Bench b2 = new Bench();
  9.     Bench b3 = new Bench();
  10.  
  11.     Action<int> huiAction;
  12.  
  13.     float tick = 1;
  14.     public int start = 5000000;
  15.     public int upd = 10000;
  16.  
  17.     void Start(){
  18.         b1.sw.Start();
  19.         for (int i = 0; i < start; i++) {
  20.             SendMessage("Hui", i);
  21.         }
  22.         b1.Tick();
  23.         UnityEngine.Debug.LogFormat("Message:\t{0}ms", b1.Avg);
  24.  
  25.         b2.sw.Start();
  26.         for (int i = 0; i < start; i++) {
  27.             Hui(i);
  28.         }
  29.         b2.Tick();
  30.         UnityEngine.Debug.LogFormat("Call:\t{0}ms\n", b2.Avg);
  31.  
  32.         huiAction += Hui;
  33.         b3.sw.Start();
  34.         for (int i = 0; i < start; i++) {
  35.             huiAction(i);
  36.         }
  37.         b3.Tick();
  38.         UnityEngine.Debug.LogFormat("Delegate:\t{0}ms\n", b3.Avg);
  39.  
  40.         b1.Reset();
  41.         b2.Reset();
  42.         b3.Reset();
  43.     }
  44.  
  45.     void Update() {
  46.         tick -= Time.deltaTime;
  47.  
  48.         b1.sw.Start();
  49.         for (int i = 0; i < upd; i++) {
  50.             SendMessage("Hui", i);
  51.         }
  52.         b1.Tick();
  53.  
  54.         b2.sw.Start();
  55.         for (int i = 0; i < upd; i++) {
  56.             Hui(i);
  57.         }
  58.         b2.Tick();
  59.  
  60.         b3.sw.Start();
  61.         for (int i = 0; i < upd; i++) {
  62.             huiAction(i);
  63.         }
  64.         b3.Tick();
  65.  
  66.         if (tick <= 0f) {
  67.             UnityEngine.Debug.LogFormat("Message:\t{0}ms", b1.Avg);
  68.             UnityEngine.Debug.LogFormat("Call:\t{0}ms", b2.Avg);
  69.             UnityEngine.Debug.LogFormat("Delegate:\t{0}ms", b3.Avg);
  70.             UnityEngine.Debug.LogFormat("fps:\t{0}\n", b2.count);
  71.             b1.Reset();
  72.             b2.Reset();
  73.             b3.Reset();
  74.             tick = 1;
  75.         }
  76.     }
  77.  
  78.     void Hui(int i) {
  79.         double x = UnityEngine.Random.Range(float.MinValue, float.MaxValue);
  80.         x += Math.Pow(x / 123, 3) - i;
  81.     }
  82.  
  83.     class Bench {
  84.         public Stopwatch sw = new Stopwatch();
  85.         public float tick = 1f;
  86.         public long ms = 0;
  87.         public int count = 0;
  88.  
  89.         public float Avg {
  90.             get {
  91.                 return  ms / count;
  92.             }
  93.         }
  94.  
  95.         public void Tick() {
  96.             sw.Stop();
  97.             ms += sw.ElapsedMilliseconds;
  98.             ++count;
  99.             sw.Reset();
  100.         }
  101.  
  102.         public void Reset() {
  103.             tick = 1f;
  104.             count = 0;
  105.             ms = 0;
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement