switch {}块的性能。 [英] Performance of switch{} block.

查看:89
本文介绍了switch {}块的性能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道开关块中的情况序列是否会影响性能。假设我打开一个int,我有4个预期的情况。

让我们说我希望案例3在95%的时间内发生。当案例3首先出现时,开关块

是否会更快地执行(意味着开始执行案例3的代码

更早),而不是显示为第3个

案例:,如下面的代码?


开关(someInt)

{

案例3:

//在这里做东西

休息;

案例1:

//做东西在这里

休息;

案例2:

//在这里做点什么

休息;

案例4:

//在这里做东西

休息;

默认:

//在这里做什么

休息;

}


只是好奇 - 并且认为有人可能已经知道了。


谢谢!

Just wondering if the sequence of the case(s) in the switch block might
impact performance. Suppose I switch on an int, and I have 4 expected cases.
Lets say I expect case 3 to happen 95% of the time. Would the switch block
be expected to execute faster (meaning "start executing the code for case 3
sooner") when case 3 appears first, as opposed to appearing as the 3rd
case:, as in the following code?

switch (someInt)
{
case 3:
// Do stuff here
break;
case 1:
// Do stuff here
break;
case 2:
// Do stuff here
break;
case 4:
// Do stuff here
break;
default:
// Do stuff here
break;
}

Just curious - and thought someone might already know offhand.

Thanks!

推荐答案

Smithers写道:
Smithers wrote:

Just想知道开关组中的情况序列是否会影响性能。假设我打开一个int,我有4个预期的情况。

让我们说我希望案例3在95%的时间内发生。当案例3首先出现时,开关块

是否会更快地执行(意味着开始执行案例3的代码

更早),而不是显示为第3个

案例:,如下面的代码?


开关(someInt)

{

案例3:

//在这里做东西

休息;

案例1:

//做东西在这里

休息;

案例2:

//在这里做点什么

休息;

案例4:

//在这里做东西

休息;

默认:

//在这里做什么

休息;

}
Just wondering if the sequence of the case(s) in the switch block might
impact performance. Suppose I switch on an int, and I have 4 expected cases.
Lets say I expect case 3 to happen 95% of the time. Would the switch block
be expected to execute faster (meaning "start executing the code for case 3
sooner") when case 3 appears first, as opposed to appearing as the 3rd
case:, as in the following code?

switch (someInt)
{
case 3:
// Do stuff here
break;
case 1:
// Do stuff here
break;
case 2:
// Do stuff here
break;
case 4:
// Do stuff here
break;
default:
// Do stuff here
break;
}



原则上,编译器可以构建代码偏移数组(a

" vector table")并简单地使用switch变量进行索引。我认为在C#编译器会这样做的情况下,这个案例

标签都是连续的。


In实践中,我曾经未组装过的每个开关语句都被编译为一个if-then-else-if树。我不知道这是否是

调试模式的函数,或者编译器是否只使用向量表时

有正确的模式。案件陈述的数量。


换句话说,是的,你的案件排序可能很重要。对于

课程,最好对此进行基准测试(在发布模式下,没有涉及调试器的情况下使用
)以查看是否有任何重要的

差异。我会感到惊讶,如果这是一个大问题,在一个循环之外

,每秒重复数百万次。


-


..NET 2.0 for Delphi程序员
www.midnightbeach.com/.net

您需要了解的内容。

In principle, the compiler can build an array of code offsets (a
"vector table") and simply use the switch variable to index into. I am
under the impression that the C# compiler will do this, when the case
tags are all contiguous.

In practice, every switch statement I''ve ever unassembled is compiled
to an if-then-else-if tree. I do not know if this is a function of
debug mode, or whether the compiler will only use a vector table when
there are the "right" number of case statements.

In other words, yes, your case ordering probably does matter. Of
course, it would be a good idea to benchmark this (in Release mode,
without the debugger involved) to see if it makes any significant
difference. I''d be surprised if this was a big deal, outside of a loop
that''s repeated millions of times a second.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.


" Mark R. Dawson" < Ma ********* @ discussion.microsoft.com写信息

新闻:B9 ******************* *************** @ microsof t.com ...
"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:B9**********************************@microsof t.com...

嗨史密瑟斯,

case语句按顺序依次评估

定义[...]
Hi Smithers,
the case statements are evaluated sequentially in the order they are
defined [...]



这是真的吗?


我不知道C#编译器/运行时如何处理事情,但一个好的C $ / $
编译器通常会将switch()语句转换为跳转 - 基于表格

解决方案。如果这些情况是连续的整数,那么可以使用跳转偏移创建一个简单的表格来代码中的正确位置。使用该解决方案,

处理切换条件的时间是线性时间,每个情况下

需要相等的时间(不计算二级问题,如缓存或
管道刷新,当然)。


如果C#做了类似的事情,那么每个案例的顺序都无关紧要。

每个可能的值需要完全相同的时间来处理。


当然,如果C#天真地将switch()语句转换为

的序列if()/否则if()...语句,然后是......第一个是最快的。但是

我没有看到任何文件表明这总是C#所做的事情是带有开关()的



Pete

Is this really true?

I don''t know how the C# compiler/run-time handles things, but a good C
compiler will often convert a switch() statement into a jump-table based
solution. If the cases are contiguous integers, a simple table can be
created with jump offsets to the correct place in code. With that solution,
handling the condition of the switch occurs in linear time, with each case
take equal amounts of time (not counting secondary issues like cache or
pipeline flushes, of course).

If C# does something like that, then the order of each case is irrelevant.
Each possible value takes the exact same time to process.

Of course, if C# naively just turns a switch() statement into a sequence of
if()/else if()... statements, then yes...the first one is the quickest. But
I haven''t seen any documentation that suggests this is always what C# does
with a switch().

Pete


用ILDASM查看生成的IL,你会立即得到你的答案。

提示:编写几个switch语句不同数量的案件;一个好的

编译器,正如你所说,可能会使用不同的技术,具体取决于有多少b / b



Tom Dacon

Dacon软件咨询


" Peter Duniho" < Np ********* @ NnOwSlPiAnMk.com在留言中写道

新闻:12 ************* @ corp.supernews.com .. 。
Look at the generated IL with ILDASM, and you''ll get your answer right away.
Hint: code several switch statements with varying numbers of cases; a good
compiler, as you say, might use different techniques depending on how many
cases there are.

Tom Dacon
Dacon Software Consulting

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:12*************@corp.supernews.com...

" Mark R. Dawson" < Ma ********* @ discussion.microsoft.com写信息

新闻:B9 ******************* *************** @ microsof t.com ...
"Mark R. Dawson" <Ma*********@discussions.microsoft.comwrote in message
news:B9**********************************@microsof t.com...

>嗨史密瑟斯,
案例语句按照它们的定义顺序进行评估[...]
>Hi Smithers,
the case statements are evaluated sequentially in the order they are
defined [...]



这是真的吗?


我不知道C#编译器/运行时如何处理事情,但一个好的C $ / $
编译器通常会将switch()语句转换为基于跳转表的

解决方案。如果这些情况是连续的整数,那么可以使用跳转偏移创建一个简单的表格来代码中的正确位置。有了这个

的解决方案,处理交换条件的时间是线性时间,每个案例需要花费相等的时间(不计算像
$这样的二级问题) b $ b缓存或管道刷新,当然)。


如果C#做了类似的事情,那么每个案例的顺序都无关紧要。

每个可能的值需要完全相同的时间来处理。


当然,如果C#天真地将switch()语句转换为序列

of if()/否则if()...语句,然后是......第一个是最快的。

但是我没有看到任何文件表明这总是C#

带开关()。


Pete


Is this really true?

I don''t know how the C# compiler/run-time handles things, but a good C
compiler will often convert a switch() statement into a jump-table based
solution. If the cases are contiguous integers, a simple table can be
created with jump offsets to the correct place in code. With that
solution, handling the condition of the switch occurs in linear time, with
each case take equal amounts of time (not counting secondary issues like
cache or pipeline flushes, of course).

If C# does something like that, then the order of each case is irrelevant.
Each possible value takes the exact same time to process.

Of course, if C# naively just turns a switch() statement into a sequence
of if()/else if()... statements, then yes...the first one is the quickest.
But I haven''t seen any documentation that suggests this is always what C#
does with a switch().

Pete



这篇关于switch {}块的性能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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