Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2024-07

What is the most efficient construction?

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Arí Ricardo Ody

Hi guys

A question that I have had for a long time:

Suppose I have the variable

list:TStringList

and I want to iterate through the items through the items in list.

Between the construction:

For i := 0 to list.Count - 1 do
begin;
  commands
end;

And the construction

j := list.Count - 1;
For i := 0 to j do
begin;
   commands
end;

Which would be the most efficient?

I ask because I have the impression that in the first construction it will be necessary to generate code to determine the value list.Count - 1 at each iteration of the For loop.

Greetings from S?o Paulo - Brazil

Ar? Ricardo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20240728/5d6aa406/attachment.htm>

Reply by email

Sven Barth
to Arí Ricardo Ody: Hi guys A question that I have had for a long time: Suppose I have the variable…

Ar? Ricardo Ody via lazarus schrieb am So.,
28. Juli 2024, 13:45:

Show quoted text 31 lines
Hi guys A question that I have had for a long time: Suppose I have the variable list:TStringList and I want to iterate through the items through the items in list. Between the construction: For i := 0 to list.Count - 1 do begin; commands end; And the construction j := list.Count - 1; For i := 0 to j do begin; commands end; Which would be the most efficient? I ask because I have the impression that in the first construction it will be necessary to generate code to determine the value list.Count - 1 at each iteration of the For loop.

These are the same, cause in Pascal unlike in C-like languages the end condition of a for-loop is only evaluated once at the start of the loop.

Regards,
Sven

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20240729/7d813fe8/attachment.htm>

Reply by email

Sven Barth
to Matthieu Giroux: Matthieu Giroux 13 Rue Fr Tanguy PRIGENT A.15 - 35000 RennesT?l : 09 61 37 01 82 - 06 44…

Matthieu Giroux via lazarus schrieb am So.,
28. Juli 2024, 16:50:

Show quoted text 17 lines
Matthieu Giroux 13 Rue Fr Tanguy PRIGENT A.15 - 35000 Rennes T?l : 09 61 37 01 82 - 06 44 733 008 http://www.liberlog.fr For i := 0 to list.Count - 1 do begin; commands end; with list do for i:=0 to count-1 do Begin LText := Strings [i];

This doesn't change anything regarding efficency except if "list" happens to be some complex expression.

Regards,
Sven

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20240729/8f67d08f/attachment-0001.htm>

Reply by email