Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----- File: NullScriptableObject.cs -----
- using UnityEngine;
- using System.Collections;
- /**
- * This is a class made to mimic UnityEngine.Object's functionality when comparing objects.
- * We wrap it so we can replace their comparison functionality and check the speed of different operations.
- */
- public class NullScriptableObject : ScriptableObject
- {
- public bool unityFakeDestroy;
- public bool fastFakeDestroy;
- Object nullObj;
- Object notNull;
- void OnEnable()
- {
- nullObj = new GameObject( "Null" );
- Object.DestroyImmediate( nullObj, true );
- notNull = new GameObject( "NotNull" );
- }
- void OnDisable()
- {
- DestroyImmediate( nullObj );
- DestroyImmediate( notNull );
- }
- /**
- * We want to override Equals to mimic what UnityEngine.Object is doing
- */
- public override bool Equals (object o)
- {
- // This is the optimized destroy, which explicitly checks for null
- if ( fastFakeDestroy )
- {
- if (o==null)
- return true;
- else
- return nullObj.Equals(o);
- }
- // This is using Unity's Equals() for when an object is destroyed. It is slow.
- if ( unityFakeDestroy )
- return nullObj.Equals(o);
- // This is using Unity's Equals() for when an object is alive and well.
- return notNull.Equals(o);
- }
- /** Need to override this or you get a warning */
- public override int GetHashCode () { return base.GetHashCode (); }
- /**
- * I'm using operator overloading with the BASE System.Object type so comparing against generically typed null objects works
- */
- public static bool operator ==( NullScriptableObject lhs, object rhs )
- {
- return object.ReferenceEquals(lhs, rhs) || (!object.ReferenceEquals(lhs, null) && lhs.Equals(rhs));
- }
- /**
- * Must define != when defining ==
- */
- public static bool operator !=( NullScriptableObject lhs, object rhs )
- {
- return !(lhs == rhs);
- }
- }
- ----- New File - CompareSpeedTest.cs -----
- using UnityEngine;
- using System.Collections;
- /**
- * This is a class which should live in your scene. You right-click on it to use the Context-Menu item "Test Null Scriptable".
- * This will benchmark your implementation of the equals operator against null in different situations.
- */
- public class CompareSpeedTest : MonoBehaviour
- {
- [SerializeField] int _numCompares = 500000;
- [ContextMenu("Test Null Scriptable")]
- void TestNullScriptable()
- {
- object obj1 = new object();
- bool equal = false;
- float startTime = Time.realtimeSinceStartup;
- for (int i = 0 ; i < _numCompares ; ++i)
- {
- equal = (obj1 == null);
- }
- Debug.Log ( "C# Object Compare Is Null: " + equal + " Time Taken: " + (Time.realtimeSinceStartup - startTime) );
- //
- // Start the Null ScriptableObject
- //
- var nullSO = ScriptableObject.CreateInstance<NullScriptableObject>();
- startTime = Time.realtimeSinceStartup;
- for (int i = 0 ; i < _numCompares ; ++i)
- {
- equal = (null == nullSO); // This still calls operator==( NullScriptableObject, object )
- }
- Debug.Log ( "Null Scriptable Object Is Null: " + equal + " Time Taken: " + (Time.realtimeSinceStartup - startTime) );
- nullSO.unityFakeDestroy = true;
- startTime = Time.realtimeSinceStartup;
- for (int i = 0 ; i < _numCompares ; ++i)
- {
- equal = nullSO.Equals(null);
- }
- Debug.Log ( "Null Scriptable Object After Unity Destroy Is Null: " + equal + " Time Taken: " + (Time.realtimeSinceStartup - startTime) );
- nullSO.fastFakeDestroy = true;
- startTime = Time.realtimeSinceStartup;
- for (int i = 0 ; i < _numCompares ; ++i)
- {
- equal = nullSO.Equals(null);
- }
- Debug.Log ( "Null Scriptable Object After Fast Destroy Is Null: " + equal + " Time Taken: " + (Time.realtimeSinceStartup - startTime) );
- object realNull = null;
- Debug.Log ( "Test With Compare to RealNull: " + (nullSO == realNull) );
- DestroyImmediate( nullSO, true );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement