digitalmars.D - array advantage?
- Lucas Goss <lgoss007 gmail.com> Jan 06 2006
- Fredrik Olsson <peylow gmail.com> Jan 07 2006
- Lars Ivar Igesund <larsivar igesund.net> Jan 07 2006
- pragma <pragma_member pathlink.com> Jan 07 2006
- Tom S <h3r3tic remove.mat.uni.torun.pl> Jan 07 2006
- Hasan Aljudy <hasan.aljudy gmail.com> Jan 07 2006
- Fredrik Olsson <peylow gmail.com> Jan 07 2006
- Lucas Goss <lgoss007 gmail.com> Jan 07 2006
- BCS <BCS_member pathlink.com> Jan 07 2006
- Lucas Goss <lgoss007 gmail.com> Jan 13 2006
- BCS <BCS_member pathlink.com> Jan 13 2006
- Lucas Goss <lgoss007 gmail.com> Jan 14 2006
- BCS <BCS_member pathlink.com> Jan 14 2006
- Lucas Goss <lgoss007 gmail.com> Jan 14 2006
- BCS <BCS_member pathlink.com> Jan 16 2006
- Tom S <h3r3tic remove.mat.uni.torun.pl> Jan 17 2006
- BCS <BCS_member pathlink.com> Jan 17 2006
- Lucas Goss <lgoss007 gmail.com> Jan 30 2006
- Tom S <h3r3tic remove.mat.uni.torun.pl> Jan 30 2006
- BCS <BCS_member pathlink.com> Jan 30 2006
- Lucas Goss <lgoss007 gmail.com> Jan 31 2006
- nick <nick.atamas gmail.com> Jan 29 2006
- Oskar Linde <oskar.lindeREM OVEgmail.com> Jan 29 2006
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?
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: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?
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
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 Igesund
Jan 07 2006
In article <dpo5ud$3m3$1 digitaldaemon.com>, Lars Ivar Igesund says...Fredrik Olsson wrote: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?
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
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 Igesund
Or just exploit D's "array properties feature" and remove the class completely:alias 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: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
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.
Jan 07 2006
Hasan Aljudy skrev:Fredrik Olsson wrote:Lucas Goss skrev:Is there any advantage or benefit to writing a class as:
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: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
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.
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: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: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).
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.
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 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.
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)
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...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 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 +/
Jan 17 2006
Tom S wrote:BCS wrote: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 believe it's Gaussian elimination :) BTW, I've also got a 3d math module: http://158.75.59.9/~h3/tmp/maths.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 later
Jan 17 2006
Tom S wrote:BCS wrote: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 believe it's Gaussian elimination :) BTW, I've also got a 3d math module: http://158.75.59.9/~h3/tmp/maths.d
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?
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.html
Thanks!
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









pragma <pragma_member pathlink.com> 