Hi Yong Hua Lee,
You must be using the following code to measure the running time of
the method , which would
return you the exact time in milliseconds which the metod took to execute :
public class TimeTest {
public static void main(String[] args) {
Long start_time = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
int j = i;
}
Long end_time = System.currentTimeMillis();
Long elapsed_time = end_time - start_time;
System.out.println("That took " + elapsed_time + "
milliseconds");
}
}
which would return you the correct results .
And for program 3.1 dont expect System.gc() to make JVM collect your
objects :) ...
You cannot force garbage collection you can ask jvm to do a GC using
System.gc() (or) Runtime.getRuntime().gc();
Its up to the JVM to do a Garbage Collection.
Thanks,
Ashok A V
On Thu, Aug 6, 2009 at 9:57 PM, Yong Hua Lee<[email protected]> wrote:
>
> Hello,
>
> I was unable to get the date/time information consistently.
>
> Exe 1.3:
> **********************************************************************
> Date d1 = new Date();
> for (int i=0; i<10000000; i++) { int j = i;}
>
> Date d2 = new Date();
>
> long elapsed_time = d2.getTime() - d1.getTime();
> System.out.println("That took " + elapsed_time
> + " milliseconds");
> }
> **********************************************************************
> I will normally get d2.getTime() - d1.getTime() = 0
> But if I ran it again, it will be able to get the time different.
>
> Exe 3.1
> ********************************************************************************************
> import java.io.*;
> class SystemDemo {
> public static void main(String args[]) throws IOException {
> int arr1[] = new int[1050000];
> int arr2[] = new int[1050000];
> long startTime, endTime;
> /* initialize arr1 */
> for (int i = 0; i < arr1.length; i++) {
> arr1[i] = i + 1;
> }
>
> /* copying manually */
> startTime = System.currentTimeMillis();
> System.out.println("start time: " + startTime + " ms.");
> for (int i = 0; i < arr1.length; i++) {
> arr2[i] = arr1[i];
> }
> endTime = System.currentTimeMillis();
> System.out.println("end time: " + endTime + " ms.");
> System.out.println("Time for manual copy: " + (endTime-startTime) + "
> ms.");
>
> /* using the copy utility provided by java */
> startTime = System.currentTimeMillis();
> System.out.println("start time: " + startTime + " ms.");
> System.arraycopy(arr1, 0, arr2, 0, arr1.length);
> endTime = System.currentTimeMillis();
> System.out.println("end time: " + endTime + " ms.");
> System.out.println("Time when using method of array copy: " +
> (endTime-startTime) + " ms.");
> System.gc(); //force garbage collector to work
> //System.setIn(new FileInputStream("temp.txt"));
> System.exit(0);
>
> }
>
> }
> ***********************************************************************************************************
> Sometime I get Time for manual copy and array copy = 0ms;
> But I can only get the manual copy time = 15ms but array copy time
> still 0ms after few run.
> If i try to debug the code by inserting a System.out.println
> (startTime), I might get the result the other way around.
> which is manual copy time = 0ms and array copy time = 15ms.
>
> Pls help... I'm not sure what is causing this strange behavior.
>
> BestRegards,
> YH
>
> >
>
--
Victory belongs to the most persevering.
- Napoleon
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---