Avatar of effiqua
effiqua

asked on 

gcc code optimization

I have a C function like below which is part of a file xyz.c and the file is compile using O2 optimization using gcc. The function GetValue() is called almost 200million times  to generate specific data. But for this specific operation cond1 is passed as false, hence code under this condition is never exercise. But, why there is lot of CPU difference in executing GetValue function, when the code chunk 'abc' is commented vs un-commented eventhough the code in that block under (cond1 == true) is not exercised at all.

if the code is commented then GetValue() take - 100 cpu seconds
                     uncomented then GetValue() take  - 120 cpu seconds why ? Is there any problem with GCC optimization ?

inline GetValue(int cond1)
{

   if (cond1 == true)
  {

      // -- code chunk 'abc'--

   }
}

Open in new window

C

Avatar of undefined
Last Comment
Kent Olsen
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi effiqua,

Is --code chunk-- the only thing in the *if* statement?  If so, the optimization may well be eliminating the test as it's do-nothing code.


Kent
Avatar of effiqua
effiqua

ASKER

The cond1 is determined dynamically. In this case cond1 is passed as false while calling GetValue().
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C
C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo