结构比同类更快? [英] Struct faster than class?

查看:99
本文介绍了结构比同类更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个基本上创建一个对象/结构的函数,那么
只保存一个函数中的数据并希望从

返回对象/结构的函数是一个类对象或结构更快?


我会想到类对象,因为它可以作为

对象引用传回而不是复制整个结构,但我已经阅读了

让我质疑结构实​​际上是否更快的东西。


真相是什么?

If you have a function that basically creates an object/struct that
holds data only in a function and wish to return the object/struct from
the function which is faster a class object or struct?

I would have thought class object since it can be passed back as an
object reference instead of copying an entire struct, but I''ve read
things that make me question if structs are in fact faster.

What''s the truth?

推荐答案

< wa ******** @ yahoo.com>写道:
<wa********@yahoo.com> wrote:
如果你有一个基本上创建一个对象/结构的函数,它只在一个函数中保存数据并希望从
返回对象/结构的函数是更快的类对象或结构?

我会想到类对象,因为它可以作为
对象引用传回而不是复制整个结构,但我已经读过了事情让我质疑结构实​​际上是否更快。

真相是什么?
If you have a function that basically creates an object/struct that
holds data only in a function and wish to return the object/struct from
the function which is faster a class object or struct?

I would have thought class object since it can be passed back as an
object reference instead of copying an entire struct, but I''ve read
things that make me question if structs are in fact faster.

What''s the truth?




回报会更快上课。但是,你需要在堆上创建

的实例,这比在堆栈上创建结构要贵得多。




但是,您应该真的问是否需要值语义或

引用语义。 默认是指默认。答案应该是创建一个类。

创建一个结构很少是一个好主意。有各种各样的

令人惊讶(如果你不小心)其他一些行为 -

特别是对于可变结构。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件



The return would be faster with a class. However, you''d need to create
the instance on the heap to start with, which would be more expensive
than creating the struct on the stack.

However, you should really ask whether you want value semantics or
reference semantics. The "default" answer should be to create a class.
It''s very rarely a good idea to create a struct. There are all kinds of
surprising (if you''re not careful) bits of behaviour otherwise -
particularly for mutable structs.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


那么,对于一个电子商务网站,每个产品可能是一个结构或

类,如果不是,那么不想产生开销似乎是合理的。
需要


Well, for an e-commerce site where each product could be a struct or
class it seems reasonable to not want to incur overhead if not
necessary.


< wa ******** @ yahoo.com>写道:
<wa********@yahoo.com> wrote:
嗯,对于一个电子商务网站,每个产品可能是一个结构或类似的,如果不是必要的话,不想产生开销似乎是合理的。
Well, for an e-commerce site where each product could be a struct or
class it seems reasonable to not want to incur overhead if not
necessary.




直到你确实遇到性能问题。微优化

如果你愿意的话会杀死可读性和优雅的设计。制定出最简单的方法来做到这一点,注意*宏* - 优化(在
交易条款,网络服务chattiness等),然后看到

是否表现得足够好。如果它没有,找到瓶颈

并优化* *。


-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget 博客: http://www.msmvps.com /jon.skeet

如果回复小组,请不要给我发邮件



Not until you''ve actually got a performance problem. Micro-optimisation
kills readability and elegant design if you let it. Work out the
simplest way to do it, paying attention to *macro*-optimisation (in
terms of transactions, web service chattiness etc), and then see
whether that performs well enough. If it doesn''t, find the bottleneck
and optimise *that*.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于结构比同类更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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