Re: C# very optimisation

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Clint Herron <ClintHerron@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> > Could you post a short but complete program which demonstrates that?
> >
> > See http://www.pobox.com/~skeet/csharp/complete.html for details of
> > what I mean by that.
>
> Hrm. You know, I can't reproduce this right now. That's odd. I'm sure I saw
> performance hits on my code, and noticed a significant improvement in my
> profiler runs when I changed this before.
>
> It might have had something to do with inherited classes, but I dunno'.
> Okay, well you got me on this one, I can't show it right now. Hrm. I'll keep
> stewing on this one.

I'll believe it when I see it :)

> > I wouldn't expect the binaries to be any different at all.
> >
> > > Another way to make your code faster is to never use foreach loops.
> >
> > Not significantly, IME.
> >
> > What exactly do you mean by "much" faster?
>
> On the order of 250% (my results right now are showing around 278%)? Would
> you consider that significant?

Only if i do virtually nothing in loops with 100,000,000 iterations
(effectively) like you are in your test case. I very rarely have a loop
where the iteration is actually the bottleneck.

Note that using an array rather than an array list would take the time
down by a factor of 10, and then foreach is faster...

> For instance, I have an application where I'm calculating high resolution
> phase and magnitude response curves in real-time, so I've got thousands of
> data points that I'm looping through at 15-20 fps -- so spending a lot of
> unnecessary time on MoveNext is a big deal to me.

You'd be better off using an array then, at which point the more
readable solution is roughly the same speed as the solution using a for
loop, and much faster than using an ArrayList.

For most apps, however, loops aren't as trivial as the one you posted,
and the bottleneck isn't in iteration.

Even if it *is* in an iteration, it tends to be localised - it's worth
considering micro-optimisations *only* when you've found the
bottleneck.

> > The right way to do something is the most readable way - look how much
> > more readable it is to use foreach than to use the for loop. Very, very
> > few applications have bottlenecks due to this kind of usage rather than
> > algorithmic or IO-based bottlenecks.
>
> *shrug*. When profiling with NProf, I notice a lot of time is spent going to
> collection indexers for MoveNext(), when I could just be incrementing a
> counter and going to the next index.
>
> I agree -- I like the way that foreach looks also, but when I notice a
> significant performance hit when I use foreach, I need to optimize. I wrote
> everything with foreach when I was starting, and then moved away from it
> after everything was working and the boss was complaining about speed issues.

And that's the time to find the bottleneck, and optimise *just* there.
Everywhere other than the bottleneck should use the most readable code.

> It's frustrating to me that not more information is readily available about
> such optimization issues.
>
> BTW, for those who don't want to run the program, here were my results
> (repeatable and irrespective of which search went first):
>
> Min value is: 1669
> Min value is: 1669
> First search took: 00:00:01.2323001
> Second search took: 00:00:03.4364141

My results weren't quite as bad as those - about the same time for the
first search, but always under 3 for the second.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: foreach for Prolog
    ... is a built-in named foreach for describing loops. ... many iteration specifiers you refer to is really fundamental. ... I am working on a compiler to compile foreach ...
    (comp.lang.prolog)
  • Re: foreach enhancement
    ... Additionally we were talking about stating increment values for cases like ... Extra syntax would be required and ... project only to make its loops look better and perform much slower. ... But if you simply could write foreach wouldn't you used ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach loops are sooooo tricky.....
    ... loops are not at all like for loops, ... Translating Fortran to PHP, because hosters won't allow anything else ... I wish PHP would do array and matrix stuff like Fortran or C, ... I find foreach loops to be quite intuitive. ...
    (comp.lang.php)
  • Re: Anything wrong with the way I use "break"s in my loops?
    ... There isn't anything logically wrong the way you use "break" twice to get out of the 2 loops, but a few lines of tedious code. ... foreach ... origWeight = bu.OriginalWeight; ...
    (microsoft.public.dotnet.general)
  • Re: foreach enhancement
    ... >> foreach ) ... > Loops are one of the most common things in programming so making them more ... deserves *no* extra syntax. ... Have you ever designed a compiler? ...
    (microsoft.public.dotnet.languages.csharp)