[Feature Request] "first:" "last:" sections in a "foreach" block

J

James Curran

I think that for features like this the best way to explore the usage is to
actually use them. Have you considered modifying mono or rotor and adding
the feature(or permutations of it)? It will give you, and others if you
release it, a chance to see how it would really work in real code.

Actually, it had never occured to my the source to a C# compiler would
be available. Though I'm not sure if I want to attempt adding features to
one. (But I have now downloaded the mono source, and will be looking into
it)
 
D

Daniel O'Connell [C# MVP]

James Curran said:
Actually, it had never occured to my the source to a C# compiler would
be available. Though I'm not sure if I want to attempt adding features to
one. (But I have now downloaded the mono source, and will be looking into
it)
LOL, ya, there is atleast mono's compiler(there is a compiler in rotor as
well, but its in C++(although you're probably better equiped to deal with it
than I) written in C#. I have done some fair amount of modification to the
mcs compiler(bout a half dozen experimental features), so I might be able to
help if you have any problems.
 
S

SpookyET

This makes no sense, if you want access to the indexer, use a for loop.
foreach loops are 50% slower than for loops anyway, which means that you
should use them in very few cases.
 
T

Tim Jarvis

SpookyET said:
This makes no sense, if you want access to the indexer, use a for
loop. foreach loops are 50% slower than for loops anyway, which
means that you should use them in very few cases.

50%

That's a very specific number...where did you get that info from ?

Rgds Tim.
 
J

Jon Skeet [C# MVP]

Tim Jarvis said:
50%

That's a very specific number...where did you get that info from ?

And in what context? Depending on the type you're indexing, foreach can
be faster than using an indexer.
 
S

SpookyET

It isn't a real number. There is this episode of the .NET Show called
Code Optimizations at
http://msdn.microsoft.com/theshow/Episode027/default.asp where it shows
you on how to optimize your code. A for loop took 1sec, forceach 1.5
secs, 2 secs. You can test it yourself by downloading the code and
playing with it. The fact is that foreach is way slower and it makes
sense to be slower. Take a look at the code behind forceach at
http://www.jaggersoft.com/csharp_standard/15.8.4.htm and see for yourself!
 
C

cody

It isn't a real number. There is this episode of the .NET Show called
Code Optimizations at
http://msdn.microsoft.com/theshow/Episode027/default.asp where it shows
you on how to optimize your code. A for loop took 1sec, forceach 1.5
secs, 2 secs. You can test it yourself by downloading the code and
playing with it. The fact is that foreach is way slower and it makes
sense to be slower. Take a look at the code behind forceach at
http://www.jaggersoft.com/csharp_standard/15.8.4.htm and see for yourself!


This is not always true. for example with arrays a for loop should perform
with
the same speed as foreach since here are no Enumerators involved.
 
D

Daniel O'Connell [C# MVP]

cody said:
This is not always true. for example with arrays a for loop should perform
with
the same speed as foreach since here are no Enumerators involved.
Also, though I can't remember which structures specifically, there are cases
where foreach will result in better performance than for. Things like linked
lists, trees, etc may be faster via a foreach since its possible for the
foreach to be written so that it doesn't have to iterate the collection
every time. An indexer would.
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
 
T

Tim Jarvis

SpookyET said:
It isn't a real number. There is this episode of the .NET Show
called Code Optimizations at
http://msdn.microsoft.com/theshow/Episode027/default.asp where it
shows you on how to optimize your code. A for loop took 1sec,

The problem with this type of benchmark in a demo is this, the point
that they are trying to make is that you need to look at specific areas
to tune your code, and they are giving a for instance...

Our product managers and technical sales guys do the same kind of thing
when they are demo'ing Optimise it (I work for Borland) and they will
show some code that needs to be optimised, the biggest impact is when
they show reasonable looking code that performs poorly. It is usually a
pretty bogus piece of code to be honest, but that is not the point of
the demo, the demo is to show how the poorly performing code can be
found.

I would not dispute that a foreach performs worse in some situations
than other loop types, and I would agree that if you are code tuning
that this is an area to look at, perhaps using a monitoring tool would
be another course of action ;-) But I would not be so bold as to quote
percentage figures, especially from a demo, that is specifically
designed around tuning code, that's just asking for dispute.

IMO when tuning code, sometimes a piece of code that performs
adequately, but is very clear in it's intent is better to have in your
source than highly performing, but slightly less obvious code, I think
that too many people leave the time taken in trying to understand old
code out of the equation when code tuning.

Please don't take this to mean that I don't advocate tuning code
(including foreach's) because that is *not* the case, but I think that
some sense needs to be applied and not based upon a MS demo in a code
tuning tutorial, the critera needs to be based upon the requirements of
the application balanced against the requirements of maintenance.

Rgds Tim.
 
T

Tim Jarvis

Daniel said:
treating a foreach like a switch would allow you to modify the syntax
without disrupting existing code. Two word keywords are syntactically

For me this would be the key thing, I would hope the obvious thing as
well ;-)

Rgds Tim.
 
C

cody

This is not always true. for example with arrays a for loop should
perform
Also, though I can't remember which structures specifically, there are cases
where foreach will result in better performance than for. Things like linked
lists, trees, etc may be faster via a foreach since its possible for the
foreach to be written so that it doesn't have to iterate the collection
every time. An indexer would.


Additionally, the jitter removes the range-check when enumerating over an
array or a string or a StringBuffer.
 
J

James Curran

SpookyET said:
It isn't a real number. There is this episode of the .NET Show called
Code Optimizations at
http://msdn.microsoft.com/theshow/Episode027/default.asp where it shows
you on how to optimize your code. A for loop took 1sec, forceach 1.5
secs, 2 secs. You can test it yourself by downloading the code and
playing with it.

Try changing the type of the collection. With an ArrayList, for() wins.
But with a Hashtable, foreach() wins by 3X to 4X.

Or better yet... Try changing the collection to a BitArray or a Cache
(or a Queue or a Stack or a StateBag or an XmlNodeList) --- BUT you can't!
You can't index any of those! (But you can use foreach on them)


--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
C

cody

Or better yet... Try changing the collection to a BitArray or a Cache
(or a Queue or a Stack or a StateBag or an XmlNodeList) --- BUT you can't!
You can't index any of those! (But you can use foreach on them)


you CAN index a BitArray.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top