BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Iterator vs For loop

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Peter Morris [Droopy Eyes
Guest





PostPosted: Mon Jan 05, 2004 9:25 pm    Post subject: Iterator vs For loop Reply with quote



Hi all

Is there any advantage to an iterator over a for loop where

a) The list is already exposed by its owner
b) The list is not a composite list

Pete


Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Mon Jan 05, 2004 10:00 pm    Post subject: Re: Iterator vs For loop Reply with quote



Peter Morris [Droopy Eyes Software] wrote:

Quote:
Is there any advantage to an iterator over a for loop where

a) The list is already exposed by its owner
b) The list is not a composite list


If the list already has an indexed Item[] property then an Iterator could be
seen as superfluous.

However, Iterators have their benefits in constructing tools like skip
iterators that filter a list, or tree iterators where the Tree has no
concept of indexing.

Iterators are best obtained from the list that they will iterate i.e. the
list should have a GetIterator method.

Could you explain a & b, they do not mean much to me at the moment?

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Marc Rohloff
Guest





PostPosted: Mon Jan 05, 2004 10:02 pm    Post subject: Re: Iterator vs For loop Reply with quote



Peter Morris [Droopy Eyes Software] wrote on Mon, 5 Jan 2004 21:25:57 -
0000 ...

Quote:
Is there any advantage to an iterator over a for loop where

a) The list is already exposed by its owner
b) The list is not a composite list

For loops are generally considerably faster than for loops, however an
iterator can offer a good level of abstraction even in a simple case
like this. For example you may later want to make your list a composite.
Well designed iterators can also insulate your code from the impact of
changes (additions and deletions) to the list while you are busy
iterating over it.

Marc Rohloff
marc rohloff at bigfoot dot com

Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Mon Jan 05, 2004 10:03 pm    Post subject: Re: Iterator vs For loop Reply with quote

Marc Rohloff wrote:

Quote:
For loops are generally considerably faster than for loops

But which for loop is faster? :-)

Quote:
Well designed iterators can also insulate your code from
the impact of changes (additions and deletions) to the list while you
are busy iterating over it.

Good point, I have the Iterator observe the list and when a deletion occurs,
I can shortcut terminate the Iterator.

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Carl Caulkett
Guest





PostPosted: Mon Jan 05, 2004 10:40 pm    Post subject: Re: Iterator vs For loop Reply with quote

Joanna Carter (TeamB) wrote:

Quote:
Marc Rohloff wrote:

For loops are generally considerably faster than for loops

But which for loop is faster? Smile

The first one obviously.

Like, duh!

<g>

--
Carl

Back to top
Ritchie
Guest





PostPosted: Mon Jan 05, 2004 11:47 pm    Post subject: Re: Iterator vs For loop Reply with quote

In article <3ff9e026$1 (AT) newsgroups (DOT) borland.com>, [email]joannac (AT) btinternetXX (DOT) com[/email]
says...
Quote:
Marc Rohloff wrote:
| For loops are generally considerably faster than for loops
But which for loop is faster? Smile

LOL - voops :)

Quote:
Good point, I have the Iterator observe the list and when a deletion occurs,
I can shortcut terminate the Iterator.

My own variant does a nil-and-pack type of operation where deleted items
are nil-ed (and packed when all iterators go away), and every created
iterator gets the Count of the list when it's created, so it never goes
past it even if items are added later. Simplistic way to keep a
relatively consistent view of the list, but it works well :)

Quote:
Joanna

Happy New Year, Joanna :)

-- Ritchie Annand
Senior Software Architect
http://www.malibugroup.com
http://nimble.nimblebrain.net
http://wiki.nimblebrain.net

Back to top
Joanna Carter (TeamB)
Guest





PostPosted: Mon Jan 05, 2004 11:50 pm    Post subject: Re: Iterator vs For loop Reply with quote

Ritchie wrote:

Quote:
Happy New Year, Joanna Smile

Ta muchly and ditto to you :-)

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Bryan Crotaz
Guest





PostPosted: Tue Jan 06, 2004 3:37 am    Post subject: Re: Iterator vs For loop Reply with quote

Quote:
For loops are generally considerably faster than for loops, however an
iterator can offer a good level of abstraction even in a simple case
like this. For example you may later want to make your list a composite.
Well designed iterators can also insulate your code from the impact of
changes (additions and deletions) to the list while you are busy
iterating over it.

ESPECIALLY when multithreading! Don't always want to lock the list to
iterate it!

Bryan



Back to top
Marc Rohloff
Guest





PostPosted: Tue Jan 06, 2004 1:34 pm    Post subject: Re: Iterator vs For loop Reply with quote

Joanna Carter (TeamB) wrote on Mon, 5 Jan 2004 22:03:30 -0000 ...

Quote:
Marc Rohloff wrote:

| For loops are generally considerably faster than for loops

But which for loop is faster? Smile

You guys are a tough audience! But you all understood that what I meant
to say was: For loops are generally considerably faster than iterators.
Right?

Marc Rohloff
marc rohloff at bigfoot dot com

Back to top
Peter Morris [Droopy Eyes
Guest





PostPosted: Tue Jan 06, 2004 1:45 pm    Post subject: Re: Iterator vs For loop Reply with quote

Don't worry Rold, I understood ;-)

Quote:
You guys are a tough audience! But you all understood that what I meant
to say was: For loops are generally considerably faster than iterators.
Right?




Back to top
Rudy Velthuis (TeamB)
Guest





PostPosted: Sun Jan 11, 2004 11:05 am    Post subject: Re: Iterator vs For loop Reply with quote

Marc Rohloff wrote:

Quote:
For loops are generally considerably faster than for loops

Er...
--
Rudy Velthuis (TeamB)

"Hemingway was a jerk."
- Harold Robbins

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.