Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is there a better way to optimise this code

Reply
Thread Tools

is there a better way to optimise this code

 
 
Sid
Guest
Posts: n/a
 
      08-25-2004
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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-25-2004
Sid wrote:
>
> 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.


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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Sid) writes:
> 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.


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) (E-Mail Removed) <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.
 
Reply With Quote
 
Jean-Michel Bechet
Guest
Posts: n/a
 
      08-25-2004

> 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)


Hi,

maybe

if(RGB[0] & RGB[1] & RGB[2] )

You should compile different solutions and compare assembly generated ...


JMB.


 
Reply With Quote
 
Jean-Michel Bechet
Guest
Posts: n/a
 
      08-25-2004

> Hi,
>
> maybe
>
> if(RGB[0] & RGB[1] & RGB[2] )
>


if((RGB[0] & RGB[1] & RGB[2])==255 )

should work better


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-25-2004
CBFalconer <(E-Mail Removed)> writes:
> Sid wrote:
> > 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.

>
> 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.


That might or might not speed things up. Conditional branches can
sometimes cause problems with instruction pipelines (though if that's
the case, the compiler should be smart enough to evaluate the
subexpressions unconditionally).

Try several methods, measure the results, and be prepared for the
tradeoffs to change with the next version of the hardware and/or
compiler.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Fredrik Tolf
Guest
Posts: n/a
 
      08-25-2004
On Tue, 2004-08-24 at 21:35 -0700, Sid wrote:
> 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


Depending on your desire for portability, you may want to look into
optimizing it with platform-specific SIMD code, such as MMX/SSE
instructions on Intel platforms.

Fredrik Tolf


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      08-25-2004
(E-Mail Removed) (Sid) wrote in message news:<(E-Mail Removed). com>...
> 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


would this help any way?

typedef union
{
unsigned a;
unsigned char val[3];
}RGB;

int main(void)
{
RGB rgb;
rgb.a = 0;

rgb.val[0] = 255;
rgb.val[1] = 255;
rgb.val[2] = 255;

if(0x00ffffff == rgb.a){
}

return 0;
}

-Paul
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-25-2004
In <412c3d16$0$8991$(E-Mail Removed) t> "Jean-Michel Bechet" <jmb@_PAS_DE_PUB_bea.be> writes:


>> 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)

>
>maybe
>
>if(RGB[0] & RGB[1] & RGB[2] )


It's actually

if ((RGB[0] & RGB[1] & RGB[2]) == 255) ...

>You should compile different solutions and compare assembly generated ...


Which is going to tell you exactly zilch about which version is likely to
be faster, because the actual data has a strong influence on the original
code behaviour: if RGB[0] != 255 for most pixels, the original version
is likely to execute in less CPU cycles, because it completely ignores
RGB[1] and RGB[2].

The right thing is to ignore the assembly generated by the compiler and
to benchmark the two versions on typical data sets.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      08-25-2004
In article <(E-Mail Removed) >,
Paul <(E-Mail Removed)> wrote:

>typedef union
>{
> unsigned a;
> unsigned char val[3];
>}RGB;
>
>int main(void)
>{
> RGB rgb;
> rgb.a = 0;
>
> rgb.val[0] = 255;
> rgb.val[1] = 255;
> rgb.val[2] = 255;
>
> if(0x00ffffff == rgb.a){


This relies on the representation of integers. On a typical
little-endian machine with integers of 32 bits it will work.
On a big-endian machine, or one with 16-bit integers, or one
with some strange implementation quirk, it won't.

Unless your code is already conditionalised for such things, forget
it.

It may well be worthwhile packing the values into a suitably-sized
integer in a more portable way (eg shifting and adding), especially
if you pass them around as a group in other contexts.

-- Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to optimise this code? Bruno Desthuilliers Python 10 08-22-2007 02:22 PM
How do you optimise your website for better engine rankings Corky NZ Computing 7 05-07-2007 08:58 AM
Optimise my ray tracer Jon Harrop Java 85 06-26-2005 08:07 AM
does the saying "don't optimise" hold true for GUI? - Java 3 03-23-2005 06:16 AM
Two Connections to a Router, how can I optimise ? Ken Computer Support 2 10-05-2003 11:57 PM



Advertisments