Efficiency question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Efficiency question

Hybrid View

  1. #1
    GregD. Guest

    Efficiency question

    All,

    I am wondering if there is an efficiency to be had here:

    {
    Bank BankArray[] = new Bank[10];

    //Get some data to be used in the bank classes

    for(i=0;i<BankArray.Length;i++)
    Bank[i] = new Bank(somedata0,data,somedata1,somedata2);
    }

    In the first line I have called the default constructor 10 times and then
    later in the for loop call a different constructor ten times.

    It seems like a waste but I'm new enough to the language that I think I
    might have missed something,
    is there anyway to populate the array with bank objects without the calling
    new twice?

    TIA

    Greg



  2. #2
    Paul Clapham Guest

    Re: Efficiency question

    No, in the first line you have created a new array with 10 elements. Those
    elements are all null; no constructors are called and no Bank objects are
    created.

    PC2

    "GregD." <gdavis@No_spam.citynetworks.com> wrote in message
    news:3bf3300f@147.208.176.211...
    > All,
    >
    > I am wondering if there is an efficiency to be had here:
    >
    > {
    > Bank BankArray[] = new Bank[10];
    >
    > //Get some data to be used in the bank classes
    >
    > for(i=0;i<BankArray.Length;i++)
    > Bank[i] = new Bank(somedata0,data,somedata1,somedata2);
    > }
    >
    > In the first line I have called the default constructor 10 times and then
    > later in the for loop call a different constructor ten times.
    >
    > It seems like a waste but I'm new enough to the language that I think I
    > might have missed something,
    > is there anyway to populate the array with bank objects without the

    calling
    > new twice?
    >
    > TIA
    >
    > Greg
    >
    >




  3. #3
    Greg Davis Guest

    Re: Efficiency question


    "Paul Clapham" <pclapham@core-mark.com> wrote in message
    news:3bf3d707$1@147.208.176.211...
    > No, in the first line you have created a new array with 10 elements.

    Those
    > elements are all null; no constructors are called and no Bank objects are
    > created.
    >
    > PC2



    But doesn't using New doesn't call the default constructor?

    Bank BankArray[] = new Bank[10];






  4. #4
    Paul Clapham Guest

    Re: Efficiency question

    NO.

    new Bank() would call the default Bank constructor and create a new Bank
    object.

    new Bank[10] creates a new array object which contains 10 nulls.

    "Greg Davis" <gdavis@nospam.citynetworks.com> wrote in message
    news:3bf405a3@147.208.176.211...
    >
    > "Paul Clapham" <pclapham@core-mark.com> wrote in message
    > news:3bf3d707$1@147.208.176.211...
    > > No, in the first line you have created a new array with 10 elements.

    > Those
    > > elements are all null; no constructors are called and no Bank objects

    are
    > > created.
    > >
    > > PC2

    >
    >
    > But doesn't using New doesn't call the default constructor?
    >
    > Bank BankArray[] = new Bank[10];





  5. #5
    Doug Bell Guest

    Re: Efficiency question


    Two points. First, as has been pointed out in other responses, you are not
    instantiating any Bank objects when you instantiate the array with new Bank[10].
    All that is created is an array of 10 Bank references, all initialized to
    null.

    Second, assuming that the order of the elements in the array is not meaningful,
    there is a minor efficiency to be had by restructuring the loop:

    for (int i = BankArray.length; --i >= 0; )
    Bank[i] = new Bank(somedata0,data,somedata1,somedata2);

    This both moves the reference to BankArray.length out of the loop and replaces
    the explicit test with a test that is readily made implicit by the JIT for
    most processors. (A test against zero following the decrement of the loop
    counter 'i' will not require an explicit test on most processors.) So there
    is a minor efficiency to be gained.

    -Doug


    "GregD." <gdavis@No_spam.citynetworks.com> wrote:
    >All,
    >
    >I am wondering if there is an efficiency to be had here:
    >
    >{
    > Bank BankArray[] = new Bank[10];
    >
    > //Get some data to be used in the bank classes
    >
    > for(i=0;i<BankArray.Length;i++)
    > Bank[i] = new Bank(somedata0,data,somedata1,somedata2);
    >}
    >
    >In the first line I have called the default constructor 10 times and then
    >later in the for loop call a different constructor ten times.
    >
    >It seems like a waste but I'm new enough to the language that I think I
    >might have missed something,
    >is there anyway to populate the array with bank objects without the calling
    >new twice?
    >
    >TIA
    >
    >Greg
    >
    >



  6. #6
    GregD. Guest

    Re: Efficiency question

    "Paul Clapham" <pclapham@core-mark.com> wrote in message news:3bf52857$1@147.208.176.211...
    NO.

    new Bank() would call the default Bank constructor and create a new Bank
    object.

    new Bank[10] creates a new array object which contains 10 nulls.


    ahh... I see now,

    Paul and Doug, thank you both for your help.




  7. #7
    GregD. Guest

    Re: Efficiency question


    "Doug Bell" <dbell@san.rr.com> wrote in message news:3bf5f7b6$1@147.208.176.211...

    Second, assuming that the order of the elements in the array is not meaningful,
    there is a minor efficiency to be had by restructuring the loop:

    for (int i = BankArray.length; --i >= 0; )
    Bank[i] = new Bank(somedata0,data,somedata1,somedata2);

    This both moves the reference to BankArray.length out of the loop and replaces
    the explicit test with a test that is readily made implicit by the JIT for
    most processors. (A test against zero following the decrement of the loop
    counter 'i' will not require an explicit test on most processors.) So there
    is a minor efficiency to be gained.

    Doug, this is good stuff to know. thank you for your advise.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center