go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Dynamic Collection Sorting: java.util.TreeSet
 
Subject: Dynamic Collection Sorting: java.util.TreeSet
Author: EricJ
In response to: Collection Sorting: Static vs. Dynamic
Posted on: 02/17/2009 09:00:37 PM

public class DynamicSorting
{
    public static void main(String[] argv)
    {
        long t1, t2;
        int max = 1000000;
		
        t1=System.currentTimeMillis();

        TreeSet mySet = new TreeSet();
        for(int i=0; i<max; i++){
            mySet.add("ABCDEFG"+Math.random()*100000);
        }		
		
        t2=System.currentTimeMillis();
		
        Runtime rt = Runtime.getRuntime();
        long memoryUsed = rt.totalMemory() - rt.freeMemory();
        System.out.println(mySet.first());
        System.out.println("Time consumed="+(t2-t1)+" (ms)   Memory used="+memoryUsed+ "(bytes)" );
    	
    }
}



Here is the output:

ABCDEFG0.07930068373074306
Time consumed=17328 (ms) Memory used=124260248(bytes)


 

> On 02/17/2009 08:51:40 PM EricJ wrote:


Static Collection Sorting: java.util.Collections.sort()

public class StaticSorting
{
    public static void main(String[] argv)
    {
        long t1, t2;
        int max = 1000000;
		
        t1=System.currentTimeMillis();
		
        List myList = new ArrayList();
        for(int i=0; i<max; i++){
            myList.add("ABCDEFG"+Math.random()*100000);
        }
        Collections.sort(myList);
		
        t2=System.currentTimeMillis();
		
        Runtime rt = Runtime.getRuntime();
        long memoryUsed = rt.totalMemory() - rt.freeMemory();
        System.out.println(myList.get(0));
        System.out.println("Time consumed="+(t2-t1)+" (ms)   Memory used="+memoryUsed+ "(bytes)" );
    	
    }
}


Here is the output:

ABCDEFG0.3944624056728685
Time consumed=10813 (ms) Memory used=103774088(bytes)





References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.