如何优化for循环 [英] how to optimize a for loop

查看:68
本文介绍了如何优化for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个循环(所有变量都是指针):

for(foo = bar; foo baz; foo--)

*(foo + 1)= * foo;

如何优化指针交换以便它使用 - 和++或一元

+ - 而不是+1(如果可能的话 - 我不是'我想要比代码更多#defines

)?

IOCCC获奖者可以真正帮助我:P

I have this loop (all variables are pointers):
for (foo = bar; foo baz; foo--)
*(foo+1) = *foo;
How do I optimize the pointer swap so that it uses -- and ++ or unary
+- instead of +1 (if possible - I don''t want to have more #defines
than code)?
IOCCC winners can really help me with this :P

推荐答案

10月30日下午6:14,andreyvul< andrey .... @ gmail.comwrote:
On Oct 30, 6:14 pm, andreyvul <andrey....@gmail.comwrote:

我有这个循环(所有变量都是指针):

for(foo = bar; foo baz; foo--)

*(foo + 1)= * foo ;

如何优化指针交换以便它使用 - 和++或一元

+ - 而不是+1(如果可能的话 - 我不会想要比代码更多#defines

)?

IOCCC获奖者可以真正帮助我:P
I have this loop (all variables are pointers):
for (foo = bar; foo baz; foo--)
*(foo+1) = *foo;
How do I optimize the pointer swap so that it uses -- and ++ or unary
+- instead of +1 (if possible - I don''t want to have more #defines
than code)?
IOCCC winners can really help me with this :P



memmove(foo + 1,foo,len);


注意memcpy()是这里不允许。

memmove(foo+1, foo, len);

Note that memcpy() is not allowed here.


10月30日晚上9点26分,user923005< dcor ... @ connx.comwrote:
On Oct 30, 9:26 pm, user923005 <dcor...@connx.comwrote:

10月30日下午6:14,andreyvul< andrey .... @ gmail.com写道:
On Oct 30, 6:14 pm, andreyvul <andrey....@gmail.comwrote:

我有这个循环(所有变量都是指针):

for(foo = bar; foo baz; foo--)

*(foo + 1)= * foo;

如何优化指针交换以便它使用 - 和++或一元

+ - 而不是+1(如果可能的话 - 我不想比代码更多#defines

)?

IOCCC获奖者可以真的帮助我:P
I have this loop (all variables are pointers):
for (foo = bar; foo baz; foo--)
*(foo+1) = *foo;
How do I optimize the pointer swap so that it uses -- and ++ or unary
+- instead of +1 (if possible - I don''t want to have more #defines
than code)?
IOCCC winners can really help me with this :P



memmove(foo + 1,foo,len);


请注意memcpy( )这里不允许。


memmove(foo+1, foo, len);

Note that memcpy() is not allowed here.



打破我的就地合并,对不起。与此相同的结果:

for(foo = bar + 1; foo baz;)

*(foo)= *( - foo);

虽然我猜测'两个班轮中的memmove是如何工作的。

有没有办法优化插入排序'的内循环是我

的意思。

完整的排序代码(t是一个值变量):

/ * ...分而治之...... * /

for(;(start< = mid)&&(mid + 1< = end); start ++){

if(* start< *(mid +) 1))//排序(当前元素属于第一个

一半)

继续;

else {/ * true inplace merge需要insersion-排序类似

*方法,因为下半部分的值被插入到

*当前元素* /

//复制第一个下半场中的元素t

t = *(mid + 1);

//将上半场向右移动(这是我试图

优化)

for(shift = mid; shift> = start; shift--)

*(sh ift + 1)= * shift;

//复制t开始

* start = t;

mid ++;

}

}

Breaks my in-place mergesort, sorry. Same result as with this:
for (foo = bar + 1; foo baz;)
*(foo) = *(--foo);
Though I''m guessing that''s how memmove works in a two-liner.
Is there a way to optimize insertion sort''s inner loop is what I
meant.
full sort code (t is a value variable):
/* ... divide and conquer ... */
for (;(start <= mid) && (mid + 1 <= end); start++) {
if (*start < *(mid + 1)) //sorted (current element belongs in 1st
half)
continue;
else { /* true inplace merge requires insersion-sort-like
* methods because a value from the second half is inserted to
* the current element */
//copy the first element in the second half to t
t = *(mid + 1);
//shift first half to the right (this is what I was trying to
optimize)
for (shift = mid; shift >= start; shift--)
*(shift + 1) = *shift;
//copy t to start
*start = t;
mid++;
}
}


andreyvul< an ******** @ gmail.comwrites:
andreyvul <an********@gmail.comwrites:

我有这个循环(所有变量都是指针):

for(foo = bar; foo baz; foo--)

*(foo + 1)= * foo;

如何优化指针交换以便它使用 - 和++或一元

+ - 而不是+1(如果可能的话 - 我不想比代码更多#defines

)?

IOCCC获奖者可以真的帮助我:P
I have this loop (all variables are pointers):
for (foo = bar; foo baz; foo--)
*(foo+1) = *foo;
How do I optimize the pointer swap so that it uses -- and ++ or unary
+- instead of +1 (if possible - I don''t want to have more #defines
than code)?
IOCCC winners can really help me with this :P



是什么让你认为替换+ 1用++表示是优化吗?


-

Keith Thompson(The_Other_Keith) ks * **@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc。 edu / ~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

What makes you think that replacing "+1" with "++" is an optimization?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于如何优化for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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