flashgameblog.at

28.12.2009
14:22

Preventing function calls brings performance

 

It isn't a secret. The overhead of function calls decreases the performance of your programs. So prevent them where ever you can. In software design encapsulation and modularity are two major, but oppositional topics. Good design finds the well balanced middle.

With some lines of code from my current project I want to show you how to prevent function calls at the usage of event dispatching. In my project I have one class dispatching an event to a lot of other objects. Each of these objects has got an ID and each event is just for one of them.

The easy way to code this is to register each object for the event and in the object's event handler I check the ID saved in the event object and skip the handler if the ID is not corresponding to the ID in the event object.

 

  1. ...
  2. for ( var i:int = 0; i < 200000; i++) {
  3.    arReceivers.push( new Receiever(i) );
  4.    sender.addEventListener( "test", arReceivers[i].evt );
  5. }
  6. ...
  7.  
  8. ...
  9. public function evt(e:MyEvent):void {
  10.     if (e.id != this.id )
  11.         return;
  12.  
  13.     // the event is for me
  14. }
  15. ..

In my test code I have 200,000 receiver objects and this codes leads two 200,000 function calls.

A better way of coding is to register one "global" handler for the event and to find the corresponding receiver object in this handler.

  1. ...
  2. sender.addEventListener( "test", evt );
  3.  
  4. for ( var i:int = 0; i < 200000; i++) {
  5.    arReceivers.push( new Receiever(i) );
  6. }
  7. ...
  8.  
  9. ...
  10. public function evt(e:MyEvent):void {
  11.     for ( var i:int = 0; i < arReceivers.length; i++) {
  12.         if (e.id == arReceivers[i].id ){
  13.             arReceivers[i].evt(e);
  14.             break;
  15.         }
  16.     }
  17. }
  18. ...
  19.  

In this code just two function calls occur independent from the number of receiver objects.

 

This is the last post for 2009. I wish you a happy new year and keep curious in 2010 :-)

  •  
  • 0 Comments
  •  

Your comment

Notify me when someone adds another comment to this post

back

< < December 2009 > >
S M T W T F S
    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    

Blog rolls

Latest Comments

ISO Blender Camera Setup
24.04.2010 06:58
update
21.03.2010 01:15
if-Statement
16.11.2009 08:34
hmm...
16.11.2009 08:00

Archive

  • [-]2010(8)
    • [-]August(1)
    • [-]July(1)
    • [-]June(2)
    • [-]April(1)
    • [-]March(2)
    • [-]February(1)
  • [-]2009(13)
    • [-]December(4)
    • [-]November(8)
    • [-]October(1)

Copy and paste this link into your RSS news reader

RSS 0.91Posts
RSS 2.0Posts

Social Bookmarking

Bookmark bei: Mr. Wong Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Favoriten Bookmark bei: Seekxl Bookmark bei: Favit Bookmark bei: Social Bookmarking Tool Bookmark bei: Power Oldie Bookmark bei: Bookmarks.cc Bookmark bei: Newskick Bookmark bei: Newsider Bookmark bei: Linksilo Bookmark bei: Readster Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Del.icio.us Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz Information