Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

3D Points (structs vs classes)

2 views
Skip to first unread message

cody

unread,
Feb 14, 2004, 7:27:19 AM2/14/04
to
When I have a 3-Dimensional Point is it better to use a class or a struct? I
heard that if the size of the struct is greater than 16 bytes but in my
feelings using a class for 3 double values (24 bytes) is overhead. In the
app I've planned i would have thousands of 3DPoints, isn't it better to use
structs in such a 3D-engine?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


Bob Powell [MVP]

unread,
Feb 14, 2004, 7:42:54 AM2/14/04
to
I'd go for structs because they will reside mostly on the stack rather than
on the heap and subject to GC (unless you box them).

Why don't you use Direct3D though? Is it because writing a 3D engine is fun?

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"cody" <dont.spam.m...@gmx.de> wrote in message
news:%23QmiGZv...@TK2MSFTNGP12.phx.gbl...

cody

unread,
Feb 14, 2004, 8:07:51 AM2/14/04
to
> I'd go for structs because they will reside mostly on the stack rather
than
> on the heap and subject to GC (unless you box them).

So I will use structs. If I use only arrays and not lists I won't need
boxing (at least I hope so)..

> Why don't you use Direct3D though? Is it because writing a 3D engine is
fun?

Fun, learning, what you want. Additionally is no official DirectX-Wrapper
for .NET yet.

Frank Hileman

unread,
Feb 14, 2004, 11:12:39 AM2/14/04
to
Hello cody,

We used structs instead of classes for a 3x2 float matrix. We found the
struct much faster, especially since they have operator overloading and
produce a lot of temp objects in complex expressions. They are a natural fit
for matrices, vectors, and points.

In fact I think you could go even higher in size and see a speed improvement
for that type of abstraction.

Regards,
Frank Hileman
Prodige Software Corporation

check out VG.net: www.vgdotnet.com
Animated vector graphics system
Integrated VS.net graphics editor


cody

unread,
Feb 14, 2004, 11:44:11 AM2/14/04
to
> We used structs instead of classes for a 3x2 float matrix. We found the
> struct much faster, especially since they have operator overloading and
> produce a lot of temp objects in complex expressions. They are a natural
fit
> for matrices, vectors, and points.
>
> In fact I think you could go even higher in size and see a speed
improvement
> for that type of abstraction.

Thank you!

Ken Tucker [MVP]

unread,
Feb 14, 2004, 12:03:16 PM2/14/04
to
Hi,

There is a managed interface for directx 9.
http://msdn.microsoft.com/library/default.asp?url=/nhp/default.asp?contentid=28000410

Ken
----------------


"cody" <dont.spam.m...@gmx.de> wrote in message

news:c0l7ln$1841l6$1...@ID-176797.news.uni-berlin.de...

cody

unread,
Feb 14, 2004, 6:02:30 PM2/14/04
to


Thank you! I didn't know I was already out!

cody

unread,
Feb 14, 2004, 6:02:54 PM2/14/04
to


Thank you! I didn't know that it was already out!

cody

unread,
Feb 14, 2004, 6:03:02 PM2/14/04
to

Thank you! I didn't know that it was already out!

--

Michael Giagnocavo [MVP]

unread,
Feb 16, 2004, 10:34:17 AM2/16/04
to

> So I will use structs. If I use only arrays and not lists I won't need
> boxing (at least I hope so)..

Right. Also, if you find yourself in need of a strongly typed array list,
you can always write your own implementation.

-mike
MVP

Robert Jacobson

unread,
Feb 20, 2004, 3:55:43 PM2/20/04
to
Also, you could use CodeSmith to autogenerate a strongly-typed resizable
list.

http://www.ericjsmith.net/codesmith/screenshots.aspx

"Michael Giagnocavo [MVP]" <mggU...@Atrevido.net> wrote in message
news:uFhCJMK9...@tk2msftngp13.phx.gbl...

Eric Gunnerson [MS]

unread,
Feb 26, 2004, 7:59:06 PM2/26/04
to
To echo what Frank said, the 16 byte part is a guideline.

The tradeoff is between the time taken in your type be a reference type (ie
allocation and GC overhead and overall memory pressure) vs the time it takes
to copy the struct as parameters and return types, and any boxing overhead.

The tradeoff is going to be different depending on how you use your type, so
it's something you need to measure. My guess is that a 3D point will be
faster as a struct, especially if you have a ton of them.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Frank Hileman" <fran...@no.spamming.prodigesoftware.com> wrote in message
news:%23UkmeVx...@TK2MSFTNGP11.phx.gbl...

codymanix

unread,
Feb 27, 2004, 10:08:26 AM2/27/04
to
> To echo what Frank said, the 16 byte part is a guideline.
>
> The tradeoff is between the time taken in your type be a reference type
(ie
> allocation and GC overhead and overall memory pressure) vs the time it
takes
> to copy the struct as parameters and return types, and any boxing
overhead.
>
> The tradeoff is going to be different depending on how you use your type,
so
> it's something you need to measure. My guess is that a 3D point will be
> faster as a struct, especially if you have a ton of them.


Yes, I thought about it: The thing that slows struct down is parameter
passing, but when you use them in an array you never need to pass the
structs itself so a struct is much better for points than a class.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]


Fuzzy

unread,
Mar 1, 2004, 2:57:14 PM3/1/04
to
"Eric Gunnerson [MS]" <eri...@online.microsoft.com> wrote in message news:<u1dQnzM$DHA....@TK2MSFTNGP12.phx.gbl>...

> To echo what Frank said, the 16 byte part is a guideline.
>
> The tradeoff is between the time taken in your type be a reference type (ie
> allocation and GC overhead and overall memory pressure) vs the time it takes
> to copy the struct as parameters and return types, and any boxing overhead.
>

So what's your opinion of passing larger structs around as ref
parameters and eliminating the copy?

cody

unread,
Mar 1, 2004, 3:06:34 PM3/1/04
to
"Fuzzy" <trno...@earthlink.net> schrieb im Newsbeitrag
news:9527b23b.04030...@posting.google.com...

using ref eliminates the copy but through the extra indirection access to
the struct can get slower, I think.
but for larger structs (>16 bytes or more) it may save time, but this highly
depends on the specific application.
the most important thing when dealing with structs is that you must be
always awre of boxing, because boxing can be the biggest performance hit.

Michael Giagnocavo [MVP]

unread,
Mar 1, 2004, 7:49:39 PM3/1/04
to
On any performance issue, measure, measure, measure! :). If you're not
sure, there's a good chance your wrong. Compiler optimizations can end up
doing all sorts of stuff, and what might seem faster could be slower.

-mike
MVP

"Fuzzy" <trno...@earthlink.net> wrote in message
news:9527b23b.04030...@posting.google.com...

Fuzzy

unread,
Mar 2, 2004, 10:37:30 AM3/2/04
to
> > So what's your opinion of passing larger structs around as ref
> > parameters and eliminating the copy?
>
> using ref eliminates the copy but through the extra indirection access to
> the struct can get slower, I think.
> but for larger structs (>16 bytes or more) it may save time, but this highly
> depends on the specific application.
> the most important thing when dealing with structs is that you must be
> always awre of boxing, because boxing can be the biggest performance hit.

I my case, the data I'm considering using a struct instead of a class
will probably be somewhere between 100 and 300 bytes. There's no real
"behavior" of the data, just definitional properties. All I need is
read access to everything and occasional write access to some of the
fields (for current state). There won't be a lot of these around at
any one time, so I'm not concerned about exhausting the stack. An
analogy is a ball in a pinball game - one of these objects comes in,
affects the state of the overall system, then leaves. Then comes
another one.

0 new messages