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.For mat1bppIndexed:
throw new InvalidOperatio nException("Ima ge is
already bitonal.");
case PixelFormat.For mat8bppIndexed:
byte b = buffer[(y * stride) + x];
return (b >= threshold);
case PixelFormat.For mat24bppRgb:
byte[] bb = new byte[4];
bb[0] = 255;
Array.Copy(buff er, (y * stride) + x, bb, 1, 3);
Color clr = Color.FromArgb( BitConverter.To Int32(bb,
0));
return ((clr.B + clr.G + clr.R) >= (threshold *
3));
case PixelFormat.For mat32bppArgb:
case PixelFormat.For mat32bppPArgb:
byte[] bbb = new byte[4];
Array.Copy(buff er, (y * stride) + x, bbb, 0, 4);
Color clr1 =
Color.FromArgb( BitConverter.To Int32(bbb, 0));
return ((clr1.B + clr1.G + clr1.R) >= (threshold *
3));
default:
throw new NotImplementedE xception("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 1923
i think its the array copy....
<al******@gmail .com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.For mat1bppIndexed: throw new InvalidOperatio nException("Ima ge is already bitonal."); case PixelFormat.For mat8bppIndexed: byte b = buffer[(y * stride) + x]; return (b >= threshold); case PixelFormat.For mat24bppRgb: byte[] bb = new byte[4]; bb[0] = 255; Array.Copy(buff er, (y * stride) + x, bb, 1, 3); Color clr = Color.FromArgb( BitConverter.To Int32(bb, 0)); return ((clr.B + clr.G + clr.R) >= (threshold * 3));
case PixelFormat.For mat32bppArgb: case PixelFormat.For mat32bppPArgb: byte[] bbb = new byte[4]; Array.Copy(buff er, (y * stride) + x, bbb, 0, 4); Color clr1 = Color.FromArgb( BitConverter.To Int32(bbb, 0)); return ((clr1.B + clr1.G + clr1.R) >= (threshold * 3));
default: throw new NotImplementedE xception("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.goo glegroups.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.For mat1bppIndexed: throw new InvalidOperatio nException("Ima ge is already bitonal."); case PixelFormat.For mat8bppIndexed: byte b = buffer[(y * stride) + x]; return (b >= threshold); case PixelFormat.For mat24bppRgb: byte[] bb = new byte[4]; bb[0] = 255; Array.Copy(buff er, (y * stride) + x, bb, 1, 3); Color clr = Color.FromArgb( BitConverter.To Int32(bb, 0)); return ((clr.B + clr.G + clr.R) >= (threshold * 3));
case PixelFormat.For mat32bppArgb: case PixelFormat.For mat32bppPArgb: byte[] bbb = new byte[4]; Array.Copy(buff er, (y * stride) + x, bbb, 0, 4); Color clr1 = Color.FromArgb( BitConverter.To Int32(bbb, 0)); return ((clr1.B + clr1.G + clr1.R) >= (threshold * 3));
default: throw new NotImplementedE xception("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******** *************@o 13g2000cwo.goog legroups.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.goo glegroups.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.For mat1bppIndexed: throw new InvalidOperatio nException("Ima ge is already bitonal."); case PixelFormat.For mat8bppIndexed: byte b = buffer[(y * stride) + x]; return (b >= threshold); case PixelFormat.For mat24bppRgb: byte[] bb = new byte[4]; bb[0] = 255; Array.Copy(buff er, (y * stride) + x, bb, 1, 3); Color clr = Color.FromArgb( BitConverter.To Int32(bb, 0)); return ((clr.B + clr.G + clr.R) >= (threshold * 3));
case PixelFormat.For mat32bppArgb: case PixelFormat.For mat32bppPArgb: byte[] bbb = new byte[4]; Array.Copy(buff er, (y * stride) + x, bbb, 0, 4); Color clr1 = Color.FromArgb( BitConverter.To Int32(bbb, 0)); return ((clr1.B + clr1.G + clr1.R) >= (threshold * 3));
default: throw new NotImplementedE xception("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?
In terms of generated machine code, how does hundreds of cases in a switch
differ from hundreds of if-elses? Do compilers or processors do any
optimization on such structured code? Do I need to worry about the
performance, usually?
|
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 Deactivation Team
--
|
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 target1;
string target2;
switch (key) {
|
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 boost its
performance?
Thanks.
Andrea
|
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 when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |