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 6149747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:30:20+00:00 2026-05-23T19:30:20+00:00

This is probably going to language dependent, but in general, what is the performance

  • 0

This is probably going to language dependent, but in general, what is the performance difference between accessing and writing to an array?

For example, if I am trying to write a prime sieve and am representing the primes as a boolean array.

Upon finding a prime, I can say

for(int i = 2; n * i < end; i++)
{
    prime[n * i] = false;
}

or

for(int i = 2; n * i < end; i++)
{
    if(prime[n * i])
    {
        prime[n * i] = false;
    }
}

The intent in the latter case is to check the value before writing it to avoid having to rewrite many values that have already been checked. Is there any realistic gain in performance here, or are access and write mostly equivalent in speed?

  • 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. Editorial Team
    Editorial Team
    2026-05-23T19:30:21+00:00Added an answer on May 23, 2026 at 7:30 pm

    In general this depends much more on the machine than the programing language. The writes often will take a few more clock cycles because, depending on the machine, more cache values need to be updated in memory.

    However, your second segment of code will be WAY slower, and it’s not just because there’s “more code”. The big reason is that anytime you use an if-statement on most machines the CPU uses a branch predictor. The CPU literally predicts which way the if-statement will run ahead of time, and if it’s wrong it has to backtrack. See http://en.wikipedia.org/wiki/Pipeline_%28computing%29 and http://en.wikipedia.org/wiki/Branch_predictor to understand why.

    If you want to do some optimization, I would recommend the following:

    • Profile! See what’s really taking up time.
    • Multiplication is much harder than addition. Try rewriting the loop so that i += n, and use this for your array index.
    • The loop condition “should” be totally reevaluated at every iteration unless the compiler optimizes it away. So try avoiding multiplication in there.
    • Use -O2 or -O3 as a compiler option
    • You might find that some values of n are faster than others because of cache locality. You might think of some clever ways to rewrite your code to take advantage of this.
    • Disassemble the code and look at what it’s actually doing on your processor
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably explained more easily with an example. I'm trying to find a
I'm probably going to take some heat for this question. But I'd like to
Alright, Im going about this, with what is probably a really complicated solution, but
This is going probably be a general question regarding checksum used for error detection.
this is probably going over the top, but I was after a function that
this is probably going to seem like a daft question but please bear with
It's a theoretical question at this point in time, but something I'm probably going
This probably has a simple answer, but I must not have had enough coffee
This probably sounds really stupid but I have noo idea how to implement jquery's
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my

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.