虚拟功能与速度 [英] virtual functions vs speed

查看:54
本文介绍了虚拟功能与速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有以下权衡要做:


一个有2个虚函数的基类对你有帮助

问题我正在努力。尽管我的程序将使用的函数很多是虚拟的,因此会减慢

计算,至少这是我读过的内容

互联网上的几个地方。这是有道理的程序必须使用某种类型的查找

表的虚拟函数来激动地知道要调用的函数。


多少钱放慢速度,是否有解决方法?


我一直在考虑使用函数指针,但这看起来很讨厌

技巧,这只会产生很多开发问题稍后。


感谢您的回应


stijn

Hello,

I have the following trade off to make:

A base class with 2 virtual functions would be realy helpfull for the
problem I''m working on. Still though the functions that my program will use
a lot are the ones that are virtual and thus will slow down the
calculation, at least that is what what I have read on several places on the
internet. It makes sense the program has to work with some kind off lookup
table for the virtual functions to excatly know what function to call.

How much is the slow down and is there a workaround?

I have been thinking about using function pointers but that seems as nasty
trick which will only create a lot development problems later on.

thanks for the respons

stijn

推荐答案



" Stijn Oude Brunink" <所以********** @ chello.nl>在消息中写道

新闻:8M ******************* @ amsnews03.chello.com ...

"Stijn Oude Brunink" <so**********@chello.nl> wrote in message
news:8M*******************@amsnews03.chello.com...
你好,

我有以下权衡:

一个有2个虚函数的基类对我正在工作的
问题很有帮助上。尽管我的程序将使用的功能很多是虚拟的,因此会减慢
计算速度,至少这是我在几个地方读到的内容。
互联网。有意义的是程序必须使用某种类型的查找表来获取虚拟函数,以便知道要调用什么函数。

减速多少并且有解决方法吗?


相比什么减速?除非你有一个解决方法,否则没有什么可比的。


我想通常的解决方法是让基类有一个额外的字段

说明派生类是什么,然后让每个''虚拟''函数

测试该字段并直接调用派生类函数(使用强制转换)。

但这也很慢。所以你所能做的就是尝试两种技巧和时间

他们。任何差异都可能以毫秒为单位进行测量。


通常的建议是首先让你的代码以最直接的方式工作(你说的)虚拟函数将非常有用。)

然后,当你有一个工作程序时,看看它是否运行得足够快。

只有当你确定了它不是你应该看看加速它的方式。任何其他方式都是浪费精力并产生丑陋的代码,因为

很好。

我一直在考虑使用函数指针,但这似乎是令人讨厌的/>技巧只会在以后产生很多开发问题。
Hello,

I have the following trade off to make:

A base class with 2 virtual functions would be realy helpfull for the
problem I''m working on. Still though the functions that my program will
use
a lot are the ones that are virtual and thus will slow down the
calculation, at least that is what what I have read on several places on
the
internet. It makes sense the program has to work with some kind off lookup
table for the virtual functions to excatly know what function to call.

How much is the slow down and is there a workaround?
The slow down compared to what? Unless you have a workaround you have
nothing to compare with.

I guess the usual workaround is to have the base class have an extra field
that says what the derived class is and then have each ''virtual'' function
test that field and directly call the derived class function (using a cast).
But that is slow as well. So all you can do is try both techniques and time
them. Any difference is likely to be measured in fractions of milliseconds.

The usual advice is to first get your code working in the most
straightforward way (you said virtual functions would be ''really useful'').
Then, when you have a working program, see if it is running fast enough.
ONLY when you have determined that it is not should you look at ways of
speeding it up. Any other way is a waste of effort and produces ugly code as
well.

I have been thinking about using function pointers but that seems as
nasty
trick which will only create a lot development problems later on.




对,它更容易加快一个编写良好的程序,它是修复bug

编写错误的程序。


john



Right, its easier to speed up a well written program that it is to fix bugs
in a badly written program.

john


Stijn写道:
Stijn wrote:
我有以下权衡要做:

一个带有2个虚函数的基类对于我正在研究的问题非常有帮助。尽管我的程序
使用的功能很多都是虚拟的,因此会减慢
计算速度,至少这是我在
上的几个地方读到的内容互联网。有意义的是程序必须使用某种类型的查找表来获取虚拟函数,以便知道要调用什么函数。

减速多少并且有解决方法吗?
I have the following trade off to make:

A base class with 2 virtual functions would be realy helpfull for the
problem I''m working on. Still though the functions that my program will use a lot are the ones that are virtual and thus will slow down the
calculation, at least that is what what I have read on several places on the internet. It makes sense the program has to work with some kind off lookup
table for the virtual functions to excatly know what function to call.

How much is the slow down and is there a workaround?




我建议在C ++对象模型中作者:Stanley Lippman - 它

正好解决了这类问题。


功能有什么作用?


David Fisher

澳大利亚悉尼



I would recommend "Inside the C++ Object Model" by Stanley Lippman - it
addresses exactly this sort of question.

What do the functions do ?

David Fisher
Sydney, Australia


>具有2个虚函数的基类对我正在研究的
> A base class with 2 virtual functions would be realy helpfull for the
问题非常有帮助。尽管我的程序
使用的功能很多都是虚拟的,因此会减慢
计算速度,至少这是我在
上的几个地方读到的内容互联网。有意义的是程序必须使用某种类型的查找表来获取虚拟函数,以便知道要调用什么函数。

减速多少并且有解决方法吗?
problem I''m working on. Still though the functions that my program will use a lot are the ones that are virtual and thus will slow down the
calculation, at least that is what what I have read on several places on the internet. It makes sense the program has to work with some kind off lookup
table for the virtual functions to excatly know what function to call.

How much is the slow down and is there a workaround?




对于每次调用,编译器需要加载2个值:首先是

虚函数表的地址,然后是地址函数本身。

但是如果你真的经常调用这些函数,那么处理器缓存中的这些值将是

,并且延迟将非常小。它可能会比一个变量的值分配给另一个变量小。

所以要么使用虚函数要么尝试完全避免函数调用(make

他们内联)。


Niels Dybdahl



For each call the compiler needs to load 2 values: first the address of the
table of virtual functions and then the address of the function itself.
However if you really call these functions a lot, then these values will be
in the processors cache and the delay will be very small. It will probably
be less than assignment of the value of one variable to another.
So either use virtual functions or try avoid function calls at all (make
them inline).

Niels Dybdahl


这篇关于虚拟功能与速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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