473,714 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct faster than class?

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?

Dec 23 '05 #1
17 3920
<wa********@yah oo.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?


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.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 23 '05 #2
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.

Dec 23 '05 #3
<wa********@yah oo.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.


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.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 23 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
<wa********@yah oo.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.


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*.


Sorry, I should have included a link to Ian Griffith's excellent
mailing list post (now on his blog):

http://www.interact-sw.co.uk/iangblo...1/09/profiling

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 23 '05 #5
Ya know, I'd rather you not answer the question than tell me something
completely other than what I was asking about.

1st off a struct is not in the least complex.
2nd I asked specifically about speed. Not about design. It's a speed
question, I don't care what you think is a good use of design time.

It was a specific speed question, between a class object and a struct,
nothing else.
I don't get why people want to try and tell other people that there
questions aren't the right ones.

Dec 23 '05 #6
<wa********@yah oo.com> wrote:
Ya know, I'd rather you not answer the question than tell me something
completely other than what I was asking about.
Sorry, I'll give you a full refund.
1st off a struct is not in the least complex.
That statement may well come back to bite you, especially if you make
your structs mutable. How well do you know the rules about when a
struct's variable is definitely initialised? How about what the effects
of unboxing are? Why is:

object o = GetSomethingFro mSomewhere();
((SomeClass)o). Value = 5;

valid but

object o = GetSomethingFro mSomewhere();
((SomeStruct)o) .Value = 5;

isn't?
2nd I asked specifically about speed. Not about design. It's a speed
question, I don't care what you think is a good use of design time.
That's an unfortunate attitude, but never mind.
It was a specific speed question, between a class object and a struct,
nothing else.
I don't get why people want to try and tell other people that there
questions aren't the right ones.


Because it's proven to be useful so many times in the past.

For instance, someone asked a while ago what the equivalent of some
Delphi code was in C#. What they really *wanted* was some C# code which
accomplished the same result - and the best solution to that was
actually to use regular expressions, rather than to transcribe the
Delphi into C#. Do you really think the best answer would have been to
give the transcription?

Why would you *not* want to know about other things which should affect
your decision, beyond the thing you happen to have thought of to start
with? Do you trust yourself to think of everything and have more
experience of the issues than everyone on the newsgroup? I know I don't
when I ask a question.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 23 '05 #7
Jon,
Based solely on your postings to this and other newsgroups I
think it would be a terrible understatement to say you add a lot to the
development community but I'm not sure if I'm more impressed with your
extensive contribution or your ability to handle posters like this in a
calm and professional manner. I can only guess that you've worked with
(and cleaned up after) this sort of tunnel-visioned/short-sighted
developer before and take comfort in the fact that as long as his kind
exist, there will be a great demand for knowledgeable and skilled
engineers.
Just wanted you to know that your work here is greatly
appreciated by most of us; please keep it up.

Happy Holidays,
Will

Dec 24 '05 #8
On Fri, 23 Dec 2005 15:03:13 -0800, wackyphill wrote:
Ya know, I'd rather you not answer the question than tell me something
completely other than what I was asking about.

Ya know, he *did* address your question. You're just ignorant to
understand that he did.
1st off a struct is not in the least complex. 2nd I asked specifically
about speed. Not about design. It's a speed question, I don't care what
you think is a good use of design time.

It was a specific speed question, between a class object and a struct,
nothing else.
I don't get why people want to try and tell other people that there
questions aren't the right ones.


Can you post your full name and address so I can ensure that I never
accidentally employ you?

Dec 24 '05 #9
>Why would you *not* want to know about other things which should affect
your decision, beyond the thing you happen to have thought of to start
with? Do you trust yourself to think of everything and have more
experience of the issues than everyone on the newsgroup? I know I don't
when I ask a question.
Because it was not a design decision, simply a curiosity of how the
run-time handles things.
For example, would a struct be more efficient if it was < 16 bytes and
an object be more efficient if > 16 bytes.
I'm curious about such things, despite their admitted irrelevance in
99% of most modern scenarios if all you're concerned w/ is getting
things to work.From a knowing how things actually work perspective (if that interests

you as it does me) it is a valid question as it was worded IMO.

Dec 24 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
1929
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these classes/structs (in most cases it will be less then 5 however) Since there's no real business logic my...
5
7186
by: Chua Wen Ching | last post by:
Hi there, I am very curious on this code: // declared as structure public struct Overlapped { public IntPtr intrnal; public IntPtr internalHigh;
2
1165
by: Pohihihi | last post by:
After reading the thread "Struct faster than class?" I thought I should surely understand if I am doing anything wrong in my work. First, Jon you are very professional. Excellent. I will wish you as my mentor. Anyways, getting to my question -- I am in need of enums a lot in my project. More for meta data reasons. e.g. Toggle -on/off, Close/Open etc. Most are int type values but this one meta data is full of Guid. Now that enum...
16
16411
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
46
2501
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
7
1503
by: Mantorok | last post by:
I've always followed a particular standard when considering structs, I mainly use a struct when a class isn't necessary (ie no requirement for multiple references). What is the recommendation when considering structs? What matters and does it "really" make a big difference? Thanks Kev
37
4018
by: JohnGoogle | last post by:
Hi, Newbie question... After a recent article in VSJ I had a go at implementing a Fraction class to aid my understanding of operator overloading. After a previous message someone suggested that I implement it as a struct rather than a class which I did and all worked OK. The simplest declaration for the struct is:
12
1553
by: Author | last post by:
I know the basic differences between a struct and a class, but apparently not good enough to know why some types/concepts are implemented as struct whereas most types are implemented as class. For example, why is DateTime implemented as a struct? Why is BitArray implemented as a class, but BitVector32 a struct? In general, in OO design, how do we determine if we should use struct or class?
20
3415
by: Plissken.s | last post by:
Is there an efficient way to create a struct of flag in C++? I need to create a struct of boolean flag, like this: struct testStruct { bool flag1; bool flag2; bool flag3; bool flag4; bool flag5; bool flag6;
0
8817
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9189
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9086
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3170
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.