是否有更好的方法来优化此代码 [英] is there a better way to optimise this code
问题描述
我正在编写一个应用程序,通过测试
来查找白色像素,如果所有的R,G,B值都是255即我使用
if(RGB [0] == 255&& RGB [1] == 255&& RGB [2] == 255)(假设RGB是一个
指向unsigned char的指针)
这个语句是针对页面中的所有像素执行的,所以如果我可以
找到一个更好的方法来做到这一点,我可以节省很多cpu
周期。
我会非常感谢任何反馈
Hi,
I am writing an application where I look for a white pixel by testing
if all the R,G,B values are 255 i.e. I use
if(RGB[0] == 255 && RGB[1] == 255 && RGB[2] == 255) (assuming RGB is a
pointer to unsigned char)
This statement gets executed for all the pixels in a page, so if I can
find a better way to do this, I could potentially save a lot of cpu
cycles.
I would greatly appreciate any feedback
推荐答案
Sid写道:
我正在编写一个应用程序,我在那里通过
测试来查找白色像素所有的R,G,B值都是255,即我使用
if(RGB [0] == 255&& RGB [1] == 255&& RGB [2] = = 255)(假设RGB是一个指向unsigned char的指针)
这个语句是针对页面中的所有像素执行的,所以
如果我能找到更好的方法o这样做,我可能会节省很多cpu周期。
I am writing an application where I look for a white pixel by
testing if all the R,G,B values are 255 i.e. I use
if(RGB[0] == 255 && RGB[1] == 255 && RGB[2] == 255) (assuming
RGB is a pointer to unsigned char)
This statement gets executed for all the pixels in a page, so
if I can find a better way to do this, I could potentially
save a lot of cpu cycles.
大多数时候只有该测试的第一部分是
已执行,因此效率比看上去好。测试的订单
可能会有所不同。
-
查克F(cb ****** **@yahoo.com)(cb********@worldnet.att.net)
可用于咨询/临时嵌入式和系统。
< ; HTTP://cbfalconer.home.att.net>使用worldnet地址!
Most of the time only the first portion of that test will be
executed, so the efficiency is better than it appears. The order
of the tests might make a difference.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
si ***** ****** @gmail.com (Sid)写道:
我正在编写一个应用程序,我通过测试来查找白色像素,如果所有的R都是G,B值是255,即使用
if(RGB [0] == 255&& RGB [1] == 255&& RGB [2] == 255)(假设RGB是指向unsigned char的指针。
这个语句是针对页面中的所有像素执行的,所以如果我能找到更好的方法来做到这一点,我可能会节省很多cpu
周期。
I am writing an application where I look for a white pixel by testing
if all the R,G,B values are 255 i.e. I use
if(RGB[0] == 255 && RGB[1] == 255 && RGB[2] == 255) (assuming RGB is a
pointer to unsigned char)
This statement gets executed for all the pixels in a page, so if I can
find a better way to do this, I could potentially save a lot of cpu
cycles.
有可能像
((RGB [0]<< 16 | RGB [1]<< 8 | RGB [2])== 0xFFFFFF)
可能更快,因为它避免了通过直接汇编&&&而产生的条件分支结果
运营商。与往常一样,
唯一可以确定的方法是测量实际性能。
检查生成的代码也可能具有指导意义。
可能需要一些明智的演员;如果unsigned int少于24位宽,那么它可能会溢出。
-
Keith Thompson(The_Other_Keith)< a href =mailto:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
It''s possible that something like
if ((RGB[0]<<16 | RGB[1]<<8 | RGB[2]) == 0xFFFFFF)
might be faster, since it avoids the conditional branches that result
from a straightforward compilation of the "&&" operator. As always,
the only way to be sure is to measure the actual performance.
Examining the generated code might also be instructive.
Some judicious casting might be called for; if unsigned int is less
than 24 bits wide, it could overflow.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
我正在编写一个应用程序,我通过测试来查找白色像素
R,G,B值是255,即我使用
if(RGB [0] == 255&& RGB [1] == 255&& RGB [2] == 255)(假设RGB是指向unsigned char的指针)
I am writing an application where I look for a white pixel by testing
if all the R,G,B values are 255 i.e. I use
if(RGB[0] == 255 && RGB[1] == 255 && RGB[2] == 255) (assuming RGB is a
pointer to unsigned char)
也许
if(RGB [0]& RGB [1]& RGB [2])
您应该编译不同的解决方案并比较生成的汇编。 ..
JMB。
Hi,
maybe
if(RGB[0] & RGB[1] & RGB[2] )
You should compile different solutions and compare assembly generated ...
JMB.
这篇关于是否有更好的方法来优化此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!