summaryrefslogtreecommitdiff
path: root/ActiveRagdoll/Assets/ThirdParty/SerializableCallback/Runtime/Test.cs
blob: 2c258bac8999e3e96ef003f315b693accfc7fb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Diagnostics;
using UnityEngine;

public class Test : MonoBehaviour {
	const int ITERATIONS = 100000;
	public float f = 0.5f;
	public string s;
	public System.Func<float, bool> RegularDelegate;
	public System.Func<float, bool> DynamicDelegate;
	public Condition condition;
	public SerializableEvent ev;

	void Start() {
		RegularDelegate = TestMethod;
		DynamicDelegate = (System.Func<float, bool>) System.Delegate.CreateDelegate(typeof(System.Func<float, bool>), this, "TestMethod");
		condition.Invoke(f);
	}

	void Update() {
		var method = Stopwatch.StartNew();
		bool methodb = false;
		for (int i = 0; i < ITERATIONS; ++i) {
			methodb = TestMethod(f);
		}
		method.Stop();

		var regularDelegate = Stopwatch.StartNew();
		bool regularDelegateb = false;
		for (int i = 0; i < ITERATIONS; ++i) {
			regularDelegateb = RegularDelegate(f);
		}
		regularDelegate.Stop();

		var dynamicDelegate = Stopwatch.StartNew();
		bool dynamicDelegateb = false;
		for (int i = 0; i < ITERATIONS; ++i) {
			dynamicDelegateb = DynamicDelegate(f);
		}
		dynamicDelegate.Stop();

		var serializedDelegate = Stopwatch.StartNew();
		bool serializedDelegateb = false;
		for (int i = 0; i < ITERATIONS; ++i) {
			serializedDelegateb = condition.Invoke(f);
		}
		serializedDelegate.Stop();

		var serializedEvent = Stopwatch.StartNew();
		for (int i = 0; i < ITERATIONS; ++i) {
			ev.Invoke();
		}
		serializedEvent.Stop();

		UnityEngine.Debug.Log("Method: " + methodb + method.Elapsed);
		UnityEngine.Debug.Log("RegularDelegate: " + regularDelegateb + regularDelegate.Elapsed);
		UnityEngine.Debug.Log("DynamicDelegate: " + dynamicDelegateb + dynamicDelegate.Elapsed);
		UnityEngine.Debug.Log("SerializedCallback: " + serializedDelegateb + serializedDelegate.Elapsed);
		UnityEngine.Debug.Log("SerializedEvent: " + serializedEvent.Elapsed);
	}

	public bool TestMethod(float f) {
		return f > 0.5f;
	}

	public bool TestMethod(string a) {
		return string.IsNullOrEmpty(a);
	}

	public bool TestMethod2(float f, string a) {
		return f > 0.5f && string.IsNullOrEmpty(a);
	}

	public void TestMethod2(string a) {
		s = a;
	}
}

[Serializable]
public class Condition : SerializableCallback<float, bool> { }