地
地质灾害
Unregistered / Unconfirmed
GUEST, unregistred user!
全组合问题。C++执行的速度比Pascal代码快一倍。可能是Pascal代码写得不好吧 哪里可以改进?C++代码如下#include <iostream>#include "math.h"#include "windows.h"using namespace std;#define USE_TEXT_BUFFERunsigned int TextBufferLen = 1024;unsigned int LoopTimes = 20;inline void BufferText(char * buf,const char * str,int lf){ if ((strlen(str)+strlen(buf))+lf>TextBufferLen) { cout<<buf;
buf[0]=0;
} strcat(buf,str);
if(lf) strcat(buf,"/n");}void OutputCombinations(int cnt,char **data){ unsigned long i,k;
#ifdef USE_TEXT_BUFFER char* buf=(char*)malloc(TextBufferLen+1);
buf[0]=0;
#endif for(i=1;i<pow(2,cnt)-1;i++) { if(i & 1) #ifdef USE_TEXT_BUFFER BufferText(buf,data[0],0);
#else
cout<<data[0];
#endif for(k=1;k<cnt;k++) { if((i>>k) & 1) #ifdef USE_TEXT_BUFFER BufferText(buf,data[k],0);
#else
cout<<data[k];
#endif } #ifdef USE_TEXT_BUFFER BufferText(buf,"/n",0);
#else
cout<<endl;
#endif } #ifdef USE_TEXT_BUFFER cout<<buf;
free(buf);
#endif} int main(int argc, char *argv[]){ DWORD Tick;
unsigned long n;
cout<<"the number of elements for testing:/n";
cin>>n;
cout<<"the string buffer size:/n";
cin>>TextBufferLen;
cout<<"how many calls:/n";
cin>>LoopTimes;
char **data=(char**)malloc(n*sizeof(char*));
cout<<"Initialize array/n";
for(Tick=0;Tick<n;Tick++) { data[Tick]= new char[2];
data[Tick][0]=65+Tick;
data[Tick][1]=0;
} cout<<"Test begin
/n";
Tick=GetTickCount();
for(int i=1;i<=LoopTimes;i++) { OutputCombinations(n,data);
} Tick=GetTickCount()-Tick;
cout<<"Test end/n";
cout<<"Time ellapsed: "<<Tick<<" milliseconds/n";
cout<<"Time used by each call: "<<Tick/LoopTimes<<" milliseconds/n";
for(Tick=0;Tick<n;Tick++) { delete []data[Tick];
} free(data);
system("pause");
return 0;}Pascal代码如下program Composite;{$APPTYPE CONSOLE}uses SysUtils, Math, Windows;var TextBufferLen:Cardinal = 1024;
LOOP_TIMES:Cardinal = 20;procedure GetString(Data:array of string);var i,j,k:Cardinal;
s:string;
begin
j:=Length(Data);
s:='';
for i:=1 to Trunc(IntPower(2,j))do
begin
if (i and 1)>0 then
s:=s+Data[0];
for k:=1 to j-1do
if ((i shr k) and 1)>0 then
s:=s+Data[k];
s:=s+#10;
if Cardinal(Length(s))>TextBufferLen then
begin
write(s);
s:='';
end;
end;
write(s);
end;
var Tick:Cardinal;
n:Cardinal;
Data:array of string;
begin
Writeln('the number of elements for testing:');
Readln;
Writeln('the string buffer size:');
Readln(TextBufferLen);
Writeln('how many calls:');
Readln(LOOP_TIMES);
SetLength(Data,n);
Writeln('Initialize array');
for Tick:=0 to n-1do
Data[Tick]:=Chr(65+Tick);
Writeln('Test begin
');
Tick:=GetTickCount();
for n:=1 to LOOP_TIMESdo
GetString(Data);
Tick:=GetTickCount()-Tick;
Writeln('Test end');
Writeln('Time ellapsed: ',Tick,' milliseconds');
Writeln('Time used by each call: ',Tick div LOOP_TIMES,' milliseconds');
Readln;
SetLength(Data,0);
end.
buf[0]=0;
} strcat(buf,str);
if(lf) strcat(buf,"/n");}void OutputCombinations(int cnt,char **data){ unsigned long i,k;
#ifdef USE_TEXT_BUFFER char* buf=(char*)malloc(TextBufferLen+1);
buf[0]=0;
#endif for(i=1;i<pow(2,cnt)-1;i++) { if(i & 1) #ifdef USE_TEXT_BUFFER BufferText(buf,data[0],0);
#else
cout<<data[0];
#endif for(k=1;k<cnt;k++) { if((i>>k) & 1) #ifdef USE_TEXT_BUFFER BufferText(buf,data[k],0);
#else
cout<<data[k];
#endif } #ifdef USE_TEXT_BUFFER BufferText(buf,"/n",0);
#else
cout<<endl;
#endif } #ifdef USE_TEXT_BUFFER cout<<buf;
free(buf);
#endif} int main(int argc, char *argv[]){ DWORD Tick;
unsigned long n;
cout<<"the number of elements for testing:/n";
cin>>n;
cout<<"the string buffer size:/n";
cin>>TextBufferLen;
cout<<"how many calls:/n";
cin>>LoopTimes;
char **data=(char**)malloc(n*sizeof(char*));
cout<<"Initialize array/n";
for(Tick=0;Tick<n;Tick++) { data[Tick]= new char[2];
data[Tick][0]=65+Tick;
data[Tick][1]=0;
} cout<<"Test begin
/n";
Tick=GetTickCount();
for(int i=1;i<=LoopTimes;i++) { OutputCombinations(n,data);
} Tick=GetTickCount()-Tick;
cout<<"Test end/n";
cout<<"Time ellapsed: "<<Tick<<" milliseconds/n";
cout<<"Time used by each call: "<<Tick/LoopTimes<<" milliseconds/n";
for(Tick=0;Tick<n;Tick++) { delete []data[Tick];
} free(data);
system("pause");
return 0;}Pascal代码如下program Composite;{$APPTYPE CONSOLE}uses SysUtils, Math, Windows;var TextBufferLen:Cardinal = 1024;
LOOP_TIMES:Cardinal = 20;procedure GetString(Data:array of string);var i,j,k:Cardinal;
s:string;
begin
j:=Length(Data);
s:='';
for i:=1 to Trunc(IntPower(2,j))do
begin
if (i and 1)>0 then
s:=s+Data[0];
for k:=1 to j-1do
if ((i shr k) and 1)>0 then
s:=s+Data[k];
s:=s+#10;
if Cardinal(Length(s))>TextBufferLen then
begin
write(s);
s:='';
end;
end;
write(s);
end;
var Tick:Cardinal;
n:Cardinal;
Data:array of string;
begin
Writeln('the number of elements for testing:');
Readln;
Writeln('the string buffer size:');
Readln(TextBufferLen);
Writeln('how many calls:');
Readln(LOOP_TIMES);
SetLength(Data,n);
Writeln('Initialize array');
for Tick:=0 to n-1do
Data[Tick]:=Chr(65+Tick);
Writeln('Test begin
');
Tick:=GetTickCount();
for n:=1 to LOOP_TIMESdo
GetString(Data);
Tick:=GetTickCount()-Tick;
Writeln('Test end');
Writeln('Time ellapsed: ',Tick,' milliseconds');
Writeln('Time used by each call: ',Tick div LOOP_TIMES,' milliseconds');
Readln;
SetLength(Data,0);
end.