如何提高C / C ++程序的速度? [英] How increase a C/C++ Program speed ?

查看:82
本文介绍了如何提高C / C ++程序的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C是最快的编程语言。但是,通过使用一些

按位操作,您可以加快程序的速度。

例如,我们讨论交换功能。对于整数交换我们

一般使用它。


void swap(int * a,int * b){

int c;

c = * a;

* b = * a;

* b = c;

}


我们使用局部变量并互相交换varibales,它可能是昂贵的b $ b。我们做这样的交换功能:


void swap(int * a,int * b){

* a ^ = * b;

* b ^ = * a;

* a ^ = * b;

}


是的,这个函数也可以交换整数。但它比第一个更快。也不需要局部变量。

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.

推荐答案

RAYYILDIZ写道:
RAYYILDIZ wrote:
我知道C是最快的进步语言。


您不确定。但是没关系......

但是,通过使用一些
按位操作,您可以加快程序的运行速度。


无论那意味着什么...

例如,我们谈论交换功能。对于整数交换,我们通常会使用它。

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


实际上,那里有一个严重的错误。它应该是


int c = * a;

* a = * b;

* b = c;

}

我们使用局部变量并互相交换varibales,它可能很昂贵。我们做这样的交换功能:

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


一般来说这是一个坏主意。

}

是的,这个函数也会交换整数。但它比第一个更快。也不需要局部变量。
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.




它并不快,因为它在一个假设的情况下不起作用


int a = 42;

swap(& a,& a);


除非它已被证明是你的原件(带有我的更正)交换过于缓慢且影响你的整体程序太多,没有必要做那种微优化的b / b
。请记住,过早优化是

万恶之源。


V



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


RAYYILDIZ写道:
RAYYILDIZ wrote:
我知道C是最快的编程语言。
我不会咬这个flamebait,但是:

我们使用局部变量并互相交换varibales,它可能很贵。我们这样做这个交换函数:
void swap(int * a,int * b){
* a ^ = * b;
* b ^ = * a;
* a ^ = * b;
}
是的,这个函数也可以交换整数。但它比第一个更快。
在我曾经使用的任何CPU上,它都不会。它可能更好用于

非常古老的架构或一些我从未与b $ b工作过的嵌入式系统。

也不要需要一个局部变量。
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.



你错了。大多数架构不能同时在两个内存上操作,因此必须将数据加载到寄存器中。在这个

的情况下,CPU寄存器基本上可以被视为临时变量。


除非我遗漏了什么,否则产生的代码很可能包括

为6 + 2(用于将指针a和b加载到寄存器中)内存读取,

3 xor指令和3次内存写入,除非编译器具有

关于函数参数的知识。 (即a是否等于b)


通常的临时变量交换方法将很容易被编译器优化以加载数据在a和b进入

寄存器,并将它们写回反向存储器位置。

临时变量很可能永远不会被放入内存中。 (2 +

2次读取,2次写入)即使它被放在堆栈上,例如因为你已经禁用了编译器优化,这只会增加一次读取并且每个都写了
,这仍然比XOR方法少得多。


我可能会在这里或那里通过一些指令,取决于

CPU架构,以及如何计算内存偏移量,但我认为我的

点仍然有效。


技术是用于非常古老的寄存器和内存饥饿系统,据我所知,
。它今天不再有用了。


~phil


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


On 2005-03-01 09:53:05 - 0500,RAYYILDIZ < RA ******* @ gmail.com>说:
On 2005-03-01 09:53:05 -0500, "RAYYILDIZ" <ra*******@gmail.com> said:
我知道C是最快的编程语言。


说谁?

但是,通过使用一些
按位操作,你可以加快你的程序。
例如,我们谈谈交换功能。对于整数交换,我们通常会使用它。

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


首先,你写错了。我希望你的意思是:


void swap(int * a,int * b)

{

int c = * a ;

* a = * b;

* b = c;

}


秒,我们已经在标准库中使用了std :: swap:


#include< algorithm>


....

{

int i = 25;

int j = 12;

swap(i,j);

//现在i == 12和j == 25

}

....


std :: swap的优势在于它适用于任何可分配的类型。


我们使用局部变量并相互交换varibales,它可能很昂贵。我们做这样的交换功能:

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


这是一个非常糟糕的主意,原因如下:

1)它'过早的优化。

2)实际上它可能比更明显的交换算法慢一些平台上的价值

3)它是不太可读

4)并不总是正确的:


int i = 25;

int j = 25;

swap(& i,& j);

是的,这个函数也交换了整数。但它比第一个更快。


说谁?

也不需要本地变量。
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.




谁在乎呢?


-

Clark S. Cox,III
cl ******* @ gmail.com



Who cares?

--
Clark S. Cox, III
cl*******@gmail.com


这篇关于如何提高C / C ++程序的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆