views:

101

answers:

1

I am writing an automation client in Visual C++ with MFC. If I right-click on my solution » Add » Class, I have the option to select MFC Class From TypeLib. Selecting this option generates source/header files for all interfaces. This allows me to write code such as:

#include "CApplication.h"
#include "CDocument.h"

// ... connect to automation server ...
CApplication *myApp = new CApplication(pDisp);
CDocument myDoc = myApp->get_ActiveDocument();

Using this method, my benchmarking function that makes about 12000 automation calls takes 1 second.

Meanwhile, the following code:

#import "progid:Library.Application"

Library::IApplicationPtr myApp;
// ... connect to automation server ...
Library::IDocumentPtr myDoc = myApp->GetActiveDocument();

takes about 2.4 seconds for the same benchmark. I assume the smart-pointer implementation is slowing me down, but I don't know why. Even worse, I'm not sure how to use #import construct to achieve the speeds that the first method yields. Is this possible? How or why not?

Thanks for your time!

A: 

Update:

Apparently the first method is late-bound while the second is early-bound. I've always heard that early-binding was the way to go for performance, but for some reason, that's not true in this case. I've verified these findings by creating clients in C#. In my first test, I did the following:

(My 3rd party automation server has a reference namespace of ABC)

ABC.Object objectCollection = ABC.get_Objects()
foreach (var object in objectCollection) 
{
    // do something with object
}

The code took roughly 2.4s to execute, like the C++ version. However, if I use C# 4.0's dynamic keyword like so:

dynamic objectCollection = ABC.get_Objects()
foreach (dynamic object in objectCollection)
{
    // do something with object
}

It executes in about 1s. I'd love if anyone has an explanation for why this happens.

Pakman