943,594 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 78312
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 3rd, 2006
0

how to convert int to binary?

the best way to do this is to use the bits. The code would be:
C++ Syntax (Toggle Plain Text)
  1. inline string to_binary( const int &x ) {
  2. int t;
  3. string ret;
  4. if( x > 0 ) t = x; else t = -1 * x;
  5. for( int i = 0; i < 32; ++i )
  6. if( t & ( 1 << i ) ) ret.push_back( '1' ); else ret.push_back( '0' );
  7. reverse( ret.begin(), ret.end() );
  8. ret.erase( 0, ret.find( '1' ) );
  9. if( ret.size() == 0 ) return "0";
  10. if( x < 0 ) return '-' + ret;
  11. return ret;
  12. }
Reputation Points: 11
Solved Threads: 0
Newbie Poster
brahle is offline Offline
18 posts
since Mar 2006
Mar 11th, 2006
1

Re: C++ Performance Tips

for( int i = 0; i < 32; ++i )
That's not very portable. Use something in <climits> or the C++ equivalent.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 8th, 2006
1

Re: C++ Performance Tips

Quote originally posted by bitforce ...
An easy way to swap 2 variables without using another variable:

C++ Syntax (Toggle Plain Text)
  1. a=a+b;
  2. b=a-b;
  3. a=a-b;
Easy way? I don't know, but sure it is inefficient (except if the optimizer is really good).

There *are* temporary variables if from an ISO standard point-of-view.


And, this algorithm can only be useful if (assuming an optimizer as good as Turbo C++ 1.0 optimizer or better, knowing that it is a very old compiler, with an obsolete optimizer):
  • Both a and b are in a register (otherwise, I don't know any architecture where the temporary would be avoided)
  • AND There is no builtin XCHG instruction at the assembly level (false for x86 and PowerPC)
  • AND The CPU lacks register (in that case, it is very probable that the CPU has a XCHG instruction).
And, in that case, you may, ocasionally (rarely) gain little performance... In all other cases, you just can pray that the optimizer will understand your bad code, and implement it in a correct way.
Most compilers I know produce bad code for that.

If you use a 25 years old compiler, with a very very bad optimizer, it might also be possible that this compiler lacks register variables...

In that case, the following code:
C++ Syntax (Toggle Plain Text)
  1. a-=b;
  2. b-=a;
  3. a-=b;
Will produce something like (on x86-16, yep I can't imagine any such bad optimizer on x86-32):
C++ Syntax (Toggle Plain Text)
  1. mov ax, [bp-8] ; timings on a k6-2 CPU (1)
  2. sub ax, [bp-4] ; 3
  3. mov [bp-8],ax ; 4 CPU cycles
  4.  
  5. mov ax,[bp-4]
  6. sub ax,[bp-8]
  7. mob [bp-4],ax
  8.  
  9. mov ax, [bp-8]
  10. sub ax, [bp-4]
  11. mov [bp-8],ax ;11.5 CPU cycle (i.e. the last one might be paired with the next instruction)
Note : If the optimizer is even more bad than what I described, the generated code might even be worst.
And, your code (which has temporaries), on the same compiler might be twice as slow...

The classical
C++ Syntax (Toggle Plain Text)
  1. int c;
  2. c=b;
  3. b=a;
  4. a=c;
Will produce something like:
C++ Syntax (Toggle Plain Text)
  1. mov ax,[bp-4]
  2. mov [bp-12],ax
  3.  
  4. mov ax,[bp-8]
  5. mov [bp-4],ax
  6.  
  7. mov ax,[bp-12]
  8. mov [bp-8],ax ; 5.5 CPU cycles (on a K6-2 CPU)
Note : I don't think that the compiler can generate worst code, even if there is no optimizer.

So, my point is that your code is *always* a bad idea.
This algorithm might only be used in some very special contexts, only on weird architectures (I don't know any such architecture, but it probably exists or existed), and at assembly level only!

Please, read my posts (SuperKoko) on these two threads (of another forum):
http://www.codeguru.com/forum/showthread.php?t=386883
http://www.codeguru.com/forum/showthread.php?t=383191

Also, remember that std::swap can be assumed as being optimal on your architecture : The compiler is allowed to do specific optimizations on it.
Reputation Points: 35
Solved Threads: 0
Newbie Poster
SuperKoko is offline Offline
10 posts
since May 2006
Aug 9th, 2006
1

Re: C++ Performance Tips

Quote originally posted by BountyX ...
When you want to pass a constant variable, pass it by reference to save memory.

Example:
int example (const int value); // Uses more memory than
int example (const int &value); // this one
That's the opposite!

Using const references is a good idea for large objects (say, 16 bytes and more).
For very small objects (smaller than a pointer), if you're lucky, you'll not loose performance.
But it is quite probable that the compiler will generate more code, slower code, and use more stack memory (if the argument is not a lvalue).
And, moreover, it is very hard for the compiler to optimize the const-reference thing when the function is used across translation units boundaries (at least, for most compilers), because it must respect a calling convention, and can't know whether the function, internally, gets the address of the const reference.
In that case, the compiler can't replace such const-reference by a simple value.

Please read my posts on these two threads:
http://www.codeguru.com/forum/showthread.php?t=339608
http://www.codeguru.com/forum/showth...=392134&page=2
Reputation Points: 35
Solved Threads: 0
Newbie Poster
SuperKoko is offline Offline
10 posts
since May 2006
Dec 15th, 2006
0

A General Performance Tip - Arithmetic involving powers of 2

Multiplication, division and modulus operations on powers of 2 can be made much faster using bitwise operators.

1. Modulo
Finding the modulus is particularly expensive. This operation can be performed mush faster using the & operator, because
a % n == a & (n-1) where n is power of 2.

2. Division
Division by a power of 2 can done faster using the >> operator.
a / n == a >> m, where n = 2^m

3. Multiplication
This is similar to division, except, use left shift << operator
a * n == a <<m , n = 2^m
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Dec 15th, 2006
0

Re: A General Performance Tip - Arithmetic involving powers of 2

Click to Expand / Collapse  Quote originally posted by vicky_dev ...
Multiplication, division and modulus operations on powers of 2 can be made much faster using bitwise operators.

1. Modulo
Finding the modulus is particularly expensive. This operation can be performed mush faster using the & operator, because
a % n == a & (n-1) where n is power of 2.

2. Division
Division by a power of 2 can done faster using the >> operator.
a / n == a >> m, where n = 2^m

3. Multiplication
This is similar to division, except, use left shift << operator
a * n == a <<m , n = 2^m
Valid, albeit ancient, advice -- much like the XOR swap.

It's one of those things that language implementers knew about ages ago, and should be done by the compiler automagically (or it may take some optimization option).

Bottom line: don't do this unless your compiler sucks. Write code to multiply by 4 like this num*=4. Obfuscating it as num<<=2 will make your code look strangeto newbies, and it will look like a newbish hack to old timers (both making what you write a little suspect).

And don't forget that right-shifting signed values is implementation defined!
Last edited by Dave Sinkula; Dec 15th, 2006 at 9:54 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 21st, 2007
1

Re: C++ Performance Tips

A few more I find useful...

1. Replace switch-case/if-else with array-indexing:
c++ Syntax (Toggle Plain Text)
  1. switch ( queue ) {
  2. case 0 : letter = 'W';
  3. break;
  4. case 1 : letter = 'S';
  5. break;
  6. case 2 : letter = 'U';
  7. break;
  8. }
  9. //or maybe
  10. if ( queue == 0 )
  11. letter = 'W';
  12. else if ( queue == 1 )
  13. letter = 'S';
  14. else
  15. letter = 'U';
  16. //A quicker method is to simply use the value as an
  17. //index into a character array, eg.
  18. static char *classes="WSU";
  19. letter = classes[queue];
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Feb 21st, 2007
0

Re: C++ Performance Tips

2. I'm sure most ppl know this but writing as it's not already mentioned in this topic so far. Use unsigned int stored in registers for loop variables.
Reasons
1. unsigned int arithmatic is faster than signed int.
2. registers are registers ! They're simply faster than memory access.

c++ Syntax (Toggle Plain Text)
  1. for( register unsigned int loop_variable = 0; loop_variable <=100000; i++ )
  2. { /*do your stuff*/ }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Feb 21st, 2007
1

Re: C++ Performance Tips

3. Optimizing the breaking conditions in for loops. If you know that the loop variable's range is from 0 to some +ve number AND it doesn't matter which way you traverse while looping, you can optimize the loop like this:

c++ Syntax (Toggle Plain Text)
  1. //original loop
  2. for( int i = 0; i <= 30; i++ )
  3. {/*do your stuff*/}
  4. //optimized loop
  5. for( int i = 30; i--; )
  6. {/*do your stuff*/}
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Feb 21st, 2007
0

Re: C++ Performance Tips

4, Optimizing very small loops using switch-case.
When you know that the range of loop variable's value is
pretty small avoid the loop altogether.

c++ Syntax (Toggle Plain Text)
  1. //unoptimized code
  2. for( int i = some_small_positive_int; i--; )
  3. {/*do your stuff with i*/}
  4.  
  5. //optimized based on the assumption that some_small_positive_int can only be from 1 to 7.
  6. switch( i )
  7. {
  8. case 7 : /*do your stuff with i*/ i++;
  9. case 6 : /*do your stuff with i*/ i++;
  10. case 5 : /*do your stuff with i*/ i++;
  11. case 4 : /*do your stuff with i*/ i++;
  12. case 3 : /*do your stuff with i*/ i++;
  13. case 2 : /*do your stuff with i*/ i++;
  14. case 1 : /*do your stuff with i*/
  15. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with an overloaded operator
Next Thread in C++ Forum Timeline: Combined accessor/mutator?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC