digitalmars.D - array advantage?
- Lucas Goss (16/16) Jan 06 2006 Is there any advantage or benefit to writing a class as:
- Fredrik Olsson (15/35) Jan 07 2006 Now lets see.
- Lars Ivar Igesund (9/47) Jan 07 2006 And even then you use arrays, just having
- pragma (3/56) Jan 07 2006 - EricAnderton at yahoo
- Tom S (26/33) Jan 07 2006 In that case we can have the best of both worlds:
- Hasan Aljudy (4/44) Jan 07 2006 I think he's referring to using arrays to group non-related things,
-
Fredrik Olsson
(18/30)
Jan 07 2006
- Lucas Goss (17/40) Jan 07 2006 Well I was sort of looking for a general reason since I have other
- BCS (5/8) Jan 07 2006 If you are looking to do 3D math I have a fairly compete library.
- Lucas Goss (6/13) Jan 13 2006 Thanks! (sorry I was a little slow to respond).
- BCS (6/23) Jan 13 2006 Making it into a template ought not to be to bad as the actual type is d...
- Lucas Goss (11/18) Jan 14 2006 Yeah, that's how I'm doing it. I have:
- BCS (4/13) Jan 14 2006 I was sticking to just the geomectric stuff like transformations (transl...
- Lucas Goss (2/8) Jan 14 2006 Ah, I'm modeling mine off of David Eberly's books. For the inverse I
- BCS (5/7) Jan 16 2006 I I'm using row ops (not sure what the method is called)
- Tom S (10/17) Jan 17 2006 I believe it's Gaussian elimination :)
- BCS (5/22) Jan 17 2006 It's up at http://www.cs.uidaho.edu/~temp0092/point_lib.d
- Lucas Goss (7/21) Jan 30 2006 I'm curious, what do you have in:
- Tom S (11/19) Jan 30 2006 Here's the rest of my engine:
- BCS (5/5) Jan 30 2006 I just put an update of my 3d vector library up. Bug fixes better docs e...
- Lucas Goss (2/8) Jan 31 2006 Thanks!
- nick (16/36) Jan 29 2006 Referring to array elements requires a few extra operations. For
- Oskar Linde (10/15) Jan 29 2006 To get the best of both worlds, the following should work:
Is there any advantage or benefit to writing a class as: class SomeClass { float number[3]; //... } Instead of: class SomeClass { float number1; float number2; float number3; //... } I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?
Jan 06 2006
Lucas Goss skrev:Is there any advantage or benefit to writing a class as: class SomeClass { float number[3]; //... } Instead of: class SomeClass { float number1; float number2; float number3; //... } I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?Now lets see. 1. The array version is shorter to write (Imagine 1000 numbers). 2. An array logically groups related values. 3. You can do for and foreach loops over the array. 4. No speed loss with arrays. 5. Should the need arise the static array is quite easy to make dynamic. Actually, I have a harder time seeing why I would want it the second way? Well except cases when the naming and grouping is set in stone and becomes more logical, like: class MyPoint { float x, y, z; // ... } // Fredrik Olsson
Jan 07 2006
Fredrik Olsson wrote:Lucas Goss skrev:And even then you use arrays, just having class MyPoint { float _point[3]; float x() { return _point[0]; } void x(float newx) { _point[0] = newx; } etc; } Lars Ivar IgesundIs there any advantage or benefit to writing a class as: class SomeClass { float number[3]; //... } Instead of: class SomeClass { float number1; float number2; float number3; //... } I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?Now lets see. 1. The array version is shorter to write (Imagine 1000 numbers). 2. An array logically groups related values. 3. You can do for and foreach loops over the array. 4. No speed loss with arrays. 5. Should the need arise the static array is quite easy to make dynamic. Actually, I have a harder time seeing why I would want it the second way? Well except cases when the naming and grouping is set in stone and becomes more logical, like: class MyPoint { float x, y, z; // ... } // Fredrik Olsson
Jan 07 2006
In article <dpo5ud$3m3$1 digitaldaemon.com>, Lars Ivar Igesund says...Fredrik Olsson wrote:Or just exploit D's "array properties feature" and remove the class completely:Lucas Goss skrev:And even then you use arrays, just having class MyPoint { float _point[3]; float x() { return _point[0]; } void x(float newx) { _point[0] = newx; } etc; } Lars Ivar IgesundIs there any advantage or benefit to writing a class as: class SomeClass { float number[3]; //... } Instead of: class SomeClass { float number1; float number2; float number3; //... } I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?Now lets see. 1. The array version is shorter to write (Imagine 1000 numbers). 2. An array logically groups related values. 3. You can do for and foreach loops over the array. 4. No speed loss with arrays. 5. Should the need arise the static array is quite easy to make dynamic. Actually, I have a harder time seeing why I would want it the second way? Well except cases when the naming and grouping is set in stone and becomes more logical, like: class MyPoint { float x, y, z; // ... } // Fredrik Olssonalias float[3] MyPoint; float x(MyPoint _point) { return _point[0]; } float x(MyPoint _point,float newx) { _point[0] = newx; return newx; } /* ... */ MyPoint p; p.x = 5.5;- EricAnderton at yahoo
Jan 07 2006
Fredrik Olsson wrote:Actually, I have a harder time seeing why I would want it the second way? Well except cases when the naming and grouping is set in stone and becomes more logical, like: class MyPoint { float x, y, z; // ... }In that case we can have the best of both worlds: struct vec3 { union { struct { float x, y, z; } struct { float r, g, b; } float[3] cell; } // ... } Sometimes it's useful to perform operations on just one component of R^3. If the 'cell' index can be calculated earlier and reused, code duplication can be avoided -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y ------END GEEK CODE BLOCK------ Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jan 07 2006
Fredrik Olsson wrote:Lucas Goss skrev:I think he's referring to using arrays to group non-related things, because, come to think of it, if the things were logically related, you would use a dynamic array.Is there any advantage or benefit to writing a class as: class SomeClass { float number[3]; //... } Instead of: class SomeClass { float number1; float number2; float number3; //... } I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?Now lets see. 1. The array version is shorter to write (Imagine 1000 numbers). 2. An array logically groups related values. 3. You can do for and foreach loops over the array. 4. No speed loss with arrays. 5. Should the need arise the static array is quite easy to make dynamic. Actually, I have a harder time seeing why I would want it the second way? Well except cases when the naming and grouping is set in stone and becomes more logical, like: class MyPoint { float x, y, z; // ... } // Fredrik Olsson
Jan 07 2006
Hasan Aljudy skrev:Fredrik Olsson wrote:<snip>Lucas Goss skrev:Is there any advantage or benefit to writing a class as:<snip>I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?I think he's referring to using arrays to group non-related things, because, come to think of it, if the things were logically related, you would use a dynamic array.For grouping things not logically related I see no reason at all to use arrays, a compiler not s good at optimization might even do worse with an array. But more importantly, it is against KISS, keep it simple stupid. An array is an ordered list of a single type of data, using it for anything else is inviting trouble if you ask me. If you put temperature and stock changes in the same array, you will have to rely on external mechanism, or even worse; coding practices, for knowing what is what. Sure you can say element 0 IS the temp, and element 1 IS the stocks. A good recipe for writing solid and maintainable code is to make it hard to write the code wrong. "Hmm... was the temperature with offset 0 or 1?", is a probably question to rise, and a good one. "Hmm... was the temperature in temp or stockRate?" Os not quite as likely, and the person asking it should be fired on the spot. Now if I was only better at coding as I preach :). // Fredrik Olsson
Jan 07 2006
Hasan Aljudy wrote:Fredrik Olsson wrote:Well I was sort of looking for a general reason since I have other classes that are structured differently, but I'm actually working on a vector class at the moment. So yeah I could do an array like:Actually, I have a harder time seeing why I would want it the second way? Well except cases when the naming and grouping is set in stone and becomes more logical, like: class MyPoint { float x, y, z; // ... } // Fredrik OlssonI think he's referring to using arrays to group non-related things, because, come to think of it, if the things were logically related, you would use a dynamic array.Lars Ivar Igesund wrote: class MyPoint { float _point[3]; float x() { return _point[0]; } void x(float newx) { _point[0] = newx; } etc; }Of course keeping the "_point" private, and just allowing the x, y, and z properties. But why do that if the properties do nothing but set the value, they might as well be public: class MyPoint { public: float x, y, z; ... } Unless there is some benefit to the array? Especially if it's speed. I know in C++ classes (and C structs) there is no guarantee to how the members are aligned. In D? Now there may be other benefits to using the array that might come in handy that I don't see till later... I was just trying to plan ahead :)
Jan 07 2006
Lucas Goss wrote:Well I was sort of looking for a general reason since I have other classes that are structured differently, but I'm actually working on a vector class at the moment. So yeah I could do an array like:If you are looking to do 3D math I have a fairly compete library. http://www.cs.uidaho.edu/~temp0092/point_math.d This is an older version (the newer one is on a differnt system), I will post the newer one in a few hours.
Jan 07 2006
BCS wrote:If you are looking to do 3D math I have a fairly compete library. http://www.cs.uidaho.edu/~temp0092/point_math.d This is an older version (the newer one is on a differnt system), I will post the newer one in a few hours.Thanks! (sorry I was a little slow to respond). My vector class actually looked pretty similar (which means I must be doing something right :) ), though I'm using templates so that you can use float, double, or real. I'd post mine but I no longer have a web site (I'll have to remedy that soon).
Jan 13 2006
Lucas Goss wrote:BCS wrote:Making it into a template ought not to be to bad as the actual type is done using a alias. (if you don't see that in the version you have, I put up a more recent version a while back). I have some more code that can go along with it (matrix transformations etc.), If i can find it I'll put it up over the weekend.If you are looking to do 3D math I have a fairly compete library. http://www.cs.uidaho.edu/~temp0092/point_math.d This is an older version (the newer one is on a differnt system), I will post the newer one in a few hours.Thanks! (sorry I was a little slow to respond). My vector class actually looked pretty similar (which means I must be doing something right :) ), though I'm using templates so that you can use float, double, or real. I'd post mine but I no longer have a web site (I'll have to remedy that soon).
Jan 13 2006
BCS wrote:Making it into a template ought not to be to bad as the actual type is done using a alias. (if you don't see that in the version you have, I put up a more recent version a while back).Yeah, that's how I'm doing it. I have: alias TVector3!(float) Vector3f; alias TVector3!(double) Vector3d; alias TVector3!(real) Vector3;I have some more code that can go along with it (matrix transformations etc.), If i can find it I'll put it up over the weekend.I'd like to see it if you can find it. I'm doing my matrix stuff now and I have part of it done, but I'm not positive about what all methods to include (I have all the operators done). Currently I have: adjoint, determinant, (get/set)Row, (get/set)Column, inverse, quadraticForm, transpose, and orthonormalize. Right now I'm getting ready to add eigenDecomposition and after that some euler conversions.
Jan 14 2006
In article <dqb9jm$2t6$1 digitaldaemon.com>, Lucas Goss says...BCS wrote:I was sticking to just the geomectric stuff like transformations (translate , rotate, skew), inverse and combining. The sticky one has been inverse (To many special cases)I have some more code that can go along with it (matrix transformations etc.), If i can find it I'll put it up over the weekend.I'd like to see it if you can find it. I'm doing my matrix stuff now and I have part of it done, but I'm not positive about what all methods to include (I have all the operators done). Currently I have: adjoint, determinant, (get/set)Row, (get/set)Column, inverse, quadraticForm, transpose, and orthonormalize. Right now I'm getting ready to add eigenDecomposition and after that some euler conversions.
Jan 14 2006
In article <dqb9jm$2t6$1 digitaldaemon.com>, Lucas Goss says... I was sticking to just the geomectric stuff like transformations (translate , rotate, skew), inverse and combining. The sticky one has been inverse (To many special cases)Ah, I'm modeling mine off of David Eberly's books. For the inverse I just use cofactors (courtesy of David Eberly :) ).
Jan 14 2006
In article <dqcc8u$vqr$1 digitaldaemon.com>, Lucas Goss says...Ah, I'm modeling mine off of David Eberly's books. For the inverse I just use cofactors (courtesy of David Eberly :) ).I I'm using row ops (not sure what the method is called) I've got most of it working and tested, I'll get it posted sometime tomorrow. BTW, is the point3d struct useful enough to merit inclusion in phobos and/or aries?
Jan 16 2006
BCS wrote:In article <dqcc8u$vqr$1 digitaldaemon.com>, Lucas Goss says...I believe it's Gaussian elimination :) BTW, I've also got a 3d math module: http://158.75.59.9/~h3/tmp/maths.d -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y ------END GEEK CODE BLOCK------ Tomasz Stachowiak /+ a.k.a. h3r3tic +/Ah, I'm modeling mine off of David Eberly's books. For the inverse I just use cofactors (courtesy of David Eberly :) ).I I'm using row ops (not sure what the method is called)
Jan 17 2006
Tom S wrote:BCS wrote:It's up at http://www.cs.uidaho.edu/~temp0092/point_lib.d Consider it bata (a.k.a. Probably has a few bugs). The unittest doesn't cover all cases and omits a few functions all together. Also I have a few function I might add laterIn article <dqcc8u$vqr$1 digitaldaemon.com>, Lucas Goss says...I believe it's Gaussian elimination :) BTW, I've also got a 3d math module: http://158.75.59.9/~h3/tmp/maths.dAh, I'm modeling mine off of David Eberly's books. For the inverse I just use cofactors (courtesy of David Eberly :) ).I I'm using row ops (not sure what the method is called)
Jan 17 2006
Tom S wrote:BCS wrote:I'm curious, what do you have in: import core.common; import core.serializer; I see this is probably pulled from core.serializer: mixin Serializer.RawStruct; Would you be open to showing those as well?In article <dqcc8u$vqr$1 digitaldaemon.com>, Lucas Goss says...I believe it's Gaussian elimination :) BTW, I've also got a 3d math module: http://158.75.59.9/~h3/tmp/maths.dAh, I'm modeling mine off of David Eberly's books. For the inverse I just use cofactors (courtesy of David Eberly :) ).I I'm using row ops (not sure what the method is called)
Jan 30 2006
Lucas Goss wrote:I'm curious, what do you have in: import core.common; import core.serializer; I see this is probably pulled from core.serializer: mixin Serializer.RawStruct; Would you be open to showing those as well?Here's the rest of my engine: http://158.75.59.9/~h3/code/heresy/backup/src-2006-01-28_0.rar It doesn't currently compile as I'm in the middle of a refactoring process. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y ------END GEEK CODE BLOCK------ Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jan 30 2006
I just put an update of my 3d vector library up. Bug fixes better docs etc. http://www.cs.uidaho.edu/~temp0092/point_lib.d http://www.cs.uidaho.edu/~temp0092/point_lib.html http://www.cs.uidaho.edu/~temp0092/point_math.d http://www.cs.uidaho.edu/~temp0092/point_math.html
Jan 30 2006
BCS wrote:I just put an update of my 3d vector library up. Bug fixes better docs etc. http://www.cs.uidaho.edu/~temp0092/point_lib.d http://www.cs.uidaho.edu/~temp0092/point_lib.html http://www.cs.uidaho.edu/~temp0092/point_math.d http://www.cs.uidaho.edu/~temp0092/point_math.htmlThanks!
Jan 31 2006
Lucas Goss wrote:Is there any advantage or benefit to writing a class as: class SomeClass { float number[3]; //... } Instead of: class SomeClass { float number1; float number2; float number3; //... } I see a lot of C++ classes that are like the first. Could it be memory alignment reasons? Or maybe for speed?Referring to array elements requires a few extra operations. For starters, you have to LOAD the base_address. Then, depending on your architecture you may have to do some math (like a multiply) to figure out the offset address and finally perform another LOAD to get the actual element into memory. This is definitely worth it if you are going to be processing a large number of elements because the overhead associated with an array is ameliorated. (only only load the base address once and most machines tend to have address generation units). On the other hand, if you are going to be performing several extra operations just to increment a couple integers then the performance may suffer. With that said, I don't know if anybody did a benchmark of a vec3 implemented with an array vs. a vec3 implemented with 3 variables. Also, unless this class is on the absolute critical path, the performance gain will be less than insignificant.
Jan 29 2006
nick wrote:With that said, I don't know if anybody did a benchmark of a vec3 implemented with an array vs. a vec3 implemented with 3 variables. Also, unless this class is on the absolute critical path, the performance gain will be less than insignificant.To get the best of both worlds, the following should work: struct vec3 { union { float number[3]; struct { float x,y,z; } } }
Jan 29 2006