Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Timing a sort

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Timing a sort

    Alright,

    So I have my class set up with five different sort methods (merge, insertion, etc)...

    My main method looks like this.

    public static void main(String[] args) {
     
    		int[] randomArray = new int[1000];
     
    		System.out.println("Selection Sorted");
    		selectionSort(randomArray);
    		PrintArray(randomArray);

    The print array just prints the sorted array then...

    Now, my teacher gave us a little piece of code that looks like this - it's used to time the amount of time it takes to sort the array.

    public static void time() {
     
    	long elapsedtime;
     
    	Date starttime = new Date();
     
    	for (int j =  1; j  <= 100; j++)
    	{
    		for (int i =  1; i  <= 10000000; i++)
    		{
     
     
    		}
    	}
    	Date endtime = new Date();
     
    	elapsedtime = endtime.getTime()- starttime.getTime();
     
    	System.out.println(elapsedtime);
    }

    Right now it's just a class... but I'm wondering if I can just use it as a method inside my main class that does the sorting of the arrays.

    I'm not sure how to implement that class so that when an array goes through the sort method it also gets timed...

    Thanks


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Timing a sort

    Why not do something like that:
    	long elapsedtime;
    	Date starttime = new Date();
     
    	selectionSort(randomArray);
     
    	Date endtime = new Date();
    	elapsedtime = endtime.getTime()- starttime.getTime();
    	System.out.println(elapsedtime);
    ?
    If you are using Unix system, you can also use time program in your console.

    For example:

    time sleep 1
     
    real	0m1.008s
    user	0m0.004s
    sys	0m0.004s

Similar Threads

  1. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 AM
  2. how to sort objects with collections???
    By kyros in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 02:21 PM
  3. Collection.sort problem
    By Minken in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 21st, 2010, 06:24 PM
  4. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  5. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM