这类程序使用宿主EXE(word)的运行空间和运行时间,和vb,vc没有什么大关系。you program have bug.唯一可能的原因就是测试环境不均等。 if word.exe is running, all will be very fast. if word.exe is not running, all will be slow. reason: more than one word.exe instance share same code page(virtual memory)
word 本身就支持vba,我想应该使用vb快
Word中的宏都是用VB做的这说明VB和word有密切联系!在这种情况应该就一种近亲关系吧!
你的计时是不是从Private Sub Command1_Click()这里开始的, VC的计时从那里开始,最好有一点点的VC代码。本人对VB不懂。
VB的代码是这样的,从点击Command1开始计时 Private Sub Command1_Click() Print Time() Dim tkWord As Object Set tkWord = CreateObject("Word.Application") With tkWord .Documents.Add .Selection.TypeText Text:="one" .Selection.TypeParagraph .Selection.TypeText Text:="two" .Selection.TypeParagraph .Selection.TypeText Text:="three" .Selection.TypeText Text:="周大雁我喜欢你" .ActiveDocument.SaveAs FileName:="c:\Doc1.doc", _ FileFormat:=wdFormatDocument, LockComments:=False, _ Password:="", AddToRecentFiles:=True, WritePassword _ :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, SaveFormsData:=False, _ SaveAsAOCELetter:=False End With tkWord.Quit Print Time()
End Sub win32 console的计时代码如下: char tbuffer[10]; _strtime(tbuffer); printf("%s\n",tbuffer); DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0}; VARIANT vResult; OLECHAR FAR* szFunction; BSTR bstrTemp; // IDispatch pointers for Word's objects IDispatch* pDispDocs; //Documents collection IDispatch* pDispSel; //Selection object IDispatch* pDispActiveDoc; //ActiveDocument object // DISPIDs DISPID dispid_Docs; //Documents property of Application //object DISPID dispid_DocsAdd; //Add method of Documents collection //object DISPID dispid_Sel; //Selection property of Application //object DISPID dispid_TypeText; //TypeText method of Selection object DISPID dispid_TypePara; //TypeParagraph method of Selection //object DISPID dispid_ActiveDoc; //ActiveDocument property of //Application object DISPID dispid_SaveAs; //SaveAs method of the Document object DISPID dispid_Quit; //Quit method of the Application //object // ******************** Start Automation *********************** //Initialize the COM libraries ::CoInitialize(NULL); 。。 。 pDispSel->Release(); pDispDocs->Release(); pDispApp->Release(); pUnk->Release(); ::CoUninitialize(); _strtime(tbuffer); printf("%s\n",tbuffer); win32 application的计时代码如下: case WM_LBUTTONDOWN: char tbuffer[10]; _strtime(tbuffer); hdc=GetDC(hWnd); TextOut(hdc,0,0,tbuffer,10); //printf("%s\n",tbuffer);
if word.exe is running, all will be very fast.
if word.exe is not running, all will be slow. reason: more than one word.exe instance share same code page(virtual memory)
VC的计时从那里开始,最好有一点点的VC代码。本人对VB不懂。
Private Sub Command1_Click()
Print Time()
Dim tkWord As Object
Set tkWord = CreateObject("Word.Application")
With tkWord
.Documents.Add
.Selection.TypeText Text:="one"
.Selection.TypeParagraph
.Selection.TypeText Text:="two"
.Selection.TypeParagraph
.Selection.TypeText Text:="three"
.Selection.TypeText Text:="周大雁我喜欢你"
.ActiveDocument.SaveAs FileName:="c:\Doc1.doc", _
FileFormat:=wdFormatDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False End With
tkWord.Quit
Print Time()
End Sub
win32 console的计时代码如下:
char tbuffer[10];
_strtime(tbuffer);
printf("%s\n",tbuffer);
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
VARIANT vResult;
OLECHAR FAR* szFunction;
BSTR bstrTemp; // IDispatch pointers for Word's objects
IDispatch* pDispDocs; //Documents collection
IDispatch* pDispSel; //Selection object
IDispatch* pDispActiveDoc; //ActiveDocument object // DISPIDs
DISPID dispid_Docs; //Documents property of Application
//object
DISPID dispid_DocsAdd; //Add method of Documents collection
//object
DISPID dispid_Sel; //Selection property of Application
//object
DISPID dispid_TypeText; //TypeText method of Selection object
DISPID dispid_TypePara; //TypeParagraph method of Selection
//object
DISPID dispid_ActiveDoc; //ActiveDocument property of
//Application object
DISPID dispid_SaveAs; //SaveAs method of the Document object
DISPID dispid_Quit; //Quit method of the Application
//object // ******************** Start Automation *********************** //Initialize the COM libraries
::CoInitialize(NULL);
。。
。
pDispSel->Release();
pDispDocs->Release();
pDispApp->Release();
pUnk->Release(); ::CoUninitialize();
_strtime(tbuffer);
printf("%s\n",tbuffer);
win32 application的计时代码如下: case WM_LBUTTONDOWN:
char tbuffer[10];
_strtime(tbuffer);
hdc=GetDC(hWnd);
TextOut(hdc,0,0,tbuffer,10);
//printf("%s\n",tbuffer);
VARIANT vResult;
OLECHAR FAR* szFunction;
BSTR bstrTemp; // IDispatch pointers for Word's objects
IDispatch* pDispDocs; //Documents collection
IDispatch* pDispSel; //Selection object
IDispatch* pDispActiveDoc; //ActiveDocument object // DISPIDs
DISPID dispid_Docs; //Documents property of Application
//object
DISPID dispid_DocsAdd; 。。
pDispApp->Release();
pUnk->Release(); ::CoUninitialize();
_strtime(tbuffer);
TextOut(hdc,0,20,tbuffer,10);
ReleaseDC(hWnd,hdc);
break;
case WM_LBUTTONDOWN:
char tbuffer[10];
_strtime(tbuffer);
hdc=GetDC(hWnd);
TextOut(hdc,0,0,tbuffer,10);
//printf("%s\n",tbuffer);
VARIANT vResult;
OLECHAR FAR* szFunction;
BSTR bstrTemp; // IDispatch pointers for Word's objects
IDispatch* pDispDocs; //Documents collection
IDispatch* pDispSel; //Selection object
IDispatch* pDispActiveDoc; //ActiveDocument object // DISPIDs
DISPID dispid_Docs; //Documents property of Application
//object
DISPID dispid_DocsAdd; //Add method of Documents collection
//object
DISPID dispid_Sel; //Selection property of Application
//object
DISPID dispid_TypeText; //TypeText method of Selection object
DISPID dispid_TypePara; //TypeParagraph method of Selection
//object
DISPID dispid_ActiveDoc; //ActiveDocument property of
//Application object
DISPID dispid_SaveAs; //SaveAs method of the Document object
DISPID dispid_Quit; //Quit method of the Application
//object // ******************** Start Automation *********************** //Initialize the COM libraries
::CoInitialize(NULL); // Create an instance of the Word application and obtain the
// pointer to the application's IDispatch interface.
CLSID clsid;
CLSIDFromProgID(L"Word.Application", &clsid); IUnknown* pUnk;
HRESULT hr ;
hr= ::CoCreateInstance( clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &pUnk);
IDispatch* pDispApp;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDispApp); // Get IDispatch* for the Documents collection object
szFunction = OLESTR("Documents");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Docs);
hr = pDispApp->Invoke (dispid_Docs, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&dpNoArgs, &vResult, NULL, NULL);
pDispDocs = vResult.pdispVal; // Invoke the Add method on the Documents collection object
// to create a new document in Word
// Note that the Add method can take up to 3 arguments, all of
// which are optional. You are not passing it any so you are
// using an empty DISPPARAMS structure
szFunction = OLESTR("Add");
hr = pDispDocs->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_DocsAdd);
hr = pDispDocs->Invoke(dispid_DocsAdd, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, &vResult, NULL, NULL); // Get IDispatch* for the Selection object
szFunction = OLESTR("Selection");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Sel);
hr = pDispApp->Invoke (dispid_Sel, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &dpNoArgs, &vResult,
NULL, NULL);
pDispSel = vResult.pdispVal;
// Get the DISPIDs of the TypeText and TypeParagraph methods of
// the Selection object. You'll use these DISPIDs multiple
// times.
szFunction = OLESTR("TypeText");
hr = pDispSel->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_TypeText); szFunction = OLESTR("TypeParagraph");
hr = pDispSel->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_TypePara); // The TypeText method has and requires only one argument, a
// string, so set up the DISPPARAMS accordingly
VARIANT vArgsTypeText[1];
DISPPARAMS dpTypeText; bstrTemp = ::SysAllocString(OLESTR("One"));
vArgsTypeText [0].vt = VT_BSTR;
vArgsTypeText [0].bstrVal = bstrTemp;
dpTypeText.cArgs = 1;
dpTypeText.cNamedArgs = 0;
dpTypeText.rgvarg = vArgsTypeText; //Invoke the first TypeText and TypeParagraph pair
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp); //Invoke the second TypeText and TypeParagraph pair
bstrTemp = ::SysAllocString(OLESTR("Two"));
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp); //Invoke the third TypeText and TypeParagraph pair
bstrTemp = ::SysAllocString(OLESTR("Three"));
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp);
希望大家检测一下其他程序有没有这样的现象。
而vc需要执行时,才调用,这样就会引起执行差异
[email protected]
又一个白痴!!!XP用BASIC编的?
在这里现宝啊
VB有时候比VC快并非没可能。
但一般情况下,要不,是你的计时不公,就是VC没写好。
要知道,查询接口是非常费时的动作,你是否优化到最少?
如果你每次调用同一个函数都调用一次GetIDsOfNames ,那么,无话可说,一定慢了。在所有调用过程中,同一个函数请保证只调用一次GetIDsOfNames 。实际上,在VC,调用后期绑定即麻烦,又慢。还是用前期绑定为好。
且vc是一种针对系统的语言,对于此类的操作,没有内部的支持,你可以研究一下
mfc的内部代码结构,就发现,vb和vc的不同,要说代码的效率,我个人认为vc>>vb
而针对具体的情况,应先研究一下它的机理.
七种武器有不同的用法,你说是吗.
ok 不多说了...
而用C写的是在你运行程序时才进行这些操作的,这样小的程序,光初始化和分配这一部分以经比你的程序主体所花费的时间还要多,
所以看起来当然是VBA快一点了.
需知:VB的编译程序(严格说,应该是半编译,哈哈)和动行库也是用C和汇编写出的的,真的要比C还高效,有可能吗??
当然,我也不是说VB不好,我自已是VB和C都用的,在写一些要求不高或要在很短时间内完成的程序时,用VB的好处明显大于用C;但在写对运行效率要求效的程序时,用C则好于用VB,C的高效不但在于它本身,而且在于它的内嵌汇编,这才是最高效的地方,在写图型处理程序时,一般都会用到不少内嵌汇编,就是这个原因。
其实没有必要说是C好还是VB好,VB是RAD语言,而C适合于写大型程序的核心部分,各有千秋.
:)