最近把VC和DELPHI做也以下比较,意外的发现DELPHI竟然比C++快,也不知道我分析的对不对!以下是两段代码!
VC:
CTime time1;
CTime starttime = CTime::GetCurrentTime();
for(int i = 0 ;i<=100000;i++)
{
for(int j = 0 ;j<=100000;j++)
{
time1 = CTime::GetCurrentTime();
}
}
CTime endtime = CTime::GetCurrnetTime();
然后我计算两个时间差,花去时间241秒
DELPHI:
var
i,j :integer;
starttime,endtime,time1 :TDateTime;
begin
starttime := time;
for i := 0 to 100000 do
begin
for j := 0 to 100000 do
begin
time1 := time;
end
end;
endtime := time;
end
然后同样计算其花去的时间,结果只用去了120秒。
为什么会这样呢?请高手分析分析
VC:
CTime time1;
CTime starttime = CTime::GetCurrentTime();
for(int i = 0 ;i<=100000;i++)
{
for(int j = 0 ;j<=100000;j++)
{
time1 = CTime::GetCurrentTime();
}
}
CTime endtime = CTime::GetCurrnetTime();
然后我计算两个时间差,花去时间241秒
DELPHI:
var
i,j :integer;
starttime,endtime,time1 :TDateTime;
begin
starttime := time;
for i := 0 to 100000 do
begin
for j := 0 to 100000 do
begin
time1 := time;
end
end;
endtime := time;
end
然后同样计算其花去的时间,结果只用去了120秒。
为什么会这样呢?请高手分析分析
解决方案 »
- 在类的重载构造函数中动态分配内存
- 文件传输问题!
- 求救vc+access 2000中debug模式时,recordsetptr的createinstance突然出现无效指针问题
- 怎样根据 钩子回调函数的参数(int nCode, WPARAM wParam, LPARAM lParam) 得到捕获的消息的内容
- 基于对话框的程序,在用AnimateWindow 时的疑问
- 大家好,如何把剪贴板中的东西或插入的OLE对象变为位图放在我的窗口中?
- 关于IHTMLElementCollection的问题,IE高手请帮忙呀。
- Ado事件问题,为什么照着MSDN中的中的列子做了,但得不到ADOConnect和Record的事件。
- SOS!win98下对文件自身不能进行读操作吗,在线等待
- 为什么图像刚开始能显示,之后就显示不了了
- 一个关于函数gethostbyname()的很菜的问题!
- 怎么样让程序自动搜索一下局域网中的在线用户,急啊,
time1 = CTime::GetCurrentTime();
time1 := time;
有些不公平,VC要调用类函数CTime::GetCurrentTime(),但PASCAL没有。最好两者都在Release版下比较,两者都调以时间最快为编译方式(不知道PASCAL有没有这种选择,没有用过)
time_t time1;
time_t starttime = time(NULL);
for(int i = 0 ;i<=100000;i++)
{
for(int j = 0 ;j<=100000;j++)
{
time1 = time(NULL);
}
}
time_t endtime = time(NULL);
BCB的文件400多K,VC的只有200多K,但是我用aspack压缩一下,都是107K了(VC的也是107K,一样???)。看上去VC没有优势了??
自建一个Test的对话框程序,建一个按钮Button1。编译为静态MFC的Releae版,默认配置。
void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
DWORD starttime = GetTickCount();
DWORD time1;
for(int i = 0 ;i<=10000;i++)
{
for(int j = 0 ;j<=10000;j++)
{
time1 = GetTickCount();
}
}
DWORD endtime = GetTickCount();
float fTime = (endtime - starttime) / 1000.0;
CClientDC dc(this);
CString strTemp;
strTemp.Format("The time is %f Second", fTime);
dc.TextOut(100, 100, strTemp);
}之所以用GetTickCount,因为参看MSDN的解释:
GetCurrentTime
The GetCurrentTime is obsolete. It is provided only for compatibility with 16-bit versions of Windows. Win32-based applications should use the GetTickCount function.
测试结果:
WinMe CIII 128M内存, 2.95 秒左右。
The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer
DWORD GetTickCount(VOID);
以上也是MSDN的解释。即是从开机开始计算的毫秒数,这在我们的编GAME中经常用到的。2:
float fTime = (endtime - starttime) / 1000.0;
CString strTemp;
strTemp.Format("The time is %f Second", fTime); //这句就是将它转为CString类型了。造成Delphi和VC的时间差别主要是在函数上,以下是我想的:Delphi的time函数只是做秒的处理;但CTime::GetCurrentTime()要做的处理还有年、月、日等东西,所以不公平,故用SDK 的GetCurrentTime的32位替代品GetTickCount来代替才较公平。
提问题时标题要简明扼要地说明问题内容,切忌使用"急","求救"之类不能说明问题的标题
http://www.betajin.com/alphasun/index.htm 给我发信息请附带原帖地址
http://alphasun.18en.com/ http://shakingtoolkit.9126.com/
DocWizard C++程序文档自动生成工具 | Wave OpenGL | HttpProxy | AjaxParser词法分析
time1 :TDateTime;
放到循环里再比
time() //hh:mm:ss
date() //yyyy-mm-dd
vc 里CTime::GetCurrentTime()取了yyyy-mm-dd hh:mm:ss所以上面的比较对vc不公平
VC6.0企业版,Release,没有优化
int k = 0;
DWORD StartTime = GetTickCount();
for(int i = 0 ;i<=100000;i++)
for(int j = 0 ;j<=100000;j++)
if(j % 2)
k++;
char Buffer[100];
itoa(GetTickCount() - StartTime, Buffer, 10);
MessageBox(Buffer);测试结果:58764Delphi5.0企业版:Release,没有优化
var
i,j,k: integer;
StartTime: DWORD;
begin
k := 0;
StartTime := GetTickCount();
for i := 0 to 100000 do
for j := 0 to 100000 do
if j mod 2 = 1 then
Inc(k);
ShowMessage(IntToStr(GetTickCount() - StartTime));
end;测试结果:54909结论,在上述的测试条件下,程序的效率差不多.