- Home /
Looking for some tips on performance testing
Hey all!
I started doing some performance testing between the different ways available to call a function from another class. I wanted to know how much slower SendMessage() is then a direct call, and also tested the speed of the custom messaging system I'm using from the wiki.
To do the testing, I'm using CodeProfiler
void Start ()
{
bike = Managers.Bike;
bikeEngine = bike.GetComponent<BikeEngine>();
}
void Update()
{
CodeProfiler.Begin("SendMessage");
for( int i = 0; i < 800; i++ )
bikeEngine.SendMessage( "TestSpeed", SendMessageOptions.DontRequireReceiver );
CodeProfiler.End("SendMessage");
CodeProfiler.Begin("Broadcast");
for( int i = 0; i < 3000; i++ )
Messenger.Broadcast( "TestSpeed", MessengerMode.DONT_REQUIRE_LISTENER );
CodeProfiler.End("Broadcast");
CodeProfiler.Begin("DirectCall");
for( int i = 0; i < 3000; i++ )
bikeEngine.TestSpeed();
CodeProfiler.End("DirectCall");
}
With the code above, I get something like:
SendMessage 75.5% 45.10ms....
Broadcast 8.25% 4.5ms....
DirectCall 1.06% 1.0ms....
Now I do another test, and I only remove the first test block (the SendMessage one), and here are the aprx. results:
Broadcast 23.5% 20.512ms....
DirectCall 1.06% 1.0ms....
Why did "Broadcast" change? Are the % and ms based on 100%, so if in the first test, SendMessage was taking 75+%, the rest was being divided between the other calls? So without SendMessage, Broadcast appears to take more time but really is the same as before? Those numbers really confused me...if someone could shed some light, I will sleep a lot better tonight ah!
Thanks, Stephane
I would imagine Unity is doing some caching of the things that can accept your message - so in the first example Broadcast gets a benefit from Send$$anonymous$$essage having run previously.
That is also what I thought. Your explanation is a bit clearer than $$anonymous$$e.
I hope that this caching is freed each frame, so that my solution with coroutines can make sense.
Answer by Kryptos · Oct 19, 2012 at 08:18 AM
The problem with your test, is that everything is done within one frame. I guess, some "SendMessage" and "Broadcast" are just the same message in the end.
Try again but with only one message per frame, by using a coroutine:
IEnumerator Start()
{
// Initialization
bike = Managers.Bike;
bikeEngine = bike.GetComponent<BikeEngine>();
// SendMessage
CodeProfiler.Begin("SendMessage");
for( int i = 0; i < 800; i++ )
{
bikeEngine.SendMessage( "TestSpeed", SendMessageOptions.DontRequireReceiver );
yield return null;
}
CodeProfiler.End("SendMessage");
// Broadcast
CodeProfiler.Begin("Broadcast");
for( int i = 0; i < 3000; i++ )
{
Messenger.Broadcast( "TestSpeed", MessengerMode.DONT_REQUIRE_LISTENER );
yield return null;
}
CodeProfiler.End("Broadcast");
// DirectCall
CodeProfiler.Begin("DirectCall");
for( int i = 0; i < 3000; i++ )
{
bikeEngine.TestSpeed();
yield return null;
}
CodeProfiler.End("DirectCall");
}
I'll give that a try and let you know the results. Thanks for the help nicolas!
Nicolas, I tested the code in Start the way you described above( except I had to use while() loops with no returns because if yield return null, CodeProfiler returns a error that a matching End tag wasn't found). The test results were the same, with "Broadcast" being around 5-7% with "Send$$anonymous$$essage" being called first, and jumping to 23+% when I removed "Send$$anonymous$$essage" and just tested "Broadcast" and "DirectCall"...interesting isn't it?
I'm really not sure how to get accurate test results, maybe the code I'm using doesn't work that way. Can anyone point/show me how to properly run execution speed tests, manually, ins$$anonymous$$d of using a "tool" like I am?
Thanks guys!
@ronronmx Looks like you cannot use CodeProfiler to test using coroutine.
Try to make your own custom profiler by using Unity's timers (Time.time) and/or built-in timers (System.DateTime).
Do it for both configuration (with and without coroutine).
I think you're right $$anonymous$$ryptos, I'll have a look on the asset store first, maybe there's already a good profiler there I can use/buy. If not, then yeah, I'm stuck making my own :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Difference between Unity's messaging system and other custom messaging systems? 1 Answer
Attack a Target (using mouse over?) 1 Answer
Unity Editor Scripts: GameObject Added / Removed Event? 1 Answer
iPad touch scripting... 0 Answers