Switch in CSharp very slow?

A

aliostad

I have started programming with .NET 2.0 using Microsoft Visual Studio
2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

and I was doing some image processing and it was taking a long time to
process and finally I found that I used a switch and it made it slower:

private static bool GetBitonalValue(byte[] buffer, int x, int y,
int stride, PixelFormat pf, int threshold)
{

switch (pf)
{
case PixelFormat.Format1bppIndexed:
throw new InvalidOperationException("Image is
already bitonal.");
case PixelFormat.Format8bppIndexed:
byte b = buffer[(y * stride) + x];
return (b >= threshold);
case PixelFormat.Format24bppRgb:
byte[] bb = new byte[4];
bb[0] = 255;
Array.Copy(buffer, (y * stride) + x, bb, 1, 3);
Color clr = Color.FromArgb(BitConverter.ToInt32(bb,
0));
return ((clr.B + clr.G + clr.R) >= (threshold *
3));

case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppPArgb:
byte[] bbb = new byte[4];
Array.Copy(buffer, (y * stride) + x, bbb, 0, 4);
Color clr1 =
Color.FromArgb(BitConverter.ToInt32(bbb, 0));
return ((clr1.B + clr1.G + clr1.R) >= (threshold *
3));

default:
throw new NotImplementedException("Pixel format is
not supported.");
}

}

Regardless of the code itself, when I removed the switch it became 10
times faster. For an image of nearly 1000000 pixels it was taking 400ms
and after removing switch it took only 30ms for that part of code.

Is there anyway to optimise switch?
 
L

Lloyd Dupont

I mean, could you repost a sample showing the time lags you mention, but
without code in the case statments?
I' not able to reproduce your slow behavior.
 
A

aliostad

Only image I was using was 8bpp so it was not using array copy at all.
That part of code, reads a single byte from a byte array.

I am sure it is switch. MVPs any ideas?
 
A

Alexander Kolliopoulos

Did you replace the switch with a bunch of if/elses? I'm not sure I
understand what you did after you removed the switch. I'm interested
because I had some code that used to be reasonably interactive, but it
now runs amazingly slow in the 2.0 RTM, and I haven't had time to
investigate why this is.
 
B

Bill Butler

I have started programming with .NET 2.0 using Microsoft Visual Studio
2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

and I was doing some image processing and it was taking a long time to
process and finally I found that I used a switch and it made it slower:

private static bool GetBitonalValue(byte[] buffer, int x, int y,
int stride, PixelFormat pf, int threshold)
{

switch (pf)
{
case PixelFormat.Format1bppIndexed:
throw new InvalidOperationException("Image is
already bitonal.");
case PixelFormat.Format8bppIndexed:
byte b = buffer[(y * stride) + x];
return (b >= threshold);
case PixelFormat.Format24bppRgb:
byte[] bb = new byte[4];
bb[0] = 255;
Array.Copy(buffer, (y * stride) + x, bb, 1, 3);
Color clr = Color.FromArgb(BitConverter.ToInt32(bb,
0));
return ((clr.B + clr.G + clr.R) >= (threshold *
3));

case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppPArgb:
byte[] bbb = new byte[4];
Array.Copy(buffer, (y * stride) + x, bbb, 0, 4);
Color clr1 =
Color.FromArgb(BitConverter.ToInt32(bbb, 0));
return ((clr1.B + clr1.G + clr1.R) >= (threshold *
3));

default:
throw new NotImplementedException("Pixel format is
not supported.");
}

}

Regardless of the code itself, when I removed the switch it became 10
times faster.

Care to demonstrate HOW you removed the swich and still had the same logic.

Also.
1. Why the HECK are you using "return" inside your switch statement??
2. it looks like you call this method repeatedly as you loop over your image. That means that you
execute the same test thousands/millions of times....for no good reason. Test the format once and
than process accordingly. You will also remove tons of method calls in that case.

I doubt that "switch" is the evil villian in this code block.
I rather suspect it is poor design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top