[OT] Money for nothing, and your bit flips for free?

Nicholas Leippe nick at leippe.com
Wed Feb 7 16:19:52 MST 2007


On Wednesday 07 February 2007 16:13, Steve wrote:
> Turns out I had to create an assignment to get it function as I wanted.
>
> s[y] = ~s[y];

Yes, that would seem more correct.

[snip]

> inline void flip(std::string& s){
> 	int y = 0;
> 	while(y++ < s.length()){
> 		s[y] = ~s[y];
> 	}
> }

Try this optimization:

inline void flip(std::string& s) {
	int len = s.length(); // compute the string length only once
	for (int y = 0; y < len; y++) {
		s[y] = ~s[y];
	}
}


Depending on how std::string.length() is implemented, the compiler may already 
have been able to do this optimization for you.  You can test by comparing 
both sets of code with optimizations turned off.




More information about the PLUG mailing list