指针解引用开销对分支/条件语句 [英] Pointer dereferencing overhead vs branching / conditional statements

查看:109
本文介绍了指针解引用开销对分支/条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在诸如游戏应用程序中发现的重循环中,可能有许多因素决定循环体的哪一部分被执行(例如,字符对象将根据其当前状态而不同地更新),因此

In heavy loops, such as ones found in game applications, there could be many factors that decide what part of the loop body is executed (for example, a character object will be updated differently depending on its current state) and so instead of doing:

void my_loop_function(int dt) {
  if (conditionX && conditionY) 
    doFoo();
  else
    doBar();

  ...
}

指向对应于字符的当前状态的特定逻辑函数的函数指针,如:

I am used to using a function pointer that points to a certain logic function corresponding to the character's current state, as in:

void (*updater)(int);

void something_happens() {
  updater = &doFoo;
}
void something_else_happens() {
  updater = &doBar;
}
void my_loop_function(int dt) {
  (*updater)(dt);

  ...
}

我不想做任何事情,我定义一个虚函数,并指向它,当我需要:

And in the case where I don't want to do anything, I define a dummy function and point to it when I need to:

void do_nothing(int dt) { }



现在我真正想知道的是:我不必要地痴迷?上面给出的例子当然很简单;有时我需要检查许多变量来确定需要执行的代码段,所以我想出使用这些状态函数指针确实会更加优化,对我来说,自然,但几个人我

Now what I'm really wondering is: am I obsessing about this needlessly? The example given above of course is simple; sometimes I need to check many variables to figure out which pieces of code I'll need to execute, and so I figured out using these "state" function pointers would indeed be more optimal, and to me, natural, but a few people I'm dealing with are heavily disagreeing.

所以,是使用一个(虚拟)函数指针的收益,而不是填充我的循环条件语句流动的逻辑?

So, is the gain from using a (virtual)function pointer worth it instead of filling my loops with conditional statements to flow the logic?

编辑:以澄清指针是如何设置的,它是通过基于每个对象的事件处理完成的。当一个事件发生时,例如,该字符具有附加的自定义逻辑,它会在该事件处理程序中设置updater指针,直到另一个事件发生,这将再次改变流程。

Edit: to clarify how the pointer is being set, it's done through event handling on a per-object basis. When an event occurs and, say, that character has custom logic attached to it, it sets the updater pointer in that event handler until another event occurs which will change the flow once again.

谢谢

推荐答案

函数指针方法让你使转换异步。而不是将 dt 传递到 updater ,也传递对象。现在updater本身可以负责状态转换。这将本地化状态转换逻辑而不是全局化它在一个大丑陋 if ... else if ... else if ... 函数。

The function pointer approach let's you make the transitions asynchronous. Rather than just passing dt to the updater, pass the object as well. Now the updater can itself be responsible for the state transitions. This localizes the state transition logic instead of globalizing it in one big ugly if ... else if ... else if ... function.

至于这个间接的代价,你在乎吗?你可能会关心如果你的更新程序是如此之小,以至于取消引用的成本加上一个函数调用压倒了执行更新程序代码的成本。如果更新器具有任何复杂性,那么这种复杂性将压倒这种增加的灵活性的成本。

As far as the cost of this indirection, do you care? You might care if your updaters are so extremely small that the cost of a dereference plus a function call overwhelms the cost of executing the updater code. If the updaters are of any complexity, that complexity is going to overwhelm the cost of this added flexibility.

这篇关于指针解引用开销对分支/条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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