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

  • SEARCH
  • Home
  • 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 8672481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:12:54+00:00 2026-06-12T19:12:54+00:00

If I have a boolean and some code which maybe changes it, and then

  • 0

If I have a boolean and some code which maybe changes it, and then I want to set it to true, should I check if it’s false?

For example:

bool b = false;
// Some code
// Here "b" can be true or false
if (cond) {
    b = true;
}

vs

bool b = false;
// Some code
// Here `b` can be `true` or `false`
if (cond && !b){
    b = true;
}

Which is faster?

Note:

I ask that because of the following implementation of Sieve of Eratosthenes: http://bloc.gerardfarras.com/wp-content/uploads/2011/12/erastotenes.txt

if (( i % divisor == 0 ) && ( numsprimers[i] == 0 )) {
    numsprimers[i] = 1;
}

(If numsprimers[i]==1 it means that i isn’t a prime number. And if it’s 0 it can be prime or not)

  • 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-06-12T19:12:57+00:00Added an answer on June 12, 2026 at 7:12 pm

    It’s being very very nitpicky, but generally speaking it’s better to just change the value.

    Checking and setting a value have roughly the same overhead anyway, so why would you want to have to do both in some cases?

    Now if you’re wondering if you should overwrite some custom type (lets say a list of 100000 words) or if you should check to see if it needs to be overwritten first (let’s say by simply checking a boolean value or a timestamp) then you should check first, because the cost of checking a boolean or timestamp is much less than writing so many words to memory.

    This is of course all dependent on various things such as whether or not the memory you are editing is in cache, how expensive the “check” is, how often you need to overwrite the value versus how often it does not need to be overwritten, and of course the size of the memory.

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

Sidebar

Related Questions

I have a boolean which is set to false when being declared, public bool
I have some vb.net code which should print out labels using Teklynx LabelView software
I have three boolean variables. I need to check if all three are true
I have a model which has a boolean property and some other properties that
I have written some code which passes values between java and c using jni.
I have some code in which I am waiting on the current x,y,z position
I have a dll, which I want to use in my java code. I
I want to stop threads by using a boolean field. I have implemented some
In this case I have some code which is working without problem in an
I have some generic code which I cannot figure out how to legitimately prevent

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.