Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using UnityEngine;
- public class HuiPizda : MonoBehaviour {
- Bench b1 = new Bench();
- Bench b2 = new Bench();
- Bench b3 = new Bench();
- Action<int> huiAction;
- float tick = 1;
- public int start = 5000000;
- public int upd = 10000;
- void Start(){
- b1.sw.Start();
- for (int i = 0; i < start; i++) {
- SendMessage("Hui", i);
- }
- b1.Tick();
- UnityEngine.Debug.LogFormat("Message:\t{0}ms", b1.Avg);
- b2.sw.Start();
- for (int i = 0; i < start; i++) {
- Hui(i);
- }
- b2.Tick();
- UnityEngine.Debug.LogFormat("Call:\t{0}ms\n", b2.Avg);
- huiAction += Hui;
- b3.sw.Start();
- for (int i = 0; i < start; i++) {
- huiAction(i);
- }
- b3.Tick();
- UnityEngine.Debug.LogFormat("Delegate:\t{0}ms\n", b3.Avg);
- b1.Reset();
- b2.Reset();
- b3.Reset();
- }
- void Update() {
- tick -= Time.deltaTime;
- b1.sw.Start();
- for (int i = 0; i < upd; i++) {
- SendMessage("Hui", i);
- }
- b1.Tick();
- b2.sw.Start();
- for (int i = 0; i < upd; i++) {
- Hui(i);
- }
- b2.Tick();
- b3.sw.Start();
- for (int i = 0; i < upd; i++) {
- huiAction(i);
- }
- b3.Tick();
- if (tick <= 0f) {
- UnityEngine.Debug.LogFormat("Message:\t{0}ms", b1.Avg);
- UnityEngine.Debug.LogFormat("Call:\t{0}ms", b2.Avg);
- UnityEngine.Debug.LogFormat("Delegate:\t{0}ms", b3.Avg);
- UnityEngine.Debug.LogFormat("fps:\t{0}\n", b2.count);
- b1.Reset();
- b2.Reset();
- b3.Reset();
- tick = 1;
- }
- }
- void Hui(int i) {
- double x = UnityEngine.Random.Range(float.MinValue, float.MaxValue);
- x += Math.Pow(x / 123, 3) - i;
- }
- class Bench {
- public Stopwatch sw = new Stopwatch();
- public float tick = 1f;
- public long ms = 0;
- public int count = 0;
- public float Avg {
- get {
- return ms / count;
- }
- }
- public void Tick() {
- sw.Stop();
- ms += sw.ElapsedMilliseconds;
- ++count;
- sw.Reset();
- }
- public void Reset() {
- tick = 1f;
- count = 0;
- ms = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement