Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Which is faster in a long loop: testing or exceptions

loop

  • Please log in to reply
5 replies to this topic

#1 rc3250

rc3250

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 08 December 2010 - 07:35 AM

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

  • 0

#2 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 08 December 2010 - 07:40 AM

testing is normally faster
  • 0

#3 rc3250

rc3250

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 08 December 2010 - 07:44 AM

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
  • 0

#4 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 08 December 2010 - 08:02 AM

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

  • -1

#5 rc3250

rc3250

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 08 December 2010 - 08:54 AM

Thanks. This helped me a lot.
  • 0

#6 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 08 December 2010 - 08:59 AM

no problem-o!
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download