Thanks again,

I have tested the code:
public static void main(String[] args) {
        final int Z = 250000;
        Timer t1 = new Timer();
        for (int i = 1; i <= Z; i++){
                String[] strArray={String.valueOf(i*4), String.valueOf(i*5), 
String.valueOf(i*6), String.valueOf(i*7)};
                arlQueryResult.add(strArray);
        }
        t1.print("Adding ArrayList using new array directly");
}

Where....

public class Timer {
/**
 * Author: Glen McCluskey
 * E-mail: [email protected]
 * http://www.glenmccl.com/jperf/index.htm
 */
       long t;
        // constructor
        public Timer() {
                reset();
        }
        // reset timer
        public void reset() {
                t = System.currentTimeMillis();
        }
        // return elapsed time
        public long elapsed() {
                return System.currentTimeMillis() - t;
        }
        // print explanatory string and elapsed time
        public void print(String s) {
                 System.out.println(s + ": " + elapsed());
        }

}

And the elapsed time for 3 times running is: 1844, 1852, 1897
While using the previous code, the value is: 2154, 2167, 2174

Thus, you are proved to be right and you can see the efficiency.

Thanks again,
Patrick

PS.  
This is out of the topic, but when I changed Z to one million, the
system hold for a moment and print no output, not even an error message.
Is this because of out of memory or what?  How can I catch the error
message? How to deal with the phenomenon?  I'm using Ubuntu Linux 8.04
and Eclipse.



On Mon, 2009-01-26 at 03:27 +0200, Vasile Braileanu wrote:
> Hi,
> 
> Well, as I said, Ewald gived the perfect solution: create a new
> array. 
> Why? Because clone will effectively clone your object making this
> operations:
> 1. create a new object, with a new reference
> 2. copy all content from first object to second
> Note that second operation can be very costly. Here is not the case(we
> have very small data) but think at a database with million rows.
> 
> Scenario in our case(with clone):
> 1. Fill the array with the new values
> 2. create a new object with clone, create a new reference for the
> cloned object
> 3. copy data from original to clone
> 4. transmit the reference of the cloned object to add method.
> 
> Now, look here what I meant to say:
> ...
> for (int i = 1; i <= 2; i++){
> addElement(String.valueOf(i*4), String.valueOf(i*5),
> String.valueOf(i*6), String.valueOf(i*7));
> }
> ...
> 
> private static void addElement(String a1, String a2, String a3, String
> a4)
>         {
>             String[] strArray={a1,a2,a3,a4};
>             arlQueryResult.add(strArray);
>         }
> 
> Scenario in this case:
> 1. transmit parameters to addElement method (no need for array in main
> method)
> 2. create a new array
> 3. add 
> Here, we don't copy any data, so this scenario is faster. Is true, we
> have a function here so the arguments will be transmitted through
> stack but we also can write the code inside and creating a new
> String[] every time, as said Ewald:
> 
> for(int i = 1; i <=2; i++){
> String[] strArray={String.valueOf(i*4), String.valueOf(i*5),
> String.valueOf(i*6), String.valueOf(i*7)};
> arlQueryResult.add(strArray);
> }
> 
> This is the fastest code, does not have first step from above(transmit
> parameters to a function). 
> I have used clone() method to make you understand the fact you passed
> the same object. Sometimes the clone() method is very useful, but in
> this case is not.
> 
> Happy coding!
> 
>  


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to