| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 699 人关注过本帖
标题:大家帮看看,怎么回事?Delphi竟比vc++还快
只看楼主 加入收藏
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:4 
大家帮看看,怎么回事?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:51
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
收藏
得分:0 
计算结果贴图:
delphi7.0
图片附件: 游客没有浏览图片的权限,请 登录注册

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

2015-04-26 13:52
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:14 
不要重复发帖

DO IT YOURSELF !
2015-04-26 14:04
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
我不是来改进楼主算法的,只是看楼主的代码不工整,删除垃圾代码并排版一下,算法没变

C++代码
程序代码:
#include <cstdio>
#include <cstdlib>
#include <ctime>

int main( void )
{
    const unsigned n = 100000000;
    unsigned* a = new unsigned[n];
    unsigned m = 0;

    clock_t tm_beg = clock();
    for( unsigned i=2; i!=n+1; ++i )
    {
        bool isprime = true;
        for( unsigned j=0; isprime && j!=m && a[j]*a[j]<=i; ++j )
            isprime = (i%a[j]!=0);

        if( isprime )
            a[m++] = i;
    }
    clock_t tm_end = clock();

    delete[] a;
    printf( "素数个数 = %u\n", m );
    printf( "计算时间 = %.3f\n", (tm_end-tm_beg)*1.0/CLOCKS_PER_SEC );

    return 0;
}

C代码
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>

int main( void )
{
    const unsigned n = 100000000;
    unsigned* a = malloc( n*sizeof(*a) );
    unsigned m = 0;

    clock_t tm_beg = clock();
    for( unsigned i=2; i!=n+1; ++i )
    {
        bool isprime = true;
        for( unsigned j=0; isprime && j!=m && a[j]*a[j]<=i; ++j )
            isprime = (i%a[j]!=0);

        if( isprime )
            a[m++] = i;
    }
    clock_t tm_end = clock();

    free( a );
    printf( "素数个数 = %u\n", m );
    printf( "计算时间 = %.3f\n", (tm_end-tm_beg)*1.0/CLOCKS_PER_SEC );

    return 0;
}

2015-05-05 12:59
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
收藏
得分:0 
楼上的代码比较工整、简洁,学习了。但我拷贝代码到vc6上运行发现很慢,查看了代码,只声明为无符号类型,没有指明是哪一具体类型;此外C代码在vc6编译通不过,不知什么原因?
图片附件: 游客没有浏览图片的权限,请 登录注册
2015-05-15 09:54
快速回复:大家帮看看,怎么回事?Delphi竟比vc++还快
数据加载中...
 
   



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

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