The Artima Developer Community
Sponsored Link

Weblogs Forum
Static versus Dynamic Event Handlers in C++

4 replies on 1 page. Most recent reply: Dec 22, 2004 11:14 AM by Christopher Diggins

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Static versus Dynamic Event Handlers in C++ (View in Weblogs)
Posted: Dec 20, 2004 10:35 PM
Reply to this message Reply
Summary
I show how static event handlers through template function specializations can be simpler and more efficient than dynamic event handlers through function pointers.
Advertisement

Often in event driven frameworks, functions pointers are used, to allow the user of a library to respond to particular events generated by a library. If we know the event handlers to be static (i.e. not changing during program execution) then we can use another simpler and more efficient technique based on template function specializations. This post is intended to replace the somewhat inelegant previous post I made. on the technique.

I have also demonstrated the technique in action in a minimal Win32 GUI application at http://www.codeproject.com/useritems/winevent.asp

Here is a C++ program which shows the two techniques side by side:

#include <stdio.h>
#include <time.h>

const int BIG_NUM = 100000000;

const int EVENT_A = 0; 
const int EVENT_B = 1; 

int gnCnt = 0;

/////////////////////////////////////////////////
// static dispatch code

template<typename int> 
inline bool StaticHandler(int nArg) {
  return false;
}

template<typename Dummy_T>
void StaticDispatch() {
  while (StaticHandler<EVENT_A>(BIG_NUM)) {
    StaticHandler<EVENT_B>(1);
  }
}

/////////////////////////////////////////////////
// dynamic dispatch code

typedef bool (*HandlerFxn)(int);

bool DefaultDynamicHandler(int nArg) {
  return false;
}

HandlerFxn FxnPtrTable[2] = { 
  &DefaultDynamicHandler, 
  &DefaultDynamicHandler 
}; 

void RegisterHandler(int EventCode, HandlerFxn pFxn) {
  FxnPtrTable[EventCode] = pFxn;  
}

void DynamicDispatch() {  
  while (FxnPtrTable[EVENT_A](BIG_NUM)) {
    FxnPtrTable[EVENT_B](1);
  }
}

/////////////////////////////////////////////////
// dynamic user-defined event handlers 

inline bool DynamicHandlerA(int nArg) {
  return gnCnt < nArg;    
}

inline bool DynamicHandlerB(int nArg) {
  gnCnt += nArg;
  return true;
}

/////////////////////////////////////////////////
// static user-defined event handlers 

template<>
inline bool StaticHandler<EVENT_A>(int nArg) {
  return gnCnt < nArg;
}

template<>
inline bool StaticHandler<EVENT_B>(int nArg) {
  gnCnt += nArg;
  return true;
}

/////////////////////////////////////////////////
// main entry point

int main()
{
  int nStart;
  int nEnd;
  
  {
    gnCnt = 0;
    RegisterHandler(EVENT_A, &DynamicHandlerA);
    RegisterHandler(EVENT_B, &DynamicHandlerB);
    nStart = clock();
    DynamicDispatch();    
    nEnd = clock();
    printf("time elapsed for dynamic dispatch %d msec\n", (nEnd - nStart) * CLOCKS_PER_SEC / 1000);
  }
  
  {
    gnCnt = 0;
    nStart = clock();
    StaticDispatch<void>(); 
    nEnd = clock();
    printf("time elapsed for static dispatch %d msec\n", (nEnd - nStart) * CLOCKS_PER_SEC / 1000);
  }
  getchar();    
	return 0;
}

On my system (Windows XP) and compiler (Visual C++ 7.1) I got results of roughly 2000 msec for dynamic dispatch versus 1500 msec for static dispatch. This is not surprising considering that the compiler can much more easily optimize the template calls. Compilers are not good at guessing when dynamic contexts are in fact static. This is what programmers are for.

Notice that the static dispatch system requires far less code and does not need any kind of registration step.


Jeff Smith

Posts: 2
Nickname: smithjt
Registered: Dec, 2004

Re: Static versus Dynamic Event Handlers in C++ Posted: Dec 22, 2004 10:43 AM
Reply to this message Reply
I cannot argue that the template driven method looks cleaner, but the speed proof is broken in several ways. First, using only one sample as a basis for comparison as opposed to running through a loop with interrupts disabled is questionable. Second, the speed results themselves fail the sanity check, even though the proper ratio between scores is maintained. You should -divide- the clock delta by CLOCKS_PER_SEC and -multiply- by 1000 (ms/s) to have the units balance to milisecond delta. Right now it is clocks squared per 1000 milliseconds, and 1.5 to 2 seconds delay is simply unbelievable. I would like to see a fixed proof, since this is really a very good and interesting premise.

Jeff Smith

Posts: 2
Nickname: smithjt
Registered: Dec, 2004

Re: Static versus Dynamic Event Handlers in C++ Posted: Dec 22, 2004 10:49 AM
Reply to this message Reply
Whoops. Actually looking at the code, there is a substantial loop occuring in the handler, so I'm full of it. I just looked at main and the results and (not knowing even the order of magnitude of CLOCKS_PER_SEC) assumed the test itself was broken. My apologies!

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Static versus Dynamic Event Handlers in C++ Posted: Dec 22, 2004 11:13 AM
Reply to this message Reply
> My apologies!

No problem, I appreciate your interest in my work, and that you took the time to look at it closely.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Static versus Dynamic Event Handlers in C++ Posted: Dec 22, 2004 11:14 AM
Reply to this message Reply
> My apologies!

No problem, I appreciate your interest in my work, and that you took the time to look at it closely.

Flat View: This topic has 4 replies on 1 page
Topic: Event Driven Programming using Template Specializations in C++ Previous Topic   Next Topic Topic: Service Oriented Architectures - Separating Hype From Reality

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use