G
Guest
Hey Everyone -
I'm trying to determine the fastest way of moving array information around
and could use some help.
Here's the setup:
Class A stores a DateTime and an amount (a payment).
Class B is functionally an array of A objects (representing a cashflow).
Class C is functionally an array of B objects (representing an organization
of cashflows).
Class D contains a C object.
Class E contains a static method that takes a collection of D objects, does
some computations, creates a bunch of B objects, one at a time, and basically
calls D.C.Add(B) for each.
I'm trying to determine the fastest way of moving the information created in
E into the appropriate D objects. I've tried various situations including
having B and C extend CollectionBase, changing the ArrayList of B objects in
C to an array, using AddRange() vs. CopyTo(), etc...
1. class E
2. {
3. static Method(D[] pD)
4. {
5. C mC = new C();
6. foreach (D d in pD)
7. {
8. B mB = Compute(d);
9. mC.Add(mB);
10. }
11. for (int i = 0; i < mC.Length; i++)
12. {
13. pD.c = new C(1);
14. pD.c[0].b = new B();
15. pD.c[0].b.AddRange(mC);
16. }
17. }
18. }
Basically, I want to optimize lines 13 - 15. Here are some setups I used
for lines 13-15 and the resulting time the method took to run those lines
when pD contains a total of ~20million A objects:
Case 1:
14. pD.c[0] = new B(mC.Length); // sets the capacity for b's list
15. pD.c[0].b.AddRange(mC);
Time: 5.23 seconds
Case 2:
14. pD.c[0] = new B(mC.Length); // sets the capacity for b's list
15. pD.c[0].b = mC; // direct access to b
Time: 4.16 seconds
Case 3:
13-14. pD.c.Add(new B(mC.Length)); // sets the capacity for b's list
and adds the new b to c
15. pD.c[0].b = mC;
Time: 5.76 seconds
Case 4:
13-14. pD.c.Add(new B(mC.Length)); // sets the capacity for b's list
and adds the new b to c
15. mC.CopyTo(mD.c[0].b, 0);
Time: 5.18 seconds
Case 5:
Then I tried the following just for testing purposes and found that it ran
much more quickly.
13. ArrayList mT = new ArrayList();
14. B[] mB = new B[mC.Length];
14.5. mT.Add(mB);
15. mC.CopyTo((B[])mT[0], 0);
Time: 0.15 seconds!!!
It creates dummy objects limited to the scope of E.Method() and moves the
computed mC[] information to those dummy objects just to see how fast the
operation could take.
Any thoughts on why it takes at least 4 more seconds setting the computed
information to pD versus the setup used in Case 5? Or any suggestions on how
to really streamline this operation? Hopefully at least some of this makes
sense...
Thanks in advance.
Craig
I'm trying to determine the fastest way of moving array information around
and could use some help.
Here's the setup:
Class A stores a DateTime and an amount (a payment).
Class B is functionally an array of A objects (representing a cashflow).
Class C is functionally an array of B objects (representing an organization
of cashflows).
Class D contains a C object.
Class E contains a static method that takes a collection of D objects, does
some computations, creates a bunch of B objects, one at a time, and basically
calls D.C.Add(B) for each.
I'm trying to determine the fastest way of moving the information created in
E into the appropriate D objects. I've tried various situations including
having B and C extend CollectionBase, changing the ArrayList of B objects in
C to an array, using AddRange() vs. CopyTo(), etc...
1. class E
2. {
3. static Method(D[] pD)
4. {
5. C mC = new C();
6. foreach (D d in pD)
7. {
8. B mB = Compute(d);
9. mC.Add(mB);
10. }
11. for (int i = 0; i < mC.Length; i++)
12. {
13. pD.c = new C(1);
14. pD.c[0].b = new B();
15. pD.c[0].b.AddRange(mC);
16. }
17. }
18. }
Basically, I want to optimize lines 13 - 15. Here are some setups I used
for lines 13-15 and the resulting time the method took to run those lines
when pD contains a total of ~20million A objects:
Case 1:
14. pD.c[0] = new B(mC.Length); // sets the capacity for b's list
15. pD.c[0].b.AddRange(mC);
Time: 5.23 seconds
Case 2:
14. pD.c[0] = new B(mC.Length); // sets the capacity for b's list
15. pD.c[0].b = mC; // direct access to b
Time: 4.16 seconds
Case 3:
13-14. pD.c.Add(new B(mC.Length)); // sets the capacity for b's list
and adds the new b to c
15. pD.c[0].b = mC;
Time: 5.76 seconds
Case 4:
13-14. pD.c.Add(new B(mC.Length)); // sets the capacity for b's list
and adds the new b to c
15. mC.CopyTo(mD.c[0].b, 0);
Time: 5.18 seconds
Case 5:
Then I tried the following just for testing purposes and found that it ran
much more quickly.
13. ArrayList mT = new ArrayList();
14. B[] mB = new B[mC.Length];
14.5. mT.Add(mB);
15. mC.CopyTo((B[])mT[0], 0);
Time: 0.15 seconds!!!
It creates dummy objects limited to the scope of E.Method() and moves the
computed mC[] information to those dummy objects just to see how fast the
operation could take.
Any thoughts on why it takes at least 4 more seconds setting the computed
information to pD versus the setup used in Case 5? Or any suggestions on how
to really streamline this operation? Hopefully at least some of this makes
sense...
Thanks in advance.
Craig