Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How increase a C/C++ Program speed ?

Reply
Thread Tools

How increase a C/C++ Program speed ?

 
 
RAYYILDIZ
Guest
Posts: n/a
 
      03-01-2005
I Know C is the fastest progrmming language. However, by using some
bitwise operation you can get faster the your program.
For instance, we talk about swap function. For a integer swapping we
use this generally.

void swap(int* a,int* b){
int c;
c=*a;
*b=*a;
*b=c;
}

we use a local variable and swapping varibales with each other, it may
be expensive. We do this swap function like that:

void swap(int*a ,int *b){
*a ^= *b;
*b ^= *a;
*a ^= *b;
}

Yeah, this function swap the integers, too. But it is faster than the
first one. Also do not need a local variable.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-01-2005
RAYYILDIZ wrote:
> I Know C is the fastest progrmming language.


You know incorrectly. But never mind...

> However, by using some
> bitwise operation you can get faster the your program.


Whatever that means...

> For instance, we talk about swap function. For a integer swapping we
> use this generally.
>
> void swap(int* a,int* b){
> int c;
> c=*a;
> *b=*a;
> *b=c;


Actually, you have a serious error there. It should be

int c = *a;
*a = *b;
*b = c;

> }
>
> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:
>
> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;


That's a bad idea, generally.

> }
>
> Yeah, this function swap the integers, too. But it is faster than the
> first one. Also do not need a local variable.


It's not faster because it doesn't work in a hypothetical case

int a = 42;
swap(&a, &a);

Unless it's proven that your original (with my corrections) swap is too
slow and affects your overall program too much, there is no need to do
that kind of micro-optimizations. Remember, "Premature optimization is
the root of all evil".

V
 
Reply With Quote
 
 
 
 
Phillip Jordan
Guest
Posts: n/a
 
      03-01-2005
RAYYILDIZ wrote:
> I Know C is the fastest progrmming language.

I won't bite this flamebait, but:

> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:
> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;
> }
>
> Yeah, this function swap the integers, too. But it is faster than the
> first one.

On any CPU I've ever worked with, it won't be. It might work better for
extremely old architectures or some embedded systems that I've never
worked with, though.

> Also do not need a local variable.

You are mistaken. Most architectures can't operate on two memory
operands at once, so the data must be loaded into registers. In this
case, the CPU registers can essentially be regarded as temporary variables.

Unless I'm missing something, the code produced will most likely consist
of 6 + 2 (for loading the pointers a and b into registers) memory reads,
3 xor instructions, and 3 memory writes, unless the compiler has
knowledge about the function parameters. (i.e. whether a can equal b or not)

The usual swapping approach with a temporary temporary variable will
easily be optimised by the compiler to load the data at a and b into
registers, and write them back into the reversed memory locations. The
temporary variable will most likely never even be put into memory. (2 +
2 reads, 2 writes) Even if it is put on the stack, for example because
you've disabled compiler optimisations, that only adds one read and
write each, that is still considerably less than the XOR method.

I'm probably off by a few instructions here or there, depending on the
CPU architecture, and how memory offsets are calculated, but I think my
point still stands.

The technique was used on very old register- and memory-starved systems,
as far as I know. It's no longer useful today.

~phil
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      03-01-2005
On 2005-03-01 09:53:05 -0500, "RAYYILDIZ" <> said:

> I Know C is the fastest progrmming language.


Says who?

> However, by using some
> bitwise operation you can get faster the your program.
> For instance, we talk about swap function. For a integer swapping we
> use this generally.
>
> void swap(int* a,int* b){
> int c;
> c=*a;
> *b=*a;
> *b=c;
> }


First, you've written that incorrectly. I hope you meant:

void swap(int*a,int*b)
{
int c = *a;
*a = *b;
*b = c;
}

Second, we already have std::swap in the Standard Library:

#include <algorithm>

....
{
int i = 25;
int j = 12;
swap(i,j);
//Now i == 12 and j == 25
}
....

std::swap has the advantage that it will work with any assignable type.


> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:
>
> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;
> }




This is a very bad idea, for several reasons:
1) It's premature optimization.
2) It may actually be slower than the more obvious swapping algorithm
on some platforms
3) It is less readable
4) It is not always correct:

int i = 25;
int j = 25;
swap(&i,&j);

> Yeah, this function swap the integers, too. But it is faster than the
> first one.


Says who?

> Also do not need a local variable.


Who cares?

--
Clark S. Cox, III


 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      03-01-2005

"RAYYILDIZ" <> wrote in message
news: oups.com...

>I Know C is the fastest progrmming language. However, by using some
> bitwise operation you can get faster the your program.
> For instance, we talk about swap function. For a integer swapping we
> use this generally.


> void swap(int* a,int* b){
> int c;
> c=*a;
> *b=*a;
> *b=c;
> }


> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:


> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;
> }


> Yeah, this function swap the integers, too. But it is faster than the
> first one. Also do not need a local variable.


Have you actually measured it? On my machine, there's no significant
difference in execution time. Moreover, calling std::swap is significantly
faster than either version.

Using exclusive-or for swapping is a cute trick, but rarely useful in
practice.




 
Reply With Quote
 
Chris Jefferson
Guest
Posts: n/a
 
      03-01-2005
RAYYILDIZ wrote:
> I Know C is the fastest progrmming language. However, by using some
> bitwise operation you can get faster the your program.
> For instance, we talk about swap function. For a integer swapping we
> use this generally.
>
> void swap(int* a,int* b){
> int c;
> c=*a;
> *b=*a;
> *b=c;
> }
>


> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:
>
> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;
> }
>


NNNNNNNNOOOOOOOOOO!!!!!!!!!!!

The XORing version, on C++ compiler I've ever used is SLOWER once you
enable even basic optimisation.

SSSSLLLOOOWWWEEERR!!

(sorry, but this comes up so often). Any modern compiler can easily
remove unused variables, or just keep them in a register. They aren't
stupid!

Lets see what the average compiler will do (I check g++ 3.3 at
optimisation -O1):

For your first swap function, any compiler on any optimisation level
will produce the code (sudo-assembler)

read *a into register 1
read *b into register 2
write register 1 into *b
write register 2 into *a

Your code will produce:

read *a into register 1
read *b into register 2
register 1 = register 1 XOR register 2
register 2 = register 2 XOR register 1
register 1 = register 1 XOR register 2
put register 1 into *a
put register 2 into *b

As you can see, your code is clearly taking longer

Chris
 
Reply With Quote
 
assaarpa
Guest
Posts: n/a
 
      03-01-2005
> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:
>
> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;
> }
>
> Yeah, this function swap the integers, too. But it is faster than the
> first one. Also do not need a local variable.


Depends on your compiler, processor architechture, how the turing complete
(?) system executing the code is configured and other factors someone might
kindly contribute. A neat trick but the possible speedup in a typical case
doesn't come from fact that this tiny fragment of code itself when compiled
to something is faster than what the other function would produce but rather
the fact that it might require one less register (assuming this turing
complete computing system has registers or some level of hierarchy as far as
accessing the variables is concerned speedwise) possibly reducing or
completely avoiding spilling (assuming that your computer and/or
microarchitechture and/or/maybe turing complete computing system and the
compiler implementation are related to the concept of spilling in any shape,
form, method, way or fashion).

Furthermore (insert previous disclaimers enmasse here for security reasons),
your compiler might implement these functions without linking time code
generation, leading to observation that the implementation uses some
fashion, form or equivalent of call/return instructions, or close
resemblance thereof concepts making the issue of avoiding spilling a moot
one from any practical point of view in terms of performance or size of the
compiled code. <- this paragraph makes some rather bold assumptions about
the state of the system you are querying the possible differences of
performance for.

However, you should not concern yourself with this level of optimization
very much as it is highly platform and compiler dependent. The fastest way
to do something is not to do it at all, if you can avoid computing
something: don't compute it. It may be faster overall to do relatively slow
operation only a few times rather than optimized operation many times. Use
std::swap and when it becomes apparent that it is too slow and actual
bottleneck in your application you might find out that no matter how fast
swap you have won't help either.. at that time you will be optimizing
something that actually matters.

I'm not saying it's not all good and beneficial, even sexy if you have the
world's fastest swap.. it's just that either of these two can be faster than
the other one..


 
Reply With Quote
 
Heinz Ozwirk
Guest
Posts: n/a
 
      03-01-2005

"RAYYILDIZ" <> schrieb im Newsbeitrag news: oups.com...
> I Know C is the fastest progrmming language. However, by using some
> bitwise operation you can get faster the your program.
> For instance, we talk about swap function. For a integer swapping we
> use this generally.
>
> void swap(int* a,int* b){
> int c;
> c=*a;
> *b=*a;
> *b=c;
> }
>
> we use a local variable and swapping varibales with each other, it may
> be expensive. We do this swap function like that:
>
> void swap(int*a ,int *b){
> *a ^= *b;
> *b ^= *a;
> *a ^= *b;
> }
>
> Yeah, this function swap the integers, too. But it is faster than the
> first one. Also do not need a local variable.


There is at least one compile which proves you wrong. On my machine, the simple solution using a temporary takes about 19 seconds for 1000 millions of swaps. The obfuscated version takes about 26 seconds for the same number of swaps.

Heinz
 
Reply With Quote
 
dumitru
Guest
Posts: n/a
 
      03-02-2005
Hey, folks... I saw in Doom3 SDK code that Mr. Carmack uses this swap
trick....

So it's safe .

 
Reply With Quote
 
alef
Guest
Posts: n/a
 
      03-02-2005
Il 2005-03-01, RAYYILDIZ <> ha scritto:
> I Know C is the fastest progrmming language. However, by using some
> bitwise operation you can get faster the your program.
> For instance, we talk about swap function. For a integer swapping we
> use this generally.


Does know your i396 compiler tool the instruction XCHG ?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to increase the speed of this program? HYRY Python 18 11-29-2006 05:56 AM
To increase speed on manipulating big arrays in Java with minimal bounds checking, ... Casey Hawthorne Java 16 09-01-2005 06:33 AM
can i increase da simulation speed of design anupam VHDL 4 09-03-2004 01:56 PM
speed speed speed a.metselaar Computer Support 14 12-30-2003 03:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57