如何增加FOR循环语句中的FOR循环值? [英] How to increase the FOR-loop value in a FOR-loop statement?

查看:236
本文介绍了如何增加FOR循环语句中的FOR循环值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在FOR循环语句中增加值。

I want to know how to increase the value in a FOR-loop statement.

这是我的代码。

function Check(var MemoryData:Array of byte;MemorySignature:Array of byte;Position:integer):boolean;
var i:byte;
begin
 for i := 0 to Length(MemorySignature) - 1 do
 begin
  while(MemorySignature[i] = $FF) do inc(i); //<< ERROR <<
  if(memorydata[i + position] <> MemorySignature[i]) then Result:=false;
 end;
 Result := True;
end;

错误是:E2081赋值给FOR-Loop变量'i'。

The error is: E2081 Assignment to FOR-Loop variable 'i'.

我试图将一个旧的代码从C#转换成Delphi,但是我不能增加'我'。
增加我不是唯一的办法,但我想知道问题在哪里。

I'm trying to translate an old code from C# to Delpbut I can't increase 'i'. Increasing 'i' is not the only way to go,but I want to know where the problem is.

推荐答案

当然,其他的(一般)是正确的。没有说的是,你的循环中的我 不存在 。 Delphi使用一个CPU寄存器。这就是为什么你不能改变它,这就是为什么你应该使用'for'循环(而不是'while'),因为'for'更快。这是你的代码修改(未测试,但我认为你有这个想法) - 还有你有一些错误 - 也修复它们:

Of course the others are (generally) correct. What wasn't said, is that 'i' in your loop doesn't exist. Delphi uses a CPU register for it. That's why you cannot change it and that's why you should use a 'for' loop (not a 'while') because the 'for' is way faster. Here is your code modified (not tested but I think that you got the idea) - also imho you had some bugs - fixed them also:

function Check(var MemoryData:Array of byte;MemorySignature:Array of byte;Position:integer):boolean;
var i:byte;
begin
 Result := True; //moved at top. Your function always returned 'True'. This is what you wanted?
 for i := 0 to Length(MemorySignature) - 1 do //are you sure??? Perhaps you want High(MemorySignature) here... 
 begin
  if MemorySignature[i] <> $FF then //speedup - '<>' evaluates faster than '='
  begin
   Result:=memorydata[i + position] <> MemorySignature[i]; //speedup.
   if not Result then 
     Break; //added this! - speedup. We already know the result. So, no need to scan till end.
  end;
 end;
end;

... MemorySignature应该有一个'const'或'var'。否则,现在数组被复制。这意味着每次调用检查时减速。有一个'var'的东西要快得多,代码不变,因为AFAIS的MemorySignature没有改变。

...also MemorySignature should have a 'const' or 'var'. Otherwise as it is now the array gets copied. Which means slowdown at each call of 'Check'. Having a 'var' the things are much faster with code unchanged because AFAIS the MemorySignature isn't changed.

HTH

这篇关于如何增加FOR循环语句中的FOR循环值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆