Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ronronmx · Oct 19, 2012 at 04:47 AM · eventssendmessage

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Oct 19, 2012 at 08:31 AM 0
Share

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.

avatar image Kryptos · Oct 19, 2012 at 08:37 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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");    
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Oct 19, 2012 at 08:24 PM 0
Share

I'll give that a try and let you know the results. Thanks for the help nicolas!

avatar image ronronmx · Oct 21, 2012 at 12:11 AM 0
Share

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!

avatar image Kryptos · Oct 22, 2012 at 07:48 AM 0
Share

@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).

avatar image ronronmx · Oct 22, 2012 at 08:50 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges