• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Referencing struct fields in C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Referencing struct fields in C


  • Subject: Re: Referencing struct fields in C
  • From: Graham Cox <email@hidden>
  • Date: Tue, 31 May 2011 01:04:13 +1000

On 30/05/2011, at 11:30 PM, Oleg Krupnov wrote:

> In fact, in the meantime I created a quick and dirty prototype to
> measure this operation isolated, and it seems that out of 3 options
> (plain if, function pointer, and pointer arithmetics) the plain if is
> the winner, surprisingly :)

Not that surprising. Modern CPUs are highly pipelined, and a conditional like that won't slow things if it's repeatedly coming up with the same answer.

> One thing that bothers me though. How the compiler will understand
> that the state is loop-invariant? Should I necessarily declare the
> checked boolean state as a local stack variable (unlike to a class
> member variable that may change as a side-effect, if foo() was a class
> method)?

No, just leave it. It can see that it's loop-invariant because it's passed in as a value to the function - within the function nothing ever changes its value. Copying it to another variable might do more harm than good (though I suspect the optimizer wouldn't be fazed by that).

Another way to "optimise" in a simple manner is just to have two loops, one for true and one for false. Effectively that's how the optimizer rewrites your code anyway.

--Graham



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Referencing struct fields in C
      • From: Robert Martin <email@hidden>
References: 
 >Referencing struct fields in C (From: Oleg Krupnov <email@hidden>)
 >Re: Referencing struct fields in C (From: Oleg Krupnov <email@hidden>)
 >Re: Referencing struct fields in C (From: Graham Cox <email@hidden>)
 >Re: Referencing struct fields in C (From: Oleg Krupnov <email@hidden>)
 >Re: Referencing struct fields in C (From: Graham Cox <email@hidden>)
 >Re: Referencing struct fields in C (From: Oleg Krupnov <email@hidden>)

  • Prev by Date: Re: Cocoa-dev Digest, Vol 8, Issue 383
  • Next by Date: Re: Why does NSArray count return NSUInteger?
  • Previous by thread: Re: Referencing struct fields in C
  • Next by thread: Re: Referencing struct fields in C
  • Index(es):
    • Date
    • Thread