English 中文(简体)
本案中是否有较快的替代物?
原标题:Is there a faster alternative to if-else in this case?
while(some_condition){  
    if(FIRST)  
    {   
        do_this;  
    }  
    else  
    {  
        do_that;  
    }
}

在我的节目中,<代码>>(FIRST)> 的成功率约为1 000。 C/C++中的任何备选案文 这样,我们就能够避免在内部检查每处情况,同时希望能看到在这种情况下取得更好的业绩。

Ok! Let me put in some more detail. i am writing a code for a signal acquisiton and tracking scheme where the state of my system will remain in TRACKING mode more often that ACQUISITION mode.

while(signal_present)  
{    
    if(ACQUISITION_SUCCEEDED)  
    {     
        do_tracking();  // this functions can change the state from TRACKING to ACQUISITION  
    }    
    else  
    {    
        do_acquisition();  // this function can change the state from ACQUISITION to TRACKING  
    }     
}    

因此,这里发生的情况是,该系统通常仍处于跟踪模式之中,但在跟踪失败时可以进入采购模式,但并非常见。 假设所输入的数据数量有限。 iii

问题回答

单一部门的业绩成本不会很大。 你们唯一能够做的事情是,除某些教学内容外,首先制定最有可能的法典。 Maybe。 这确实深入到微观发展。

There is no particularly good reason to try to optimize this. Almost all modern architectures incorporate branch predictors. These speculate that a branch (an if or else) will be taken essentially the way it has been in the past. In your case, the speculation will always succeed, eliminating all overhead. There are non-portable ways to hint that a condition is taken one way or another, but any branch predictor will work just as well.

您为改进教学-教学地点而可能要做的一件事是将do_<>>s/code>移出 lo(除非它是一种功能要求)。

海湾合作委员会有。 您可以用来向汇编者说明哪一个部门可能采取。 你们可以这样做:

if(__builtin_expect(FIRST, 1)) …

这是否有用? 我没有想法。 我从未使用过它,从未看到它使用过(除了据称位于欧伦特油轮之外)。 海湾合作委员会的文件实际上阻止了它使用貌相信息来实现更可靠的衡量标准。

关于最近的x86处理器系统,最后执行速度将几乎依靠源代码的执行。

http://igoro.com 看到加工商内部的优化程度。

如果与<代码>do_aquisition<>/code>的实施相比,这一试验确实需要大量时间,那么,如果有一个职能表的话,你可能会有动力:

typedef void (*trackfunc)(void);
trackfunc tracking_action[] = {do_acquisition, do_tracking};
while (signal_present)
{
   tracking_action[ACQUISITION_STATE]();
}

这些手工优化的影响在很大程度上取决于平台、汇编和优化环境。

您花费时间衡量和调整成果,并确实采用跟踪算法,最有可能取得更大的业绩。

如果你不知道什么时候“FIRST”是真实的,那么就没有了。

问题是,飞行情报和安全局是否耗时;或许可以评估飞行情报和安全局(或其中的一部分)之前的情况,并且只是测试毛坯。

页: 1

while( some_condition )
{
   do_that;
   if( FIRST )
   {
     do_this; // overwrite what you did earlier.
   }
}

根据你的新情况,我说以下几点:

while(some_condition)
{
  while(ACQUISITION_SUCCEEDED)
  {
    do_tracking();
  }
  if (some_condition)
    while(!ACQUISITION_SUCCEEDED)
    {
      do_acquisition();
    }
}

问题是<代码>。 必要信息(即如果<>条码_condition>,则一定程度上必须包含以下信息(即如果<>t_condition>,它将打破内部循环。 是不实的,因此有机会打破外 lo。

这是优化的一个典型。 如果你能够的话,你就应避免将条件限制在院内。 该法典:

while(...)
{
    if( a )
    {
       foo();
    }
    else
    {
       bar();
    }
}

改写成:

if( a )
{
    while(...)
    {
        foo();
    }
}
else
{
    while(...)
    {
        bar();
    }
}

并非总能做到,在你努力优化衡量之前和之后业绩时,你总是应该这样做。

不能比照你的榜样优化你的工作。

<代码>do_this和do_的电话/分机可取消您通过优化-then-else而获得的任何节余。

优化业绩的规则之一是减少分行。 多数加工商倾向于执行顺序法。 他们可以拿到顺序法的nk子,并把它 ha入ches。 中断这一抗辩,并可能导致完全重载教学海滩(失去宝贵的执行时间)。

在您进行微观创新之前,对您的设计进行审查,以确定您是否能够:

  1. Eliminate unnecessary branching.
  2. Split up code so it fits into the cache.
  3. Organize the data to reduce fetches from memory or hard drive.

I m sure that the above steps will gain you more performance than optimizing your posted loop.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签