慢速循环(图像数据访问) [英] Slow For-Loop (Picture Data Access)

查看:100
本文介绍了慢速循环(图像数据访问)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要显示16位数据。为了显示它我将
将一定范围的输入数据压缩为8位(我需要控制

)。所有似乎都工作正常,除了它在

发布和调试模式下都很慢。主要问题是

代码中显示的for循环。没有for循环,我可以获得大约10-20

更新/秒。使用for-loop,我每2-3秒就会得到一次更新。

奇怪的是,当我测量实际时间时(如图所示

贝娄)我得到的值400-600ms。那仍然非常缓慢,但我观察到的是另外5倍的速度。我不知道它来自哪里。


任何关于如何非常显着地改进这个或任何其他

完全不同的解决方案的想法表示赞赏。


谢谢


单一尺度= ...;

bmpData = image.LockBits(rec,ImageLockMode.WriteOnly ,

image.PixelFormat);

ptr = bmpData.Scan0;

//日期时间;

for (i = 0; i< image.Width * image.Height; i ++){

Int32值;


value =(Int32)((数据) [i] -l)* scale);

if(value< 0)value = 0;

else if(value 255)value = 255;

rgbValues [i * 3 + 0] =(字节)值;

rgbValues [i * 3 + 1] =(字节)值;

rgbValues [i * 3 + 2] =(字节)值;

}

//

MessageBox.Show(((TimeSpan)DateTime。 Now.Subtract(d ate))。Milliseconds.ToString());


System.Runtime.InteropServices.Marshal.Copy(rgbVal ues,0,

ptr,numberBytes);

image.UnlockBits(bmpData);

I have 16-bit data that I want to display. In order to display it I
compress a certain range of the input data into 8 bit (I need control
over this). All seems to work ok except that it is dead slow both in
release and debug mode. The main problem is the for-loop shown in the
code bellow. Without the for-loop I can get somewhere around 10-20
updates/second. With the for-loop I get an update every 2-3 seconds.
The strange thing is that when I measure the actual time (as shown
bellow) I get values of 400-600ms. That is still VERY slow but what I
observe is even another 5x slower. I don''t know where that comes from.

Any idea on how to very significantly improve this or any other
completely different solution is appreciated.

Thanks

Single scale = ...;
bmpData = image.LockBits(rec, ImageLockMode.WriteOnly,
image.PixelFormat);
ptr = bmpData.Scan0;
// DateTime time;
for (i = 0; i < image.Width*image.Height; i++) {
Int32 value;

value = (Int32)((data[i]-l)*scale);
if(value < 0) value = 0;
else if(value 255) value = 255;
rgbValues[i * 3 + 0] = (Byte)value;
rgbValues[i * 3 + 1] = (Byte)value;
rgbValues[i * 3 + 2] = (Byte)value;
}
//
MessageBox.Show(((TimeSpan)DateTime.Now.Subtract(d ate)).Milliseconds.ToString());

System.Runtime.InteropServices.Marshal.Copy(rgbVal ues, 0,
ptr, numberBytes);
image.UnlockBits(bmpData);

推荐答案

是的...一个好的开始就是删除来自循环的if()

if(value< 0)value = 0;

else if(value 255)value = 255;


我想你可以用地图做到这一点吗?


您的数组数据[i] -l ...如果你反汇编代码,你得到一个

数组边界检查?





rgbValues [i * 3 + 0] =(字节)值;

rgbValues [i * 3 + 1] =(字节)价值;

rgbValues [i * 3 + 2] =(字节)值;


你应该可以用1写处理rgbvalues作为一个uint *

而不是一个字节数组(然后将3添加到一个字节指针(强制转换为uint

等)


干杯,

格雷格

< hu ******* @ yahoo.com写信息

新闻: 11**********************@p79g2000cwp.googlegr oups.com ...
yes ... a good start would be removing the if() from the loop

if(value < 0) value = 0;
else if(value 255) value = 255;

I would imagine you could do this with a map?

your array data[i]-l ... if you disassemble the code are you getting an
array bounds check?

also

rgbValues[i * 3 + 0] = (Byte)value;
rgbValues[i * 3 + 1] = (Byte)value;
rgbValues[i * 3 + 2] = (Byte)value;

you should be able to do this with 1 write treat rgbvalues as an uint*
instead of a byte array (then add 3 to a byte pointer (cast back to a uint
etc)

Cheers,

Greg
<hu*******@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...

> ;我想要显示16位数据。为了显示它我将b $ b压缩一定范围的输入数据到8位(我需要控制

过了S)。所有似乎都工作正常,除了它在

发布和调试模式下都很慢。主要问题是

代码中显示的for循环。没有for循环,我可以获得大约10-20

更新/秒。使用for-loop,我每2-3秒就会得到一次更新。

奇怪的是,当我测量实际时间时(如图所示

贝娄)我得到的值400-600ms。那仍然非常缓慢,但我观察到的是另外5倍的速度。我不知道它来自哪里。


任何关于如何非常显着地改进这个或任何其他

完全不同的解决方案的想法表示赞赏。


谢谢


单一尺度= ...;

bmpData = image.LockBits(rec,ImageLockMode.WriteOnly ,

image.PixelFormat);

ptr = bmpData.Scan0;

//日期时间;

for (i = 0; i< image.Width * image.Height; i ++){

Int32值;


value =(Int32)((数据) [i] -l)* scale);

if(value< 0)value = 0;

else if(value 255)value = 255;

rgbValues [i * 3 + 0] =(字节)值;

rgbValues [i * 3 + 1] =(字节)值;

rgbValues [i * 3 + 2] =(字节)值;

}

//

MessageBox.Show(((TimeSpan)DateTime。 Now.Subtract(d ate))。Milliseconds.ToString());


System.Runtime.InteropServices.Marshal.Copy(rgbVal ues,0,

ptr,numberBytes);

image。 UnlockBits(bmpData);
>I have 16-bit data that I want to display. In order to display it I
compress a certain range of the input data into 8 bit (I need control
over this). All seems to work ok except that it is dead slow both in
release and debug mode. The main problem is the for-loop shown in the
code bellow. Without the for-loop I can get somewhere around 10-20
updates/second. With the for-loop I get an update every 2-3 seconds.
The strange thing is that when I measure the actual time (as shown
bellow) I get values of 400-600ms. That is still VERY slow but what I
observe is even another 5x slower. I don''t know where that comes from.

Any idea on how to very significantly improve this or any other
completely different solution is appreciated.

Thanks

Single scale = ...;
bmpData = image.LockBits(rec, ImageLockMode.WriteOnly,
image.PixelFormat);
ptr = bmpData.Scan0;
// DateTime time;
for (i = 0; i < image.Width*image.Height; i++) {
Int32 value;

value = (Int32)((data[i]-l)*scale);
if(value < 0) value = 0;
else if(value 255) value = 255;
rgbValues[i * 3 + 0] = (Byte)value;
rgbValues[i * 3 + 1] = (Byte)value;
rgbValues[i * 3 + 2] = (Byte)value;
}
//
MessageBox.Show(((TimeSpan)DateTime.Now.Subtract(d ate)).Milliseconds.ToString());

System.Runtime.InteropServices.Marshal.Copy(rgbVal ues, 0,
ptr, numberBytes);
image.UnlockBits(bmpData);




hu ******* @ yahoo.com 写道:

我有16位数据要显示。为了显示它我将
将一定范围的输入数据压缩为8位(我需要控制

)。所有似乎都工作正常,除了它在

发布和调试模式下都很慢。主要问题是

代码中显示的for循环。没有for循环,我可以获得大约10-20

更新/秒。使用for-loop,我每2-3秒就会得到一次更新。

奇怪的是,当我测量实际时间时(如图所示

贝娄)我得到的值400-600ms。那仍然非常缓慢,但我观察到的是另外5倍的速度。我不知道它来自哪里。


任何关于如何非常显着地改进这个或任何其他

完全不同的解决方案的想法表示赞赏。


谢谢


单一尺度= ...;

bmpData = image.LockBits(rec,ImageLockMode.WriteOnly ,

image.PixelFormat);

ptr = bmpData.Scan0;

//日期时间;

for (i = 0; i< image.Width * image.Height; i ++){

Int32值;


value =(Int32)((数据) [i] -l)* scale);

if(value< 0)value = 0;

else if(value 255)value = 255;

rgbValues [i * 3 + 0] =(字节)值;

rgbValues [i * 3 + 1] =(字节)值;

rgbValues [i * 3 + 2] =(字节)值;

}

//

MessageBox.Show(((TimeSpan)DateTime。 Now.Subtract(d ate))。毫秒。 ToString());


System.Runtime.InteropServices.Marshal.Copy(rgbVal ues,0,

ptr,numberBytes);

image.UnlockBits(bmpData);
I have 16-bit data that I want to display. In order to display it I
compress a certain range of the input data into 8 bit (I need control
over this). All seems to work ok except that it is dead slow both in
release and debug mode. The main problem is the for-loop shown in the
code bellow. Without the for-loop I can get somewhere around 10-20
updates/second. With the for-loop I get an update every 2-3 seconds.
The strange thing is that when I measure the actual time (as shown
bellow) I get values of 400-600ms. That is still VERY slow but what I
observe is even another 5x slower. I don''t know where that comes from.

Any idea on how to very significantly improve this or any other
completely different solution is appreciated.

Thanks

Single scale = ...;
bmpData = image.LockBits(rec, ImageLockMode.WriteOnly,
image.PixelFormat);
ptr = bmpData.Scan0;
// DateTime time;
for (i = 0; i < image.Width*image.Height; i++) {
Int32 value;

value = (Int32)((data[i]-l)*scale);
if(value < 0) value = 0;
else if(value 255) value = 255;
rgbValues[i * 3 + 0] = (Byte)value;
rgbValues[i * 3 + 1] = (Byte)value;
rgbValues[i * 3 + 2] = (Byte)value;
}
//
MessageBox.Show(((TimeSpan)DateTime.Now.Subtract(d ate)).Milliseconds.ToString());

System.Runtime.InteropServices.Marshal.Copy(rgbVal ues, 0,
ptr, numberBytes);
image.UnlockBits(bmpData);



什么是rgbValues的类型?

What is the type of "rgbValues"?


Greg,

这是一个很好的建议。我要尝试的是设置一个包含2 ^ 16个条目的

表,然后只对每个值进行查找。另外

为了让事情变得更容易我将使用RGBA而不是RGB因为

对齐4个字节。说实话,根据我的经验,我不是那个

相信这会带来30-60倍的改善但是值得一试

尝试。老实说,我相对简单的循环大约200k应该没有

环境需要2-3秒。也许有一些类型的错过匹配

占用了所有这些时间。


谢谢


Greg Young写道:
Greg,

This is a great suggestion. What I am going to try is setting up a
table with 2^16 entries and then just do a lookup for each value. Also
in order to make things easier I will use RGBA rather then RGB as that
aligns to 4 bytes. To be honest, from my experience I am not that
confident that this will give a 30-60x improvement but it''s worth a
try. Honestly, I relatively simple loop of around 200k should in no
circimstances take 2-3 seconds. Maybe there is some type miss-match
that takes up all this time.

Thanks

Greg Young wrote:

yes ...一个好的开始就是从循环中移除if()


if(value < 0)value = 0;

else if(value 255)value = 255;


我想你可以用地图做到这一点?


你的数组数据[i] -l ...如果你反汇编代码你得到一个

数组边界检查?





rgbValues [i * 3 + 0] =(字节)值;

rgbValues [i * 3 + 1] =(字节值;

rgbValues [i * 3 + 2] =(字节)值;


你应该可以用1写处理rgbvalues来做到这一点作为一个uint *

而不是一个字节数组(然后将3添加到一个字节指针(强制转换为uint

等)


干杯,

格雷格

< hu*******@yahoo.com写信息

news:11 ********************** @ p79g2000cwp。 googlegr oups.com ...
yes ... a good start would be removing the if() from the loop

if(value < 0) value = 0;
else if(value 255) value = 255;

I would imagine you could do this with a map?

your array data[i]-l ... if you disassemble the code are you getting an
array bounds check?

also

rgbValues[i * 3 + 0] = (Byte)value;
rgbValues[i * 3 + 1] = (Byte)value;
rgbValues[i * 3 + 2] = (Byte)value;

you should be able to do this with 1 write treat rgbvalues as an uint*
instead of a byte array (then add 3 to a byte pointer (cast back to a uint
etc)

Cheers,

Greg
<hu*******@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...

我有16位数据要显示。为了显示它我将
将一定范围的输入数据压缩为8位(我需要控制

)。所有似乎都工作正常,除了它在

发布和调试模式下都很慢。主要问题是

代码中显示的for循环。没有for循环,我可以获得大约10-20

更新/秒。使用for-loop,我每2-3秒就会得到一次更新。

奇怪的是,当我测量实际时间时(如图所示

贝娄)我得到的值400-600ms。那仍然非常缓慢,但我观察到的是另外5倍的速度。我不知道它来自哪里。


任何关于如何非常显着地改进这个或任何其他

完全不同的解决方案的想法表示赞赏。


谢谢


单一尺度= ...;

bmpData = image.LockBits(rec,ImageLockMode.WriteOnly ,

image.PixelFormat);

ptr = bmpData.Scan0;

//日期时间;

for (i = 0; i< image.Width * image.Height; i ++){

Int32值;


value =(Int32)((数据) [i] -l)* scale);

if(value< 0)value = 0;

else if(value 255)value = 255;

rgbValues [i * 3 + 0] =(字节)值;

rgbValues [i * 3 + 1] =(字节)值;

rgbValues [i * 3 + 2] =(字节)值;

}

//

MessageBox.Show(((TimeSpan)DateTime。 Now.Subtract(d ate))。Mi lliseconds.ToString());


System.Runtime.InteropServices.Marshal.Copy(rgbVal ues,0,

ptr,numberBytes);

image.UnlockBits(bmpData);
I have 16-bit data that I want to display. In order to display it I
compress a certain range of the input data into 8 bit (I need control
over this). All seems to work ok except that it is dead slow both in
release and debug mode. The main problem is the for-loop shown in the
code bellow. Without the for-loop I can get somewhere around 10-20
updates/second. With the for-loop I get an update every 2-3 seconds.
The strange thing is that when I measure the actual time (as shown
bellow) I get values of 400-600ms. That is still VERY slow but what I
observe is even another 5x slower. I don''t know where that comes from.

Any idea on how to very significantly improve this or any other
completely different solution is appreciated.

Thanks

Single scale = ...;
bmpData = image.LockBits(rec, ImageLockMode.WriteOnly,
image.PixelFormat);
ptr = bmpData.Scan0;
// DateTime time;
for (i = 0; i < image.Width*image.Height; i++) {
Int32 value;

value = (Int32)((data[i]-l)*scale);
if(value < 0) value = 0;
else if(value 255) value = 255;
rgbValues[i * 3 + 0] = (Byte)value;
rgbValues[i * 3 + 1] = (Byte)value;
rgbValues[i * 3 + 2] = (Byte)value;
}
//
MessageBox.Show(((TimeSpan)DateTime.Now.Subtract(d ate)).Milliseconds.ToString());

System.Runtime.InteropServices.Marshal.Copy(rgbVal ues, 0,
ptr, numberBytes);
image.UnlockBits(bmpData);


这篇关于慢速循环(图像数据访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆