home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,984 of 33,346   
   Krzysztof =?UTF-8?B?xbtlbGVjaG93c2t to All   
   Re: How to avoid repeated run-time looku   
   03 Mar 12 12:11:12   
   
   8438ed98   
   From: giecrilj@stegny.2a.pl   
      
   GH wrote:   
      
   > Hi all, I am trying to optimize a piece of code that looks like the   
   > following:   
   >   
   > struct Game;   
   >   
   > struct PlayerBase { virtual void play(Game*) const = 0; ... };   
   >   
   > template struct Player : PlayerBase { virtual   
   > void play(Game*) const; ... };   
   >   
   > struct Game {   
   >  bool gameOver();   
   >  void start(const vector& players) {   
   >    while (! gameOver()) { // (1)   
   >      for (vector::const_iterator i=players.begin(); i!   
   > =players.end(); ++i)  // (2)   
   >        (*i)->play(this);  // (3)   
   >    }   
   >  }   
   > };   
   >   
   > At (3), there is some overhead because of the virtual function play(),   
   > which is quite significant when play() itself is simple. But the   
   > runtime resolution for the virtual function for each element in the   
   > vector needs to be done only once at the first iteration of loop (1).   
   > Is there a good way to achieve that?   
   >   
   >   
      
   In general, you have to ask the first player: how would you play this?  This   
   is a virtual question, and the result will be a simple function pointer that   
   takes a player and the context, or a member function pointer.  You use the   
   pointer within the loop.   
      
   However, this means that the players must be redesigned to support this   
   protocol.   
      
   HTH,   
   Chris   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca