"智能"索引 [英] "smart" indexing

查看:65
本文介绍了"智能"索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi NG,


我正在开发一个软件,它基本上包含一个循环,

,其中包含2个部分。这两个部分基本上是相同的,

但是它们在某些变量中有所不同。所以我将变量从foo0

和foo1更改为foo [2],因此我可以将它们编入索引。这样我可以通过

循环两次,但是在0和1之间切换索引。

这很简单。我是这样做的:


int idx;

for(idx = 0 ;; idx =!idx)

{

do_something_with(foo [idx]);

if(somestatement)break;

}


我的问题是:标准是否保证idx =!idx总是在1或0中产生

?如果我希望编译器循环展开它,

会有用吗?或者这只适用于具有明确的

结束语句而不是休息的for循环?


提前致谢,

马克

Hi NG,

I am developing a piece of software, which contains basicly one loop,
which consinsts of 2 parts. Thos two parts are again basicly the same,
but they deiffer in some variables. So I changed the variables from foo0
and foo1 to foo[2], so I can index them. This way I can run through the
loop two times more, but switch the index between 0 and 1.
This is pretty easy. I did it this way:

int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always result
in a 1 or a 0? And if I would want the compiler to loop unroll this,
would that work? Or does that only work for a for loop which has a clear
end statement in stead of a break?

Thanks in advance,
Mark

推荐答案

" Capstar" < SP *** @ eg.homeip.net>在留言中写道

news:3F ************** @ eg.homeip.net ...
"Capstar" <sp***@eg.homeip.net> wrote in message
news:3F**************@eg.homeip.net...

int idx;
for(idx = 0 ;; idx =!idx)
{/ / do_something_with(foo [idx]);
if(somestatement)break;
}

我的问题是:标准是否保证idx =!idx总是在1或0中得到



是的,它确实如此(第6.5.3.3节,第5段)。

如果我希望编译器循环展开它,
那会有用吗?或者这只适用于一个for循环,它有一个清晰的结束语句而不是休息?

int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always result
in a 1 or a 0?
Yes, it does (section 6.5.3.3, paragraph 5).
And if I would want the compiler to loop unroll this,
would that work? Or does that only work for a for loop which has a clear
end statement in stead of a break?




我不知道。 :-\


-

Russell Hanneken
rh ******* @ pobox.com




" Capstar" < SP *** @ eg.homeip.net>在留言中写道

新闻:3F ************** @ eg.homeip.net ...

....

|这很简单。我是这样做的:

|

| int idx;

| for(idx = 0 ;; idx =!idx)

| {

| do_something_with(foo [idx]);

| if(somestatement)break;

| }

|

|我的问题是:

|标准是否保证idx =!idx始终产生1或0?


是。

或者,您可以写:

idx = 1-idx; //不是真的更好

或:

idx =(idx + 1)%2; //可以适用于任何范围(%3,%4等......)


|如果我希望编译器循环展开它,

|那会有用吗?或者这只适用于具有明确

|的for循环结束语句而非休息?


作为所有优化,循环展开是非常特定于实现的。

你必须尝试检查生成的代码是确定的。

或者不用担心,直到你需要优化代码的性能




radix omnia malorum prematurae optimisatia est

- Donald Knuth

干杯,

Ivan

-
http://ivan.vecerina.com


Hi,
"Capstar" <sp***@eg.homeip.net> wrote in message
news:3F**************@eg.homeip.net...
....
| This is pretty easy. I did it this way:
|
| int idx;
| for(idx=0;;idx=!idx)
| {
| do_something_with(foo[idx]);
| if(somestatement) break;
| }
|
| my question is:
| Does the standard guarantee that idx=!idx always result in a 1 or a 0?

Yes.
Alternatively, you could write:
idx = 1-idx; // not really better
or:
idx = (idx+1)%2; // could apply to any range ( %3, %4, etc... )

| And if I would want the compiler to loop unroll this,
| would that work? Or does that only work for a for loop which has a clear
| end statement in stead of a break?

As all optimizations, loop unrolling is very implementation-specific.
You''ll have to try and check the generated code to be sure.
Or not worry about it, until you need to optimize the performance
of your code.

radix omnia malorum prematurae optimisatia est
-- Donald Knuth
Cheers,
Ivan
--
http://ivan.vecerina.com



Capstar写道:
Capstar wrote:

.... snip ...
int idx;
for( idx = 0 ;; idx =!idx)
{
do_something_with(foo [idx]);
if(somestatement)break;
}

我的问题是:标准是否保证idx =!idx总是
导致1或0?
.... snip ...
int idx;
for(idx=0;;idx=!idx)
{
do_something_with(foo[idx]);
if(somestatement) break;
}

my question is: Does the standard guarantee that idx=!idx always
result in a 1 or a 0?




是的。然而,你可以有一个更简单和更清晰的循环:


for(idx = 0; idx< 2; idx ++){

do_something_with(foo [ idx]);

}


(注意几年前空白的短缺结束)


-

回复应该是新闻组

Chuck Falconer度假。



Yes. However you can have a much simpler and clearer loop with:

for (idx = 0; idx < 2; idx++) {
do_something_with(foo[idx]);
}

(Note that the blank shortage ended several years ago)

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.


这篇关于&QUOT;智能&QUOT;索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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