Hi Jon.
About my *that* long time it's something I remembered from a discussion on
Microsoft C++ and Borland Delphi compilers. I have no links, sorry.
Regarding your test; Was it compiled in debug or release mode? Also, for
such a small scale test, using DateTime.Now to time performance is way to
crude.
The point is still made clear: Setting up a try-block take *some* time. For
tight loops there is a noticable penalty. However, getting rid of try blocks
for performance considerations is just plain wrong. Agreed?
Best Regards
- Michael S
ps.
On a more funny side: A friend of mine applied for work at a game-shop. As
he being brainwashed by managed code (mainly in Java), at the job interview,
he started talking about the importance of exception handling, assertions,
interfaces and factories for making robust quality code. Their response
was: - We do computer games. Most our code is in asm!
... He didn't get the job... =)
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b324b39c43ae90498abce@msnews.microsoft.c om...[color=blue]
> Michael S <a@b.c> wrote:[color=green]
> > This is not true.
> >
> > Setting up the try-block do indeed cost several 100'000 cycles.[/color]
>
> While I agree it's not absolutely free, I don't think it takes *that*
> much time. For instance:
>
> using System;
>
> public class Test
> {
> static int x;
> static int y=0;
>
> const int Iterations = 100000000;
>
> static void Main()
> {
> y=1;
> DateTime start=DateTime.Now;
> for (int i=0; i < Iterations; i++)
> {
> IncX();
> }
> DateTime end=DateTime.Now;
>
> Console.WriteLine ("Time for {0} iterations: {1}",
> Iterations, end-start);
> }
>
> static void IncX()
> {
> try
> {
> if (y==0)
> {
> throw new Exception ("Whoops");
> }
> x++;
> }
> catch (Exception e)
> {
> throw new ApplicationException("Yikes "+e.Message);
> }
> }
> }
>
> On my P4/3GHz laptop, the results are that it takes about a second for
> the 100,000,000 iterations. If each iteration were taking even one
> hundred thousand cycles, that would suggest that my laptop were capable
> of executing 10^13 cycles per second. I don't believe that's the case.
>
> Note that if I replace IncX with:
>
> [MethodImpl(MethodImplOptions.NoInlining)]
> static void IncX()
> {
> x++;
> }
>
> the test takes between 0.4 and 0.8 seconds. Say it takes about half the
> time of the version which includes try/catch. That suggests that
>
> method invocation + try/catch + increment takes twice as long as
> method invocation + increment
>
> In other words, the cost of setting up a try/catch is the same as a
> method invocation and an increment. Do you really think that *that*
> takes several hundred thousand cycles?
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]