效率问题:需要C天才的帮助 [英] EFFICIENCY question: need help from the C geniuses

查看:47
本文介绍了效率问题:需要C天才的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好 -


我正在尝试使addbitwise函数更有效率。代码

下面是一个二进制数组(大小为5)并执行

按位加法。它看起来很丑陋而且不优雅,但看起来好像工作的b $ b。使用时间,我测量它需要.041s执行,我承认不是很多。但是,问题是这个程序将在我的项目中被多次调用很多次(可能至少有几次,如果不是更多的话),因此,效率非常重要。

任何建议(和解释)?谢谢...!


mark。


int main()

{

int arr1 [5];

int arr2 [5];


arr1 [0] = 0;

arr1 [1] = 1;

arr1 [2] = 0;

arr1 [3] = 0;

arr1 [4] = 0 ;


arr2 [0] = 1;

arr2 [1] = 0;

arr2 [2] = 1;

arr2 [3] = 1;

arr2 [4] = 0;


addbitwise(arr1,arr2);


退出(1);

}


int addbitwise(int x [],int y [])

{

int result [5];

int i,carry = 0;


for(i = 4; i> = 0; i--)

{

result [i] = x [i] ^ y [i]; / *按位的结果

add * /


if(x [i]& y [i])/ * a carry已经发生* /

{

carry = 1;


如果(i!= 0)/ *阻止最后一次迭代* /

{/ *从偷看越界* /

if(x [i-1] == 0)

{

x [i-1] = 1; / *用进位替换下一个0位1 * /

进位= 0;

}


else if(y [i-1] == 0)/ *查看下一个数组位* /

{

y [i-1] = 1;

随身携带= 0;

}

}

}

}


返回(随身携带);

}


_____________________

Mark Fonnemann

波士顿学院

学士,计算机科学2000

MA,数学2002

_____________________

Hello-

i am trying to make the function addbitwise more efficient. the code
below takes an array of binary numbers (of size 5) and performs
bitwise addition. it looks ugly and it is not elegant but it appears
to work. using time, i measured it takes .041s to execute, which i
admit isnt much. but, the problem is that this procedure will be
called many, many times in my project (probably at least a few
thousand times, if not more) so thus, efficiency very is important.
any suggestions (and explanations)? thanks...!

mark.

int main()
{
int arr1[5];
int arr2[5];

arr1[0] = 0;
arr1[1] = 1;
arr1[2] = 0;
arr1[3] = 0;
arr1[4] = 0;

arr2[0] = 1;
arr2[1] = 0;
arr2[2] = 1;
arr2[3] = 1;
arr2[4] = 0;

addbitwise(arr1, arr2);

exit(1);
}

int addbitwise(int x[], int y[])
{
int result[5];
int i, carry = 0;

for (i=4; i>=0; i--)
{
result[i] = x[i] ^ y[i]; /* result of the bitwise
add */

if (x[i] & y[i]) /* a carry has occured */
{
carry = 1;

if (i != 0) /* prevents final iteration */
{ /* from peeking out of bounds */
if (x[i-1] == 0)
{
x[i-1] = 1; /* replace the next 0 bit with the carry''d 1 */
carry = 0;
}

else if (y[i-1] == 0) /* peek at the next array bit */
{
y[i-1] = 1;
carry = 0;
}
}
}
}

return(carry);
}

_____________________
Mark Fonnemann
Boston College
B.A., Computer Science 2000
M.A., Mathematics 2002
_____________________

推荐答案



2003年11月30日星期一,mark写道:

On Mon, 30 Nov 2003, mark wrote:

我试图让addbitwise函数更有效率。


哦亲爱的,我们再来一次...... :)你看,马克,这个新闻组

让很多人问我怎么能使这段代码更快,或者更小,或更多缓存利用,或其他什么?通常是问题

是微不足道的,或者是家庭作业,或两者兼而有之。 [有点像你的那种似乎

。]效率问题的简单答案是:

这取决于。

复杂的答案也是:这取决于。这取决于您的编译器优化级别上的

系统,很多东西

这里甚至没有远程主题。

但是,我们可以通过陈述您可能忽视的明显的事情来帮助我们。例如,为什么你在试图将数组中的整数存储在数组中?或者怎么样?

这个怪物最终不仅存在,而且在

a循环的中间?

代码
下面是一个二进制数组(大小为5)并执行逐位加法。它看起来很丑陋而且不优雅,但似乎工作起来。使用时间,我测量它需要.041s来执行,我承认不是很多。


这取决于。关于你的机器有多快,以及其他需要执行的事情的速度有多快。其他考虑因素。

但是,问题是这个程序将在我的项目中被多次调用(可能至少几千次,如果不是更多的话)因此,效率非常重要。
任何建议(和解释)?谢谢...!

标记。


int main()


int main(void)在这里被许多人所青睐,包括我自己。

{
int arr1 [5];
int arr2 [5];

arr1 [0] = 0;
arr1 [1] = 1;
arr1 [2] = 0;
arr1 [3] = 0;
arr1 [4] = 0;

arr2 [0] = 1;
arr2 [1] = 0;
arr2 [2] = 1;
arr2 [3] = 1;
arr2 [4] = 0;

addbitwise(arr1 ,arr2);


好​​的,第一次优化:


int i1 = 8;

int i2 = 22;

(i1 + i2> 31); / *''addbitwise''的重复效果* /


我打赌,即使考虑到它取决于,我也会更快地运行*批次。

你想要真正的解决方案,你最好写下真正的问题。

什么迫使你使用这些丑陋的位数组在第一个

的地方?你能使用更好的数据结构吗?

exit(1);


非便携式退货代码。出口(EXIT_SUCCESS);或退出(EXIT_FAILURE);

是可移植的,简单和传统的返回0; 。

}
int addbitwise(int x [],int y [])
{
int result [5];
int i,carry = 0;

for(i = 4; i> = 0; i--)
{
result [i] = x [i] ^ y [一世]; / *按位的结果
添加* /

if(x [i]& y [i])/ * a carry已经发生* /
{
carry = 1;

如果(i!= 0)/ *阻止最后的迭代* /
{/ *从偷看超出范围* /
if(x [i -1] == 0)
{
x [i-1] = 1; / *用进位替换下一个0位1 * /
进位= 0;
}
如果(y [i-1] == 0) / *查看下一个数组位* /
{
y [i-1] = 1;
carry = 0;
}
}
}

返回(携带);
}

i am trying to make the function addbitwise more efficient.
Oh dear, here we go again... :) You see, Mark, this newsgroup
gets a lot of people asking "how can I make this code faster, or
smaller, or more cache-utilizing, or whatever?" Usually on problems
that are trivial, or homework, or both. [Kind of like yours seems
to be.] And the simple answer to the efficiency question is:
It depends.
The complex answer is also: It depends. It depends on your
system, on your compiler optimization level, on lots of things
that are not even remotely on-topic here.
But we can help, a little bit, by stating the obvious things that
you may have been overlooking. Such as, "Why on earth are you
trying to store integers in arrays anyway?" Or "How on earth did
this monstrosity end up not only existing, but in the middle of
a loop?"
the code
below takes an array of binary numbers (of size 5) and performs
bitwise addition. it looks ugly and it is not elegant but it appears
to work. using time, i measured it takes .041s to execute, which i
admit isnt much.
It depends. On how fast your machine is, and how fast other
things need to execute. Among other considerations.
but, the problem is that this procedure will be
called many, many times in my project (probably at least a few
thousand times, if not more) so thus, efficiency very is important.
any suggestions (and explanations)? thanks...!

mark.
int main()
int main(void) is preferred by many here, myself included.
{
int arr1[5];
int arr2[5];

arr1[0] = 0;
arr1[1] = 1;
arr1[2] = 0;
arr1[3] = 0;
arr1[4] = 0;

arr2[0] = 1;
arr2[1] = 0;
arr2[2] = 1;
arr2[3] = 1;
arr2[4] = 0;

addbitwise(arr1, arr2);
Okay, first optimization:

int i1 = 8;
int i2 = 22;
(i1+i2 > 31); /* duplicate effects of ''addbitwise'' */

I bet that runs a *lot* faster, even considering that "it depends."
You want real solutions, you''d better write down the real problem.
What is forcing you to use these ugly "bit arrays" in the first
place? Can you use a better data structure?
exit(1);
Non-portable return code. exit(EXIT_SUCCESS); or exit(EXIT_FAILURE);
are portable, as is the simple and traditional return 0; .
}

int addbitwise(int x[], int y[])
{
int result[5];
int i, carry = 0;

for (i=4; i>=0; i--)
{
result[i] = x[i] ^ y[i]; /* result of the bitwise
add */

if (x[i] & y[i]) /* a carry has occured */
{
carry = 1;

if (i != 0) /* prevents final iteration */
{ /* from peeking out of bounds */
if (x[i-1] == 0)
{
x[i-1] = 1; /* replace the next 0 bit with the carry''d 1 */
carry = 0;
}

else if (y[i-1] == 0) /* peek at the next array bit */
{
y[i-1] = 1;
carry = 0;
}
}
}
}

return(carry);
}



这很恶心。既然你还在使用整数,那么试试这个怎么样?


int addbitwise(int x [5],int y [5])

{

int t = 0;

int i;

for(i = 0 ; i< 5; ++ i)

t + =(x [i] + y [i])<< (4-i);

返回(t> 31);

}


[注意:明确使用数组范围内的5 - 请帮助你的

维护程序员!]


肯定有更好的方法可以做到,但直到你解释一下

*为什么*你需要解决这个特殊的作业问题,我不会给b $ b感觉更像是给出更好的答案。


仍然是HTH,

-Arthur


This is disgusting. Since you''re using integers anyway, how''s
about giving this a try:

int addbitwise(int x[5], int y[5])
{
int t = 0;
int i;
for (i=0; i < 5; ++i)
t += (x[i]+y[i]) << (4-i);
return (t > 31);
}

[NB: the explicit use of ''5'' in the array bounds -- help your
maintenance programmers, please!]

There are certainly better ways to do it, but until you explain
*why* you need this particular homework problem solved, I don''t
feel much like giving any better answers.

Still HTH,
-Arthur


mark写道:
mark wrote:
你好 -

我试图让addbitwise函数更有效率。下面的代码
采用二进制数组(大小为5)并执行逐位加法。它看起来很丑陋而且不优雅,但似乎工作起来。使用时间,我测量它需要.041s来执行,我承认不是很多。但是,问题是这个程序在我的项目中会被多次调用(可能至少有几千次,如果不是更多的话)因此,效率非常重要。
任何建议(和解释)?谢谢...!

标记。

int main(void)
{int arr1 [5];
int arr2 [5];

arr1 [0] = 0;
arr1 [1] = 1;
arr1 [2] = 0;
arr1 [3] = 0;
arr1 [4] = 0;

arr2 [0] = 1;
arr2 [1] = 0;
arr2 [2] = 1;
arr2 [3] = 1;
arr2 [4] = 0;

addbitwise(arr1,arr2);
退出(EXIT_SUCCESS);
}

int addbitwise(int x [],int y [])
{int result [5];
int i,carry = 0;

for(i = 4; i> = 0; i--)
{result [i] = x [i] ^ y [i]; / *按位的结果* /
如果(x [i]& y [i])/ * a进位已经发生* /
{carry = 1;
if(i != 0)/ *阻止最后的迭代* /
{if(x [i-1] == 0)
{x [i-1] = 1; / *替换下一个0位with carry''d 1 * /
carry = 0;
}
如果(y [i-1] == 0)/ *偷看下一个数组位* /
{y [i-1] = 1;
carry = 0;
}
}
}
}

返回(携带);
}
Hello-

i am trying to make the function addbitwise more efficient. the code
below takes an array of binary numbers (of size 5) and performs
bitwise addition. it looks ugly and it is not elegant but it appears
to work. using time, i measured it takes .041s to execute, which i
admit isnt much. but, the problem is that this procedure will be
called many, many times in my project (probably at least a few
thousand times, if not more) so thus, efficiency very is important.
any suggestions (and explanations)? thanks...!

mark.

int main(void)
{ int arr1[5];
int arr2[5];

arr1[0] = 0;
arr1[1] = 1;
arr1[2] = 0;
arr1[3] = 0;
arr1[4] = 0;

arr2[0] = 1;
arr2[1] = 0;
arr2[2] = 1;
arr2[3] = 1;
arr2[4] = 0;

addbitwise(arr1, arr2);
exit(EXIT_SUCCESS);
}

int addbitwise(int x[], int y[])
{ int result[5];
int i, carry = 0;

for (i=4; i>=0; i--)
{ result[i] = x[i] ^ y[i]; /* result of the bitwise add */
if (x[i] & y[i]) /* a carry has occured */
{ carry = 1;
if (i != 0) /* prevents final iteration */
{ if (x[i-1] == 0)
{ x[i-1] = 1; /* replace the next 0 bit with the carry''d 1 */
carry = 0;
}
else if (y[i-1] == 0) /* peek at the next array bit */
{ y[i-1] = 1;
carry = 0;
}
}
}
}

return(carry);
}




Mark ...


我'假设你想要执行无符号的两个补码

。为什么不将所有位都放入无符号算术

类型?如果只有五位,则将所有五位放入

unsigned char中。你已经知道两个五位

实体的总和不能超过六位(请注意,一旦所有的传输都被传播,这将返回

):


unsigned char addbitwise(unsigned char * a,unsigned char * b)

{unsigned char t;

while (* b)

{t = * a ^ * b; / *半加法结果* /

* b =(* a& * b)<< 1; / *传播任何进位* /

* a = t; / *准备添加携带* /

};

返回* b>> 5;

}


如果是我的项目,我只需要传入a和b并返回总和:

unsigned char addbitwise(unsigned char a,unsigned char b)

{unsigned char t;

while(b)

{t = a ^ b; / *半加器结果* /

b =(a& b)<< 1; / *传播任何进位* /

a = t; / *准备添加携带* /

};

返回a; / *返还金额* /

}


-

Morris Dovey

West Des Moines ,爱荷华州美国

C链接在 http://www.iedu。 com / c

读我的嘴唇:苹果离树不远。



Mark...

I''m assuming you want to perform unsigned two''s complement
addition. Why not put all the bits into an unsigned arithmetic
type? If there are only five bits, then put all five bits into an
unsigned char. You already know that the sum of two five bit
entities can''t be more than six bits (note that this will return
as soon as all carries have been propagated):

unsigned char addbitwise(unsigned char *a,unsigned char *b)
{ unsigned char t;
while (*b)
{ t = *a ^ *b; /* half-adder result */
*b = (*a & *b) << 1; /* propagate any carries */
*a = t; /* prepare to add carries */
};
return *b >> 5;
}

If it were my project I''d just pass in a and b and return the sum:

unsigned char addbitwise(unsigned char a,unsigned char b)
{ unsigned char t;
while (b)
{ t = a ^ b; /* half-adder result */
b = (a & b) << 1; /* propagate any carries */
a = t; /* prepare to add carries */
};
return a; /* return sum */
}

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn''t fall far from the tree.


在文章< f6 ************************** @ posting.google.com>,
没有************ @ yahoo.com (mark)写道:
In article <f6**************************@posting.google.com >,
no************@yahoo.com (mark) wrote:
你好 -
我想让addbitwise函数更有效率。下面的代码
采用二进制数组(大小为5)并执行逐位加法。它看起来很丑陋而且不优雅,但似乎工作起来。使用时间,我测量它需要.041s来执行,我承认不是很多。但是,问题是这个程序在我的项目中会被多次调用(可能至少有几千次,如果不是更多的话)因此,效率非常重要。
任何建议(和解释)?谢谢...!
Hello-

i am trying to make the function addbitwise more efficient. the code
below takes an array of binary numbers (of size 5) and performs
bitwise addition. it looks ugly and it is not elegant but it appears
to work. using time, i measured it takes .041s to execute, which i
admit isnt much. but, the problem is that this procedure will be
called many, many times in my project (probably at least a few
thousand times, if not more) so thus, efficiency very is important.
any suggestions (and explanations)? thanks...!




您测量了启动和停止程序的时间,没有别的。



You measured the time to start and stop the program, nothing else.


这篇关于效率问题:需要C天才的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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