内存占用太多,为什么?
哪位能不能帮我优化一下?我这个程序不知道为什么,占用了很大的内存,速度很慢
typedef struct AList
{
int no1;
int no2;
int no3;
int no4;
int no5;
} TAList;
typedef TAList* PAList;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
randomize();
ResultList = new TList;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
DWORD StartTime,EndTime,LastTime;
StartTime=GetTickCount(); //从这开始计算下面这个循环运行的时间
for(int i=0;i<100000;i++)
{
PAList AStruct;
AStruct = new TAList;
AStruct->no1=random(10);
AStruct->no2=random(10);
AStruct->no3=random(10);
AStruct->no4=random(10);
AStruct->no5=random(10);
ResultList->Add(AStruct);
}
//开始比较,此处占用了极多的内存
for(int i=0;i<Form1->ResultList->Count-1;i++)
{
PAList AStruct;
AStruct = new TAList;
AStruct = (PAList) ResultList->Items[i];
for(int j=i+1;j<Form1->ResultList->Count;j++)
{
PAList BStruct;
BStruct = new TAList;
BStruct = (PAList) ResultList->Items[j];
int iSame=0;
if(AStruct->no1==(BStruct->no1+1)) continue;
if(AStruct->no2==(BStruct->no2+1)) continue;
if(AStruct->no3==(BStruct->no3+1)) continue;
if(AStruct->no4==(BStruct->no4+1)) continue;
if(AStruct->no5==(BStruct->no5+1)) continue;
if(AStruct->no1==(BStruct->no1+2)) continue;
if(AStruct->no2==(BStruct->no2+2)) continue;
if(AStruct->no3==(BStruct->no3+2)) continue;
if(AStruct->no4==(BStruct->no4+2)) continue;
if(AStruct->no5==(BStruct->no5+2)) continue;
ResultList->Delete(j);j--;
delete BStruct;
}
}
EndTime=GetTickCount(); //循环结束,计时也结束
LastTime=EndTime-StartTime;
Application->MessageBoxA(IntToStr(LastTime).c_str(),"运算时间",MB_ICONEXCLAMATION);
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
if(ResultList->Count>0)
for(int i=ResultList->Count-1;i<-1;i--)
{
PAList AStruct;
AStruct = new TAList;
AStruct =(PAList) ResultList->Items[i];
ResultList->Delete(i);
delete[] AStruct;
}
}