Naveen's Weblog

Bridge to future

Archive for March, 2010

Why Avoid Exception

Posted by codingsense on March 16, 2010

Hi,

Last week I was optimizing a module, as I was working I found that if exceptions are avoided then we can save lot of fruitful time. So made the below sample to see how much time we can save by avoiding an exception.

In below sample a method just iterates and another method throws an exception.




using System;

namespace ConsoleApplication1
{
class Program    
{
static void Main(string[] args)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

sw.Start();
loopProper();
sw.Stop();
Console.WriteLine("Looping Time : " + sw.ElapsedMilliseconds);

sw.Reset();
sw.Start();
ThrowException();
sw.Stop();
Console.WriteLine("Exception not hanlded Time : " + sw.ElapsedMilliseconds);

Console.Read();
}

static void loopProper()
{
for (long Index = 0; Index < 350000; Index++)
{

}
}

static void ThrowException()
{
long Temp = 0;
long Zero = 0;

try            
{
Temp = Temp / Zero;
}
catch (ArithmeticException ex)
{

}
}
}
}



After running the above sample we can find that an time taken by just one exception is approx around time taken by 3,50,000 iterations. So if we can avoid exceptions then we can do more fruitful work in the same time.

I dont mean that exception handling should not be used, but try to avoid it to maximum. If you can use some if conditions where you know the line might give an exception, and see how the performance increases.

Any about my output, the module had a algorithm that would take 80-85 sec to complete and after optimization its taking 2 sec to complete. Changes in logic of the algorithm and exception handling increased the performance drastically.

Happy learning,
Codingsense :)

Posted in C# | Leave a Comment »

Aborting a suspended thread

Posted by codingsense on March 16, 2010

Hi,

I was working on a multithreading application and found an interesting thing on resuming a thread, so planned to share with you.

When a thread is running if we abort it, it gets stopped. But if a suspended thread is aborted then it gives an exception “Thread is suspended; attempting to abort”.

The below sample demonstrates it, first the thread is started, then suspended and then aborted. Run the application and check the error.




using System;
using System.Threading;

namespace ConsoleApplication1
{
class Program    
{
static void Main(string[] args)
{
Thread thread = new Thread(new ThreadStart(DoSomething));
thread.Start();

for (int Index = 0; Index < 10; Index++)
{
switch (Index)
{
case 4:
thread.Suspend();
Console.WriteLine("Thread suspended");
break;

case 6:
thread.Abort();
Console.WriteLine("Thread aborted");
break;

}

//Main Thread is made to sleep                
Thread.Sleep(300);
}

Console.Read();
}

private static void DoSomething()
{
while (true)
{
Thread.Sleep(300);
Console.WriteLine("I am busy doing work");
}
}
}
}


To overcome this exception we can use Resume to invoke the suspended thread and then abort.
Replace the case 6 with below code snippet.




case 6:
try                        
{
thread.Abort();
}
catch (ThreadStateException)
{
thread.Resume();
}
Console.WriteLine("Thread aborted");
break;


now running the application aborts the thread as expected.
From this sample we can learn that if we use resume on a suspended thread then it starts again, but if we have signalled for abort on that thread and try resume then it will stop the thread in proper manner.

Happy Learning
Codingsense :)

Posted in C# | Tagged: | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.