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? 6 1904
i think its the array copy....
<al******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... 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?
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.
<al******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... 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?
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?
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.
if it's the case, you should post the simplest project you can reproducing
these unfortunate behavior there: http://lab.msdn.microsoft.com/produc...k/Default.aspx
"Alexander Kolliopoulos" <ak******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... 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.
<al******@gmail.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com... 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.
For an image of nearly 1000000 pixels it was taking 400ms and after removing switch it took only 30ms for that part of code. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: He Shiming |
last post by:
Hi,
I just wrote a function that has over 200 "cases" wrapped in a "switch"
statement. I'm wondering if there are performance issues in such
implementation. Do I need to optimize it some way?
...
|
by: Duncan McNutt [BSDM] |
last post by:
Is it possible to have ranges or patterns in a case ?
If not, why wasnt this designed in to make it easier instead of listing
every case value needed?
--
Duncan McNutt
Microsoft Product...
|
by: William Stacey |
last post by:
Using the following code sample:
public byte Get()
{
// <= Possible to switch Here??
lock(syncLock)
{
//Do something in Get().
}
}
|
by: _eddie |
last post by:
Is there a good way to code a switch/case-type construct for maximal
speed? The goal is to parse text key/value pairs.
IOW:
// key = "Text of some kind" // value = "Value Text"
string...
|
by: Chris |
last post by:
Hi,
can you specify a range in a switch - statement ?
switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
|
by: Andrea |
last post by:
I've created an application is CSharp. My problem is that it's very
slow to startup the very first time I run it. The second time it's
much faster. Is this normal? Is there anything I can do to...
|
by: Evan Camilleri |
last post by:
I am going from VB.NET to c#. How can I do the following?
switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
|
by: _DS |
last post by:
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables...
|
by: |
last post by:
Is it fine to call another method from Switch?
Eg.
Switch (stringVar)
{
case ("a"):
somVar = "whatever";
Another_Method(); //call another method
return;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |