Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 165397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:54:50+00:00 2026-05-11T11:54:50+00:00

I’m writing some optimized C code that basically runs through an array and does

  • 0

I’m writing some optimized C code that basically runs through an array and does something to each element. What it does depends on the current value of the element so something like:

for (i=0; i < a_len; i++) {     if (a[i] == 0) {         a[i] = f1(a[i]);     } else if (a[i] % 2 == 0) {         a[i] = f2(a[i]);     } else {         a[i] = 0; } 

I’m returning to C after many years working in dynamic languages, where my practice has been to try to write straightforward code and not create lots of local variables for things that I can just refer to directly, like a[i] above. I am very much aware that best practices are to write readable code and trust that the compiler is smarter than you and will do good optimizations.

If I were writing the code above in assembler, I would load a[i] into a register once and then just use that value each time because I know that a[] is private memory and won’t change between references. However, even a smart compiler might do a load every time because it can’t be sure that the memory hasn’t changed. (Or do I have to explicitly declare ‘a’ volatile for the compiler to not make this optimization?).

So, my question is: should I expect better performance by rewriting with a local variable like so:

for (i=0; i < a_len; i++) {     val = a[i];     if (val == 0) {         a[i] = f1(val);     } else if (val % 2 == 0) {         a[i] = f2(val);     } else {         a[i] = 0; } 

Or does stuff like -O3 take care of this automatically for me? The code I’m optimizing takes days to run, so even modest improvements will make a difference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:54:50+00:00Added an answer on May 11, 2026 at 11:54 am

    The functions f1 and f2 seems to share the same signature. How differently do they behave? Do you really need the check outside? Or, can you embed the logic in one function?

    If you have a if-else ladder instead of only two such functions, try to use an array of function pointers instead. Use the value of a[ i ] to index in to that array and call the correct function.

    Hand-optimization often turns out to be error prone micro-optimization. It’s best to leave this task to the compiler. If you really need to optimize, look at the big picture, think of algorithms, the design, layers etc.

    As for your question: Yes, most compilers are likely to optimize out the memory read should a[ i ] be not declared volatile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 174k
  • Answers 174k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no way to know this. However, I will… May 12, 2026 at 2:53 pm
  • Editorial Team
    Editorial Team added an answer You can use IIS Extention Mapping. For More information click… May 12, 2026 at 2:53 pm
  • Editorial Team
    Editorial Team added an answer Saving files on filesystem but inside a DB Saving files… May 12, 2026 at 2:53 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.