LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] a quick gcc microoptimisation question

[ILUG] a quick gcc microoptimisation question

P at draigBrady.com P at draigBrady.com
Fri Dec 17 11:04:08 GMT 2004


jm at jmason.org wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> 
> HiLUG,
> 
> quickie: when using gcc's -O switches, what is more efficient:
> 
>     if (condition) {
>       /* more common case */
>     } else {
>       /* less common case */
>     }
> 
> or 
> 
>     if (condition) {
>       /* less common case */
>     } else {
>       /* more common case */
>     }

I don't know what the default is but you can give branch
prediction hints with __builtin_expect() in gcc >= 3,
and playing with that and looking at the resulting assembly
should show the default behaviour.

I find the __[un]likely macros from the linux kernel
the nicest interface to this:

/* __likely etc. is to provide the compiler
    with branch prediction information.
    Search for builtin_expect in `info gcc` */
#if __GNUC__ < 3
     #define __builtin_expect(foo,bar) (foo)
     #define expect(foo,bar) (foo)
#else
     #define expect(foo,bar) __builtin_expect((long)(foo),bar)
#endif
#define __likely(foo) expect((foo),1)
#define __unlikely(foo) expect((foo),0)
 

Note I think tests for NULL (!something) don't need an unlikely
as gcc does that by default itself?

-- 
Pádraig Brady - http://www.pixelbeat.org
--- Following generated by rotagator ---

Changing priority of a process

The nice value (-20 to 19) represents the priority of a process.
The lower the value, the higher the priority (not as nice to others).
For non interactive processes (especially on multi-user systems) do:
   nice low_priority_command
You can retroactively give a process less priority like:
   renice 19 -p low_priority_pid
For e.g. this is useful at the start of long running shell scripts:
   renice 19 -p $$ > /dev/null
Only root can give processes more priority.
--



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell