最近把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秒。
为什么会这样呢?请高手分析分析

解决方案 »

  1.   

    你在VC里面完成了复杂的函数调用,在dephi里面只是简单的赋值,你说谁快?
      

  2.   

    已经几乎忘记了PASCAL了,发现这里
    time1 = CTime::GetCurrentTime();
    time1 := time;
    有些不公平,VC要调用类函数CTime::GetCurrentTime(),但PASCAL没有。最好两者都在Release版下比较,两者都调以时间最快为编译方式(不知道PASCAL有没有这种选择,没有用过)
      

  3.   

    VC:
        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);
      

  4.   

    其实这个程序就是比较VC和DELPHI调用API函数哪个更快,比较的就是CTime::GetCurrentTime()函数和time函数的速度,这两个函数都是封装了系统的API,但DELPHI更快,在VC中除了用CTime::GetCurrentTime()函数获得系统时间外还有其他的函数吗?
      

  5.   

    我还有一个问题,用VC和BCB各写一个程序,功能一样。都是静态链接。
    BCB的文件400多K,VC的只有200多K,但是我用aspack压缩一下,都是107K了(VC的也是107K,一样???)。看上去VC没有优势了??
      

  6.   

    啊,time在PASCAL竟然是个函数,还以为是一个变量。
    自建一个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 秒左右。
      

  7.   

    函数GetTickCount()是获得系统时间的函数吗?那该怎么样把他转化成CString类型呢?
      

  8.   

    1:
    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来代替才较公平。
      

  9.   

    要比较就调用同样的函数,比如上面的GetTickCount()。
      

  10.   

    这两个程序是不一样的,所以不能拿来比较。程序内容应该尽量不涉及操作系统调用。因为我们不是比较操作系统。为了比较两个编译器产生的代码的效率,我们应该采用数值算法、数据结构算法等方法。其中最好不要有操作系统调用。素数测试这样的程序应该是很好的材料。============================================================================
    提问题时标题要简明扼要地说明问题内容,切忌使用"急","求救"之类不能说明问题的标题
    http://www.betajin.com/alphasun/index.htm          给我发信息请附带原帖地址
    http://alphasun.18en.com/                    http://shakingtoolkit.9126.com/
    DocWizard C++程序文档自动生成工具 | Wave OpenGL | HttpProxy | AjaxParser词法分析
      

  11.   

    你应该把这句话
    time1 :TDateTime;
    放到循环里再比
      

  12.   

    delphi 里取时间的函数应该是:now() //yyyy-mm-dd hh:mm:ss
                                 time() //hh:mm:ss
                                 date() //yyyy-mm-dd
    vc 里CTime::GetCurrentTime()取了yyyy-mm-dd hh:mm:ss所以上面的比较对vc不公平
      

  13.   

    把你的程序修改了一下:
    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结论,在上述的测试条件下,程序的效率差不多.