Performance test report on MAP and LIST

xiaoxiao2021-03-06  41

Note: The test code I have involved is for 1 million records. My hardware configuration is as follows: CPUP4 1.5G, 256M DDR memory.

If you want to test the code, you should use this way: java -xms128m -xmx250m Testmap

1. Requirements for memory capacity

The MAP object is higher than the List. Because MAP also requires an Object's key in addition to Value, it increases the capacity of the MAP. The test code is as follows:

Import java.util. *;

Public class testmap {

Public static void main (String [] args) {

System.out.Println ("Begin >>>>>>>>>>>>>>

Long SM = runtime.getRuntime (). TotalMemory ();

System.out.println ("Begin Memory:" SM);

Map m = new hashtable ();

// linkedlist tmplist = new linkedList ();

For (int i = 0; i <1000000; i ) {

M.PUT ("" i, "i =" i);

// integer = new integer (i);

//tmplist.add(integer);

}

Long em = runtime.getRuntime (). Totalmemory ();

System.out.println ("End Memory:" EM);

System.out.println ("End-Start Memory:" (EM - SM));

// walklist (tmplist);

System.out.println ("<<<<<<<<<<<<<<<<");

// Find (m, "985757");

Walkmap (m);

// list tmplist = new linkedList ();

//tmplist.addall (m.values ​​());

}

}

The comparison results are as follows: 1 million records maps about 70m extra space, and List does not need, its extra space is 0

2. Traverse

I am experimenting, I found that we often use the way of traversing the way is a wrong traversal way, and its performance is very bad. We often traverse the List code instance, as follows:

For (int I = 0; i

Object obj = list.get (i);

}

If the traversal of the list is changed, its performance and MAP are similar, we will expand the above code, thus compare the traversal speed of MAP and LIST:

Import java.util. *;

/ **

* @Author Whxu

* @version 1.0.0 2003-4-22 am

* /

Public class testmap {

Public static void main (String [] args) {

System.out.Println ("Begin >>>>>>>>>>>>>>); long SM = runtime.getRuntime (). TotalMemory ();

System.out.println ("Begin Memory:" SM);

Map m = new hashtable ();

// linkedlist tmplist = new linkedList ();

For (int i = 0; i <1000000; i ) {

M.PUT ("" i, "i =" i);

// integer = new integer (i);

//tmplist.add(integer);

}

Long em = runtime.getRuntime (). Totalmemory ();

System.out.println ("End Memory:" EM);

System.out.println ("End-Start Memory:" (EM - SM));

// walklist (tmplist);

System.out.println ("<<<<<<<<<<<<<<<<");

// Find (m, "985757");

Walkmap (m);

// list tmplist = new linkedList ();

//tmplist.addall (m.values ​​());

}

Public Static Void Find (Map M, String Key) {

Long ST = system.currenttimemillis ();

Object obj = m.get (key);

System.out.println ("Object:" OBJ);

Long et = system.currenttimemillis ();

System.out.println ("Start Time:" ST);

System.out.println ("End Time:" ET);

System.out.println ("Use Time:" (ET - ST));

}

Public Static Void Walkmap (Map M) {

Long ST = system.currenttimemillis ();

ITerator it = m.values ​​(). Iterator (); it.hasnext ();) {

Object obj = it.next ();

}

Long et = system.currenttimemillis ();

System.out.println ("Walkmap Start Time: ST);

System.out.println ("Walkmap End Time: ET);

System.out.Println ("Walkmap Use Time:" (et - st));

}

Public Static Void Walklist (LinkedList L) {

Long ST = system.currenttimemillis ();

Object obj = NULL;

//System.out.println (L.Get (3434 )); / * This performance of this traversal method is poor, it is recommended not to use

For (int i = 0; i

Obj = L.GET (i);

}

* /

Iterator it = L.ITerator (); it.hasnext ();) {

Obj = it.next ();

}

Long et = system.currenttimemillis ();

System.out.println ("Walklist Start Time: ST);

System.out.println ("Walklist End Time: ET);

System.out.println ("Walklist Use Time:" (et - ST));

}

}

3. Find randomly. If Map knows KEY, List knows INDEX, its performance has no difference in one million data. The example code is as follows:

Import java.util. *;

/ **

* @Author Whxu

* @version 1.0.0 2003-4-22 am

* /

Public Class Testmap

{

Public static void main (string [] args)

{

System.out.Println ("Begin >>>>>>>>>>>>>>

Long SM = runtime.getRuntime (). TotalMemory ();

System.out.println ("Begin Memory:" SM);

Map m = new hashtable ();

LinkedList TMPLIST = New LinkedList ();

For (int i = 0; i <1000000; i ) {

//m.put("" i, "i= " i);

TMPLIST.ADD ("i =" i);

}

Long em = runtime.getRuntime (). Totalmemory ();

System.out.println ("End Memory:" EM);

System.out.println ("End-Start Memory:" (EM - SM));

// walklist (tmplist);

System.out.println ("<<<<<<<<<<<<<<<<");

// Find (m, "985757");

Find (TMPLIST, 1000000-1);

// walkmap (m);

// list tmplist = new linkedList ();

//tmplist.addall (m.values ​​());

}

Public Static Void Find (Map M, String Key) {

Long ST = system.currenttimemillis ();

Object obj = m.get (key);

System.out.println ("Find Map Object:" OBJ);

Long et = system.currenttimemillis (); system.out.println ("Find Map Start Time: ST);

System.out.println ("Find Map End Time: ET);

System.out.println ("Find Map Use Time: (et - st));

}

Public Static Void Find (List L, INDEX) {

Long ST = system.currenttimemillis ();

Object obj = L.Get (Index);

System.out.println ("Find List Object: Obj);

Long et = system.currenttimemillis ();

System.out.Println ("Find List Start Time: ST);

System.out.println ("Find List End Time: ET);

System.out.println ("Find List Use Time: (et - st));

}

Public Static Void Walkmap (Map M) {

Long ST = system.currenttimemillis ();

ITerator it = m.values ​​(). Iterator (); it.hasnext ();) {

Object obj = it.next ();

}

Long et = system.currenttimemillis ();

System.out.println ("Walkmap Start Time: ST);

System.out.println ("Walkmap End Time: ET);

System.out.Println ("Walkmap Use Time:" (et - st));

}

Public Static Void Walklist (LinkedList L) {

Long ST = system.currenttimemillis ();

Object obj = NULL;

//System.out.println (L.Get (3434));

/ * for (int i = 0; i

Obj = L.GET (i);

}

* /

Iterator it = L.ITerator (); it.hasnext ();) {

Obj = it.next ();

}

Long et = system.currenttimemillis ();

System.out.println ("Walklist Start Time: ST);

System.out.println ("Walklist End Time: ET);

System.out.println ("Walklist Use Time:" (et - ST));

}

}

转载请注明原文地址:https://www.9cbs.com/read-69090.html

New Post(0)