Jump to content

Which is faster in a long loop: testing or exceptions

- - - - -

  • Please log in to reply
5 replies to this topic

#1
rc3250

rc3250

    Newbie

  • Members
  • Pip
  • 3 posts
If I want to do a long for loop with code something like this:

for (int i=0; i<BIGNUMBER; i++)

 if (ACONST/i==10) x();


my question is: should I be testing for i!=0 for each iteration or just trap the exception and return to the loop?

in other words:

for (int i =0;i<BIGNUMBER;i++) {

  if (i!=0 && ACONST/i==10) x();

}


or

for (int i=0;i<BIGNUMBER;i++)

{

try {

  if (ACONST/i==10) x();

}

catch (DivideByZeroException z) {

}

}

which is faster? Or about the same?

rc

Edited by Roger, 08 December 2010 - 09:54 AM.
added code tags


#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
testing is normally faster

#3
rc3250

rc3250

    Newbie

  • Members
  • Pip
  • 3 posts
Thank you for your reply. I should have added the following: 0 will occur about 1/256 times on average; and the size of BIGNUMBER could be quite large, like near 1 million. Does that change anything?

rc

#4
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
well, I answered that from a really general sense.
Try Catch blocks are pretty heavy, and are designed to handle exceptions, if you know for sure that a certain scenario could occur, you should try your darnedest to handle it in normal programmatic flow, therefor it wouldn't really be an exception.

But yes, testing if an input is 0 is far more cost effective than waiting for the exception.

but you know, that doesn't mean try catch blocks don't have their place either. And in some cases, you may have great reason to handle it by catching an exception.

for example, if performance isn't your top priority, and you have some standard mechanism for handling exceptions, and in relying on the exception causes a log or audit action to take place for example.

I almost want to compare this using a simple example, and I know its not exactly what your asking, but just wanted you to see how heavy catching is..
see the integer, runtimes, I set it to 10000,
try changing that to 10, then 100, then 1000 and so on..


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace TimeTest {

    class Program {

        static void Main(string[] args) {

            int parsed, runtimes = 10000;

            Console.Write("Checking....");

            DateTime start = DateTime.Now;

            for (int i = 0; i < runtimes; i++) {

                if (Int32.TryParse("ABC", out parsed)) {

                    continue;

                } else {

                    continue;

                }

            }

            DateTime end = DateTime.Now;

            Console.WriteLine((end - start).ToString());


            Console.Write("Catching....");

            start = DateTime.Now;

            for (int i = 0; i < runtimes; i++) {

                try {

                    parsed = Int32.Parse("ABC");

                    continue;

                } catch (Exception) {

                    continue;

                }

            }

            end = DateTime.Now;

            Console.WriteLine((end - start).ToString());


            Console.WriteLine("Press <Enter> to terminate...");

            Console.ReadLine();

        }


        static void x() {

            for (int i = 0; i < 10000; i++) ;

            return;

        }


    }

}

Edited by Roger, 08 December 2010 - 09:55 AM.
added code tags


#5
rc3250

rc3250

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks. This helped me a lot.

#6
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
no problem-o!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users