Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 更快的代码If/then_Performance_Delphi_If Statement - Fatal编程技术网

Performance 更快的代码If/then

Performance 更快的代码If/then,performance,delphi,if-statement,Performance,Delphi,If Statement,我找到了一些类似的代码 procedure UpdateComp(Sender: TObject) begin if Sender = edtA then begin ... //to something end; if Sender = edtB then begin ... //to something end; if Sender = edtC then begin ... //to something end; ...

我找到了一些类似的代码

procedure UpdateComp(Sender: TObject)
begin
  if Sender = edtA then
  begin
    ...  //to something
  end;
  if Sender = edtB then
  begin
    ...  //to something
  end;
  if Sender = edtC then
  begin
    ...  //to something
  end;
  ...
end;
代码中的if/then用于50多个语句 每次都是发送方与表单上的组件匹配。 那么更改代码是否符合逻辑,如:

procedure UpdateComp(Sender: TObject)
begin
  if Sender = edtA then
  begin
    ...  //to something
  end
  else if Sender = edtB then
  begin
    ...  //to something
  end
  else if Sender = edtC then
  begin
    ...  //to something
  end;
  ...
end;
或者我这样改变是不对的

我在路上发现的一段代码:

procedure TContactController.UpdateComponent(Sender: TObject);
var
  I: Integer;
begin
  UpdateComponentActive := True;

  If Sender = pnlBasicInformation
  then begin
    pnlBasicInformation.Caption := UpperCase(Trim(Format('%s %s', [CurrentContact.Name, CurrentContact.FirstName])));
    If CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId] <> ''
    then pnlBasicInformation.Caption := Format('%s - %s',
                                               [pnlBasicInformation.Caption,
                                                UpperCase(CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId])]);
    pnlBasicInformation.Caption := StringReplace(pnlBasicInformation.Caption, '&', '&&', [rfReplaceAll]);
  end;

  If Sender = gbtnLock
  then begin
    If CurrentContact.Locked
    then gbtnLock.ImageIndex := 7
    else gbtnLock.ImageIndex := 19;
  end;

  If Sender = edtInternalReference
  then edtInternalReference.Text := CurrentContact.InternalReference;

  If Sender = edtExternalReference
  then edtExternalReference.Text := CurrentContact.ExternalReference;

  If Sender = edtFirstName
  then edtFirstName.Text := CurrentContact.FirstName;

  If Sender = edtName
  then edtName.Text := CurrentContact.Name;

  If Sender = edtSubName
  then edtSubName.Text := CurrentContact.SubName;

  If Sender = cbContactFunction
  then begin
    cbContactFunction.ItemIndex := -1;
    For I := 0 to cbContactFunction.Items.Count-1 do
    begin
      If TContactFunction(cbContactFunction.Items.Objects[I]).Id = CurrentContact.ContactFunctionId
      then begin
        cbContactFunction.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = cbLanguage
  then begin
    cbLanguage.ItemIndex := -1;
    For I := 0 to cbLanguage.Items.Count-1 do
    begin
      If TLanguage(cbLanguage.Items.Objects[I]).Id = CurrentContact.LanguageId
      then begin
        cbLanguage.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = cbSalutation
  then begin
    cbSalutation.ItemIndex := -1;
    For I := 0 to cbSalutation.Items.Count-1 do
    begin
      If TSalutation(cbSalutation.Items.Objects[I]).Id = CurrentContact.SalutationId
      then begin
        cbSalutation.ItemIndex := I;
        break;
      end;
    end;
  end;

  If Sender = edtCallingCodeMobilePhone
  then edtCallingCodeMobilePhone.Text := CurrentContact.CallingCodeMobilePhone;

  If Sender = edtMobilePhone
  then begin
    edtMobilePhone.Text := CurrentContact.MobilePhone;
    edtMobilePhone.OnKeyPress := OnPhoneNumberKeyPress;
  end;

  If Sender = gbtnMobilePhone
  then gbtnMobilePhone.Enabled := Trim(CurrentContact.MobilePhone) <> '';

  If Sender = gbtnMobilePhoneSms
  then gbtnMobilePhoneSms.Enabled := Trim(CurrentContact.MobilePhone) <> '';

  If Sender = edtBirthDate
  then edtBirthDate.Text := Format('dd/mm/yyyy', [CurrentContact.BirthDate]);

  If Sender = rbGenderM
  then rbGenderM.Checked := (CurrentContact.Gender = 0);

  If Sender = rbGenderV
  then rbGenderV.Checked := (CurrentContact.Gender = 1);

  If Sender = edtIdentityCardNumber
  then edtIdentityCardNumber.Text := CurrentContact.IdentityCardNumber;

  If Sender = edtNationalNumber
  then edtNationalNumber.Text := CurrentContact.NationalNumber;

  If Sender = imgProfilePhoto
  then imgProfilePhoto.Picture.Assign(ProfilePhoto.Picture.Graphic);

  If Sender = gbtnRemovePhoto
  then gbtnRemovePhoto.Enabled := PhotoManager.PhotoExists(pmContact, CurrentContact.Id);

  If Sender = edtRemarks
  then edtRemarks.Text := CurrentContact.Remarks;

  If Sender = edtInfo
  then edtInfo.Text := CurrentContact.Info;

  If Sender = edtRowVersion
  then edtRowVersion.Text := Format('dd/mm/yyyy', [CurrentContact.RowVersion]);

  UpdateComponentActive := False;
end;
过程TContactController.UpdateComponent(发送方:TObject);
变量
I:整数;
开始
UpdateComponentActive:=True;
如果发送方=pnlBasicInformation
然后开始
pnlBasicInformation.Caption:=大写(Trim(格式('%s%s',[CurrentContact.Name,CurrentContact.FirstName]));
如果CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId]“”
然后pnlBasicInformation.Caption:=格式(“%s-%s”,
[pnlBasicInformation.Caption,
大写(CurrentContact.HomeAddress.PostalCode.City.CityDescription[ApplicationManager.CurrentLanguageId]);
pnlBasicInformation.Caption:=StringReplace(pnlBasicInformation.Caption,&',&&',[rfReplaceAll]);
结束;
如果发送方=gbtnLock
然后开始
如果当前联系人已锁定
然后gbtnLock.ImageIndex:=7
else gbtnLock.ImageIndex:=19;
结束;
如果发送方=引用
然后是edtInternalReference.Text:=CurrentContact.InternalReference;
如果发送方=edtExternalReference
然后edtExternalReference.Text:=CurrentContact.ExternalReference;
如果发送方=edtFirstName
然后是edtFirstName.Text:=CurrentContact.FirstName;
如果发送方=edtName
然后是edtName.Text:=CurrentContact.Name;
如果发送方=edtSubName
然后是edtSubName.Text:=CurrentContact.SubName;
如果发送方=cbContactFunction
然后开始
cbContactFunction.ItemIndex:=-1;
对于I:=0到cbContactFunction.Items.Count-1 do
开始
如果TContactFunction(cbContactFunction.Items.Objects[I]).Id=CurrentContact.ContactFunctionId
然后开始
cbContactFunction.ItemIndex:=I;
打破
结束;
结束;
结束;
如果发送方=CBL语言
然后开始
cbLanguage.ItemIndex:=-1;
对于I:=0到cbLanguage.Items.Count-1 do
开始
如果t语言(cbLanguage.Items.Objects[I]).Id=CurrentContact.LanguageId
然后开始
cbLanguage.ItemIndex:=I;
打破
结束;
结束;
结束;
如果发送方=CBS评估
然后开始
cbSalutation.ItemIndex:=-1;
对于I:=0到cbSalutation.Items.Count-1 do
开始
如果TSALUTION(CBSALUATION.Items.Objects[I]).Id=CurrentContact.SALLATIONID
然后开始
cbSalutation.ItemIndex:=I;
打破
结束;
结束;
结束;
如果发送方=edtCallingCodeMobilePhone
然后edtCallingCodeMobilePhone.Text:=CurrentContact.CallingCodeMobilePhone;
如果发送方=edtMobilePhone
然后开始
edtMobilePhone.Text:=CurrentContact.MobilePhone;
edtMobilePhone.OnKeyPress:=OnPhoneNumberKeyPress;
结束;
如果发送方=gbtnMobilePhone
然后gbtnMobilePhone.Enabled:=Trim(CurrentContact.MobilePhone)';
如果发送方=gbtnMobilePhoneSms
然后gbtnMobilePhoneSms.Enabled:=Trim(CurrentContact.MobilePhone)';
如果发件人=出生日期
然后是edtbirchdate.Text:=格式('dd/mm/yyyy',[CurrentContact.BirthDate]);
如果发送方=rbGenderM
然后rbGenderM.Checked:=(CurrentContact.Gender=0);
如果发送方=rbGenderV
然后rbGenderV.Checked:=(CurrentContact.Gender=1);
如果发送方=edtIdentityCardNumber
然后是edtIdentityCardNumber.Text:=CurrentContact.IdentityCardNumber;
如果发送方=edtNationalNumber
然后是edtNationalNumber.Text:=CurrentContact.NationalNumber;
如果发送方=imgProfilePhoto
然后imgProfilePhoto.Picture.Assign(ProfilePhoto.Picture.Graphic);
如果发送方=gbtnRemovePhoto
然后gbtnRemovePhoto.Enabled:=PhotoManager.PhotoExists(pmContact,CurrentContact.Id);
如果发送方=EDT,则为备注
然后是edtmarents.Text:=CurrentContact.备注;
如果发送方=edtInfo
然后edtInfo.Text:=CurrentContact.Info;
如果发送方=edtRowVersion
然后是edtRowVersion.Text:=格式('dd/mm/yyyy',[CurrentContact.RowVersion]);
UpdateComponentActive:=False;
结束;

因此,我想在第一次更改时使用if/else更改代码

您的建议是正确的,可以为您节省一些CPU指令执行(如果您非常关心性能的话)。但前提是所有的表达都是平等的。这是因为第一个代码块总是计算所有表达式(无论其中一个表达式的计算结果是否为true),而您建议的代码则不是


但在这种情况下,我会检查代码,尽量找到与您一起工作的控件的共同点,并对它们进行相应的分组(如果可能)。

您的建议是正确的,可以为您节省一些CPU指令执行(如果您非常关心性能)。但前提是所有的表达都是平等的。这是因为第一个代码块总是计算所有表达式(无论其中一个表达式的计算结果是否为true),而您建议的代码则不是


但是在这种情况下,我会检查代码,并尽量找到您使用的控件的共同点,并相应地对它们进行分组(如果可能)。

一个可能的替代方法是将不同编辑字段的
Tag
属性设置为不同的数字,并使用
大小写(发送方为TEdit)。Tag of
-子句。这将在内部使用一个跳转表,并避免必须处理一系列if语句


另一种可能性是,如果您愿意的话,很难实现您自己的类继承自TEdit(或TCustomEdit)并添加一个额外的事件句柄,仅适用于您现在调用
UpdateComp

的情况。一个可能的替代方法是将不同编辑字段的
Tag
属性设置为不同的数字,并使用
大小写(发送方作为TEdit)。Tag of
-子句。这将在内部使用一个跳转表,并避免必须处理一系列if语句

A