[2.0] Help speeding up pixel color code [resolved]
Code:
public static string OIR(string strPicPath)
{
int y = 0;
int x = 0;
DateTime tmStart = DateTime.Now;
Bitmap b = new Bitmap(strPicPath);
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
byte red, green, blue;
for (y = 0; y < b.Height; y++)
{
for (x = 0; x < b.Width; x++)
{
blue = p[0];
green = p[1];
red = p[2];
if (blue < 40 && green < 90 && red < 80)
{
DateTime tmEnd = DateTime.Now;
TimeSpan tmDiff = tmEnd - tmStart;
//MessageBox.Show ("blue: " + blue + "green: " + "red: " + red);
MessageBox.Show("x: " + x + "y: " + y);
MessageBox.Show(tmDiff.TotalMilliseconds.ToString());
return "&x=" + x + "&y=" + y;
}
p += 3;
}
p += nOffset;
}
}
return "&x=" + x + "&y=" + y;
}
The code above is a function to find the x and y coordinates of a dark pixel. It works fine, but it's really slow, it take around 150ms on a 220x180 pixel image. In vb6 i was able to use dma and it took less than a ms, so i assume there's a better way. If someone could tell me what way would be faster that would be great. Thanks
Re: [2.0] Help speeding up pixel color code
.NET Framework runs slower than machine code, But I don't know, I think this is too slow even for .NET
Re: [2.0] Help speeding up pixel color code
i figured out it was something with debug mode, once i used released it dropped to 0ms.
Re: [2.0] Help speeding up pixel color code [resolved]
Weird code.
Actually .net code does run quite fast, assuming you select the right optimisations and target the exe at the right hardware.
Re: [2.0] Help speeding up pixel color code [resolved]
my guess is that it may have something to do with memory address validation or array bounds checking.
don't have a compiler right now, but the following code may possibly work if you still want debug mode:
Code:
public static string OIR(string strPicPath)
{
int y = 0;
int x = 0;
DateTime tmStart = DateTime.Now;
Bitmap b = new Bitmap(strPicPath);
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width * 3;
byte red, green, blue;
for (y = 0; y < b.Height; y++)
{
for (x = 0; x < b.Width; x++)
{
blue = *p++; // changed
green = *p++; // changed
red = *p++; // changed
if (blue < 40 && green < 90 && red < 80)
{
DateTime tmEnd = DateTime.Now;
TimeSpan tmDiff = tmEnd - tmStart;
//MessageBox.Show ("blue: " + blue + "green: " + "red: " + red);
MessageBox.Show("x: " + x + "y: " + y);
MessageBox.Show(tmDiff.TotalMilliseconds.ToString());
return "&x=" + x + "&y=" + y;
}
//p += 3; // removed this line
}
p += nOffset;
}
}
return "&x=" + x + "&y=" + y;
}
i'm not that good with unsafe code, i'm mostly going with c++ knowledge, but that got rid of the indexing operator, so it might help. you could also try caching b.Width and b.Height to local vars. hey, just some ideas :bigyello:
Re: [2.0] Help speeding up pixel color code [resolved]
Also, you'll get speed increases if you don't use inline postfix increments...
blue = *p++;
is slower than
blue = *p;
p++;
Althrough this does often depend on whats happening around this code. If you have a lot of complex lines of code with this kind of operator usage consider moving them on to separate lines. Inner loops can become noticably faster, as it makes the compiler's job easier.