| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2217 人关注过本帖
标题:大家帮看看,怎么回事?Delphi竟比vc++还快
只看楼主 加入收藏
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
结帖率:50%
收藏
 问题点数:0 回复次数:1 
大家帮看看,怎么回事?Delphi竟比vc++还快
前天有一个求素数个数问题,用delphi7.0求得100000000以内素数用时21.234秒,为想更快一点,把代码变为C语言后用vc++6.0计算居然要27.578秒,故把代码贴出,大家帮忙查找原因?
Delphi7.0代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,math;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var i, j, k, l,m,n,r:integer;
      d,s:integer;
      x,z,t,t1,t2: double;
      a:array of integer;
    ss:string;
begin
     t1:=now;
     ss:=trim(self.Edit1.Text );
     if ss<>'' then n:=strtoint(ss) else n:=0;
     setlength(a,n+1);
     m:=0;s:=0;
         if (n=2) then
         begin
                m:=1;a[m]:=2;s:=2;
             end;
         if (n=3) then
         begin
                m:=1;a[m]:=2;s:=2;
             m:=2;a[m]:=3;s:=5;
         end;
         if (n>3)  then
         begin
                m:=1;a[m]:=2;s:=2;
                m:=2;a[m]:=3;s:=5;
                for i:=4 to n do
                 begin
                         x:=i;z:=sqrt(x);l:=trunc(z)+1;k:=0;
                        for  j:=1 to m  do
                      begin
                       if (a[j]>l) then break;
                       r:=i mod a[j];
                       if (r = 0) then
                       begin
                             inc(k);break;
                       end;
                end;
                if (k = 0) then
                begin
                       inc(m);
                       s := s + i;
                       a[m]:=i;
                end;
          end;
         end;
            
                t2:=now;
            t:=(t2-t1)*86400;     
            ss:=floattostr(t);
            self.Label5.Caption  :=ss;
            ss:=inttostr(m);
            self.Label3.Caption  :=ss;
            
end;

end.
vc++6.0代码:
// suhucs.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <malloc.h>
int main(int argc, char* argv[])
{
int i, j, k, l,m,n,r;
      int d,s;
      double x,z,t;
      clock_t begin,end;
      int *a;      
        printf("n=");
        scanf("%d",&n);
         printf("\n");   
         a=(int *)malloc((n+1)*sizeof(int))   ;
         begin=clock();
         m=0;s=0;
         if(n==2)
         {
             m=1;a[m]=2;s=2;
         }
         if(n==3)
         {
             m=1;a[m]=2;s=2;
             m=2;a[m]=3;s=5;
         }
         if(n>3)
         {
             m=1;a[m]=2;s=2;
             m=2;a[m]=3;s=5;
             for(i=4;i<=n;i++)
             {
                 x=i;z=sqrt(x);l=(int)z+1;k=0;
                 for( j=1;j<= m;j++)
                 {
                       if(a[j]>l)break;
                       r=i%a[j];
                    if (r == 0) {
                        k++;break;
                    }
                    
                }
                if (k == 0) {
                    m++;
                    s = s + i;
                    a[m]=i;
                }
               
            }
        }
            
         end=clock();
            t=(double)(end-begin)/CLOCKS_PER_SEC;
            printf("素数个数=%d\n",m);
            printf("计算时间=%f",t);
            free(a);
    system("pause");
    return 0;
}

搜索更多相关主题的帖子: procedure Windows C语言 
2015-04-26 13:45
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
收藏
得分:0 
计算结果贴图:
delphi7.0
图片附件: 游客没有浏览图片的权限,请 登录注册


vc++7.0
图片附件: 游客没有浏览图片的权限,请 登录注册


2015-04-26 13:49
快速回复:大家帮看看,怎么回事?Delphi竟比vc++还快
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013558 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved