| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 675 人关注过本帖
标题:这个程序用vc++2010编译后执行速度巨慢
只看楼主 加入收藏
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
结帖率:50%
收藏
 问题点数:0 回复次数:4 
这个程序用vc++2010编译后执行速度巨慢
以下是一个求完全数的程序,我用vc++2010编译后运行,发现速度不是一般的慢,共耗时132.969秒钟,尝试了多种优化措施,变化不大,各位大大帮忙看看是什么原因。
以下是源代码:
// 完数.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
clock_t start, finish;
    int h,i,j,k,l,m,n,p;
    long long a,b,c;
    double x,z;
    cout<<"n=";
    cin>>n;
    cout<<endl;
    if(n<1) n=1;
if(n>32)n=32;
   
start = clock();
    for(i=2;i<=n;i++)
    {
p=i;m=0;
x=p;z=sqrt(x);h=int(z);
if(h<(p-1)) h++;
for(j=2;j<=h;h++)
{
if((p % j)==0)
{
m=1;break;
}
}
if(m==0)
{
a=1;
for(k=1;k<=p;k++)
{
a*=2;
}
a=a-1;


        
        x=(double)a;z=sqrt(x);h=int(z)+1;
        
        l=0;
        for(j=2;j<=h;j++)
        {
b=j;
            if((a % b)==0)
            {
                l++;
                break;
            }
        }
        if(l==0)
        {
    if(a<3037000499) c=a*(a+1)/2; else c=a;
             cout<<" 质数: "<<i<<"  ";           
            //cout<<endl;
if(a<3037000499) cout<<"完数= "<<c;else cout<<"素数= "<<c;
cout<<endl;
        }
}
    }
      finish = clock();
  z= (double)(finish - start) / CLOCKS_PER_SEC;
      cout<<"t= "<<z;
system("pause");
    return 0;
}

搜索更多相关主题的帖子: 应用程序 include 源代码 double 控制台 
2013-09-27 09:48
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
收藏
得分:0 
为了找到原因,我把代码转化为Delphi程序,结果速度超快,试了几次均为0秒钟解决问题,这就奇怪了,平时都是vc++略快一些过Delphi,现在却倒了过来。
以下是delphi代码:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var   h,i,j,k,l,m,n,p:integer;
      a,b,c:int64;
       x,z,t1,t2,t3: double  ;
       s:string;
begin
    s:=trim(self.Edit1.Text );
    if s<>'' then n:=strtoint(s) else n:=1;
    if n>32 then n:=32;
     t1:=now;
     self.Memo1.Clear ;
    for i:=2 to n do
    begin
     p:=i;m:=0;
     x:=p;z:=sqrt(x);h:=trunc(z);
      if(h<(p-1)) then h:=h+1;
    for j:=2 to h do
     begin
       if((p mod j)=0)  then
        begin
         m:=1;break;
       end;
 end;
if(m=0) then
begin
a:=1;
for k:=1 to p do
begin
a:=a*2;
end;
a:=a-1;
        
        x:=a;z:=sqrt(x);h:=trunc(z)+1;
        l:=0;
        for j:=2 to h do
        begin
       b:=j;
            if((a mod b)=0)   then
            begin
                l:=l+1;
                break;
            end;
        end;
        if(l=0)  then
        begin

             if (a<3037000499) then c:=trunc(a*(a+1)/2)  else c:=a;
             s:=' 质数: '+ inttostr(i)+'   ';
         if(a<3037000499) then  s:=s+'完数= '+inttostr(c) else s:=s+'素数= '+inttostr(c);
       self.Memo1.Lines.Append(s);
        end;
end;
    end;
    t2:=now;
    t3:=(t2-t1)*86400;
    s:=' t= '+floattostr(t3);
    self.Memo1.Lines.Append(s);
end;

end.
2013-09-27 09:56
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
收藏
得分:0 
这位大侠,请看你的Vs里面编写的这段代码:
for(j=2;j<=h;h++)//是h++吗?应该是j++吧?时间全浪费到这个上面了,能不慢吗?
{
if((p % j)==0)
{
m=1;break;
}
}
在看你在delphi
for j:=2 to h do   这里的代码和VS里面的代码能等价吗?是不是有点不公平呀?
begin
    if((p mod j)=0)  then
    begin
      m:=1;
      break;
    end;
end;
将Vs中的h++,改成j++之后时间花费几乎也是0的,你试试。
编写代码的时候不认真!后果比较严重。
收到的鲜花
  • lonmaor2013-09-27 10:26 送鲜花  20朵   附言:看代码好仔细,一针见血

♂ 死后定当长眠,生前何须久睡。♀
2013-09-27 10:23
lzb6689
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2007-11-9
收藏
得分:0 
非常感谢303770957百忙中抽出时间来帮忙,改后问题得以解决,看来是我自己的粗心造成。
2013-09-27 15:17
wf918
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-9-28
收藏
得分:0 
回复 楼主 lzb6689
不催是啊 的
2013-09-28 05:52
快速回复:这个程序用vc++2010编译后执行速度巨慢
数据加载中...
 
   



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

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