Win32 Message Pump vs MFC Message Maps which is faster? C ++
They can't be any faster as they are just wrappers around regular Windows messages. However, they help a lot with reflected messages and message encapsulation for custom controls, and the overhead is negligible, so it might be worth it in the end if you have a complex UI project.
a source to share
I have to disagree with (for now) two other respondents who argue that the MFC message pump is less efficient simply because it wraps plain old C messages: MFC uses a completely different technique than a simple window run containing a giant switch (message) ...
The MFC message pump really relies on the Win32 message loop (it should). But the implementation is very different: based on message interceptions, dispatch messages handled internally by MFC, instead of relying on the DispatchMessage () API.
MFC uses maps to map messages to handlers: O (log n). From this point of view, there might be cases where it is faster than a large and poorly compiled switch (message): O (n) statement.
Also, it would be faster to identify the correct window object than DispatchMessage (), which we cannot know for sure since Windows is not open source.
This is unlikely, however, especially considering the extra code like command routing, idle handling, and various corner cases handled by MFC code ... and the fact that compilers are smart enough to implement large switch()
statements efficiently !
As such, the performance hit hasn't been considered significant for over 15 years.
a source to share
Serge wrote:
The MFC message pump really relies on the Win32 message loop (it should). But the implementation is very different: based on hooks, dispatching messages handled by MFC instead of relying on the DispatchMessage () API.
I'm not sure if this is correct. If you look into the MFC file thrdcore.cpp , you will see this function:
BOOL AFXAPI AfxInternalPumpMessage()
{
_AFX_THREAD_STATE *pState = AfxGetThreadState();
.......... snip ...........
// process this message
if (pState->m_msgCur.message != WM_KICKIDLE
&& !AfxPreTranslateMessage(&(pState->m_msgCur)))
{
::TranslateMessage(&(pState->m_msgCur));
::DispatchMessage(&(pState->m_msgCur));
}
return TRUE;
}
This function is called by the MFC function PumpMessage , and as you can see, it uses the Win32 DispatchMessage API just like any other Win32 application.
a source to share