Which is more optimal for changing a variable from 1 to 0?

  • Thread starter Jamin2112
  • Start date
  • Tags
    Variable
In summary: On modern CPUs, this is the fastest way to set the register to 0.Why are you even writing this code and micro-optimising it? Just call one of the strcmp functions (or the safe versions if you deal with web etc) from the standard library. More than likely your library implements it as hand-coded, optimised assembler - because it is so commonly used.Nevermind. I just made my function more compact, removing the need for the variable I had before.New function:unsigned short int stringCompare ( char * s1, char * s2 ){ while (*s1 && (*s1++ == *s2
  • #1
Jamin2112
986
12
I'm micro-optimizing my program written in classic C. When I use and unsigned short int for a boolean value, I need to switch between values of 0 and 1, so what I need to know is whether it's more efficient to use bitshift operators or reassignment. In other words, is

unsigned short int i = 1;
i >>= 1;


or

unsigned short int i = 1;
i = 0;


faster for changing i from 1 to 0?
 
Technology news on Phys.org
  • #2
I don't know...how about you do it a million times and time it?

By the way, how many times is this supposed to happen in your program? 'cause if it is not going to happen that many times, I doubt you will notice the difference.
 
  • #3
I think that after compiler optimization, it's the same result. You can check it by doing G++ -S to generate assembly file for the both solution and check the difference. A friend say to me (i don't know if it's true) that if you use 64 Bit variable on a 64 bit device it's faster than a 32 bit variable on a 64 bit machine, So instead of using unsigned int, use size_t which is an unsigned number of size 32 on a 32bit an 64 on a 64 bit computer.

Don't forget -O3 compiler option !
If you want help, post part of your code, maybe we can see another inprovement.
 
  • #4
kroni said:
I think that after compiler optimization, it's the same result. You can check it by doing G++ -S to generate assembly file for the both solution and check the difference. A friend say to me (i don't know if it's true) that if you use 64 Bit variable on a 64 bit device it's faster than a 32 bit variable on a 64 bit machine, So instead of using unsigned int, use size_t which is an unsigned number of size 32 on a 32bit an 64 on a 64 bit computer.

Don't forget -O3 compiler option !
If you want help, post part of your code, maybe we can see another inprovement.

Nevermind. I just made my function more compact, removing the need for the variable I had before.

New function:

Code:
unsigned short int stringCompare ( char * s1, char * s2 )
{
    while (*s1 && (*s1++ == *s2++));
    return (*s1 == *s2);
}

Don't ask what the old function looked like. It was needlessly long.
 
  • #5
I think you may want to check your stringCompare method again, especially for inputs where s2 is a prefix of s1.
 
  • #6
You are using char* as string ! Just use std::string and gain time and energy ! If you don't know std::string class, just ask me, i can help.
Compile with g++ to have BOOLEAN OBJECT ! If you don't want to modify your code you can do that :

#include <string>

int stringCompare(char* s1, char* s2)
{
return std::string(s1).compare(std::string(s2));
}

or something like that !
 
  • #7
Is there something wrong with strcmp?

Jamin2112 said:
Code:
unsigned short int stringCompare ( char * s1, char * s2 )
{
    while (*s1 && (*s1++ == *s2++));
    return (*s1 == *s2);
}

According to this function, "mummy" and "mommy" test as equivalent strings.

(Incidentally, to answer the OP: I remember from learning a bit of assembly language years ago that, at least on x86 processors, the fastest way to set an integer register to zero is to xor it with itself. It's really the compiler's job to handle this sort of thing, though.)
 
  • #8
Why are you even writing this code and micro-optimising it? Just call one of the strcmp functions (or the safe versions if you deal with web etc) from the standard library. More than likely your library implements it as hand-coded, optimised assembler - because it is so commonly used.
 
Last edited:
  • #9
Jamin2112 said:
Nevermind. I just made my function more compact, removing the need for the variable I had before.

New function:

Code:
unsigned short int stringCompare ( char * s1, char * s2 )
{
    while (*s1 && (*s1++ == *s2++));
    return (*s1 == *s2);
}

Don't ask what the old function looked like. It was needlessly long.

Dont waste your time rewriting libraries. Chances are, they do it better than you.

As to why this doesn't work, well, your while loop is true while these conditions are true. In the case of mommy vs mummy

the while loop gets to o vs u.. and compares them
(then increments s1, s2)
they don't match, so it exits the loop
then it compares m to m, and returns true.

Its obvious how to fix this.

wle said:
Is there something wrong with strcmp?
According to this function, "mummy" and "mommy" test as equivalent strings.

(Incidentally, to answer the OP: I remember from learning a bit of assembly language years ago that, at least on x86 processors, the fastest way to set an integer register to zero is to xor it with itself. It's really the compiler's job to handle this sort of thing, though.)

Yes, xoring a register is the fastest way to set it to 0. But most likely his variable will be stored on the stack, so the compiler will likely do something such as mov dword ptr ss: [ebp + x], 0

Jamin2112 said:
I'm micro-optimizing my program written in classic C. When I use and unsigned short int for a boolean value, I need to switch between values of 0 and 1, so what I need to know is whether it's more efficient to use bitshift operators or reassignment. In other words, is

unsigned short int i = 1;
i >>= 1;


or

unsigned short int i = 1;
i = 0;


faster for changing i from 1 to 0?

Something to ponder about your above code.

What if something happens to your unsigned integer value, and its value isn't 1 anymore? Then you'll be surprised to see that your code will evaluate to true before and after the bit shift :P
 
  • #10
Jamin2112 said:
I'm micro-optimizing my program written in classic C. When I use and unsigned short int for a boolean value, I need to switch between values of 0 and 1, so what I need to know is whether it's more efficient to use bitshift operators or reassignment. In other words, is

unsigned short int i = 1;
i >>= 1;


or

unsigned short int i = 1;
i = 0;


faster for changing i from 1 to 0?

kroni said:
I think that after compiler optimization, it's the same result. You can check it by doing G++ -S to generate assembly file for the both solution and check the difference. A friend say to me (i don't know if it's true) that if you use 64 Bit variable on a 64 bit device it's faster than a 32 bit variable on a 64 bit machine, So instead of using unsigned int, use size_t which is an unsigned number of size 32 on a 32bit an 64 on a 64 bit computer.

Don't forget -O3 compiler option !
If you want help, post part of your code, maybe we can see another inprovement.

Your friend is right, using 32 bit operations in 64 bit registers requires extra emitted stuff such as setting carry flag, overflow flag, etc

EDIT: Sorry guys, didn't mean to double post. I was going to edit this post into my last one, but slipped up.
 
  • #11
thanks for the answer !
 

Related to Which is more optimal for changing a variable from 1 to 0?

1. What is the most common method for changing a variable from 1 to 0?

The most common method for changing a variable from 1 to 0 is by using the "if" statement in programming. This allows for a conditional statement to be made, and if the condition is met (in this case, the variable being equal to 1), the variable can be changed to 0.

2. Are there any other methods for changing a variable from 1 to 0?

Yes, there are other methods for changing a variable from 1 to 0. Some programming languages have built-in functions or methods specifically for changing the value of a variable, such as "set" or "update". Additionally, in mathematical and statistical applications, there may be specific functions or equations that can be used to change the value of a variable.

3. Is it more efficient to use a loop or a conditional statement for changing a variable from 1 to 0?

This depends on the specific context and application. In general, using a conditional statement is more efficient as it only needs to check the condition once and then change the variable if necessary. However, in some cases, using a loop may be more efficient if there are multiple variables that need to be changed or if the condition is more complex.

4. Can changing a variable from 1 to 0 have any unintended consequences?

Yes, changing a variable from 1 to 0 can have unintended consequences if not done carefully. For example, if the variable is being used in other parts of the code, changing it may affect the functionality of those parts. It is important to consider the potential consequences and thoroughly test the code after making changes to variables.

5. Are there any best practices for changing a variable from 1 to 0?

One best practice for changing a variable from 1 to 0 is to use meaningful and descriptive variable names. This can help to avoid confusion and potential errors in the code. Additionally, it is important to thoroughly test the code and consider potential consequences before making changes to variables. It can also be helpful to comment the code to explain the reasoning behind changing the variable.

Similar threads

  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
7
Replies
235
Views
11K
Replies
63
Views
4K
  • Programming and Computer Science
Replies
1
Views
745
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top