Talk about Java1.5 and .Net 2.0 Generics (CCID)

xiaoxiao2021-03-06  59

Java's latest 1.5se (market name is J2se 5.0, code tiger) and Microsoft's .NET 2.0 now has surfaced, .NET 2.0 Although there are still some days from the last release, it's a basic framework. Feeling, evaluating the BETA version of Java 1.5 and .Net 2.0 or to see the overall situation and technical characteristics of these two technologies more accurately. Today I want to discuss the Java 1.5 and .Net 2.0 generics.

Generics is a very important new language feature for Java and .Net. Familiar with Template in C may not be unfamiliar with this concept. In essence, Generics is to make your type (TYPE) with parameter capabilities, the so-called parameterization type or parameter polymorphism. With .NET and C # designer Anders Hejlsberg's words, "Generics is essentially the ability to have type parameters on your type. They are also called parameterized types or parametric polymorphism." In theory, Generics should have such a prominent four The advantages.

· Type security

Let's take a simplest example. In the previous Java or .NET, if we joined an object in ArrayList, ArrayList regards this object as the broadest Object type. When removing the object, we must first turn into a corresponding object (Casting), this type conversion operation is an unavoidable overhead, and is also an operation without type of security. Because the object you taken out can be any type. In many cases, we hope that an arraylist is used to store a certain object, such as a String object. But the compiler can't protect our goal. Others can join this ArrayList to any object. When compiling, the compiler considers that this is a legitimate operation and gives compilation. But such a program will have problems when running. Because you don't have a String object from this ArrayList. You will have an exception when you translate into a string type.

With generics, this problem can be avoided. For example, in Java 1.5, you can use ArrayList this way.

ArrayList

MyList = new arraylist

();

In this case, the compiler will find all possible types of matching problems when compiling. Trying to add other non-String objects to this arraylist will be considered illegal. This ensures that the added object must be String type. The object taken from this ArrayList must also be String type, you don't need to turn it again.

· Binary code reuse

Some people may say, for your questions above, don't use generics, I have the same way to solve. For example, packaging the ArryList or derived a new class from ArryList, changing the parameter type of the function in this new class, ensuring that only String objects can be added.

This is ok, but the problem is also very obvious. For String type, you created a new type, so about Integer, Double, DateTime, and user-defined types? You have to create a new specialty class for each type, which is very cumbersome and easy to make mistakes. These new types of functions are similar, and the functionally similar functions are defined and implemented again, which is not compliant with object-oriented programming principles. Use generics, these issues can be avoided. Your algorithm can be reused to maximize. If you need to change, then you only need to modify a place.

· Performance improvement

Using generics, type security issues are determined when compiling, not at runtime. In addition, we also avoid the cost of Casting Overhead and the cost of packaging and packaging, which makes the program performance greatly. Interestingly, Java and .net have a huge difference in performance issues due to achieved differences, which will be discussed in detail later.

· Semantic clear

Using generics, our program becomes clearer and clear. Programs with potential types of security issues will not be compiled. In .NET, we can also restrict generics, which can further clarify the intent and purpose of the program to reduce the ambiguity.

So, is Java and .NET's generics to implement these commitments? We can look at the following simple programs.

This program is very simple, it is prepared in advance, these three arrays store integer, double precision floating point and string, and then we will put these things separately in the use of the Collection object that uses generics, and does not use the objects of Generics. Then take it out and see how they can express it.

Java 1.5 program (NetBeans 4.0 IDE supporting generics)

Import java.util.linkedList;

Import java.util.arraylist;

Import java.util.list;

Import java.util.vector;

/ **

* @Author jinsong zhang

* /

Public class javagenerics {

Private int [] m_testint = NULL;

Private double [] m_testdouble = null;

Private string [] m_teststring = null;

Private int m_datasize = 0;

Private int m_loopnum = 0;

Public Javagenerics (int Datasize, int loopnum) {

THIS.M_DATASIZE = DataSize;

THIS.M_LOOPNUM = LOOPNUM;

Preparedata (m_datasize);

}

Private void prepaaredata (int Datasize) {

m_testint = new int [DataSize];

m_testdouble = new double [dataize];

m_teststring = new string [dataize];

For (int i = 0; i

m_testint [i] = i;

m_testdouble [i] = i * 1.0; m_teststring [i] = double.tostring (m_testdouble [i]);

}

}

Private void testin () {

Long StartTime = system.currenttimemillis ();

Long Totalint = 0;

For (int Num = 0; Num

LinkedList ilist = new linkedList ();

For (int i = 0; i

IList.add (m_testint [i]);

For (Integer i: ILIST)

Totalint = i;

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Generics LinkedList Takes Time:

" Time " Millseconds. Result: " Totalint);

}

Private void testintb () {

Long StartTime = system.currenttimemillis ();

Long Totalint = 0;

For (int Num = 0; Num

List ilist = new arraylist ();

For (int i = 0; i

IList.add (m_testint [i]);

For (Integer i: ILIST)

Totalint = i;

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Generics ArrayList Takes Time:

" Time " Millseconds. Result: " Totalint);

}

Private void testintc () {

Long StartTime = system.currenttimemillis ();

Long Totalint = 0;

For (int Num = 0; Num

ArrayList ilist = new arraylist ();

For (int i = 0; i

IList.add (m_testint [i]);

For (Object I: ILIST)

Totalint = (Integer i) .intValue ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Regular ArrayList Takes Time:

" Time " Millseconds. Result: " Totalint);

}

Private void testdoublea () {

Long StartTime = system.currenttimemillis (); double totaldouble = 0;

For (int Num = 0; Num

LinkedList DList = New LinkedList ();

For (int i = 0; i

DList.Add (m_testdouble [i]);

For (Double D: DLIST)

Totaldouble = D. DoubleValue ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Generics LinkedList Takes Time:

" Time " Millseconds. Result: " Totaldouble);

}

Private void testdoubleb () {

Long StartTime = system.currenttimemillis ();

Double totaldouble = 0;

For (int Num = 0; Num

List dlist = new arraylist ();

For (int i = 0; i

DList.Add (m_testdouble [i]);

For (Double D: DLIST)

Totaldouble = D. DoubleValue ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Generics ArrayList Takes Time:

" Time " Millseconds. Result: " Totaldouble);

}

Private void testdoucast () {

Long StartTime = system.currenttimemillis ();

Double totaldouble = 0;

For (int Num = 0; Num

Arraylist dlist = new arraylist ();

For (int i = 0; i

DList.Add (m_testdouble [i]);

For (Object D: DLIST)

Totaldouble = ((double) d) .doublevalue ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Regular ArrayList Takes Time:

" Time " Millseconds. Result: " Totaldouble);

}

Private void teststringa () {

Long StartTime = system.currenttimemillis ();

String Temp = NULL; for (int Num = 0; Num

LinkedList Slist = new linkedList ();

For (INT i = 0; I

Slist.add (m_teststring [i]);

}

For (String S: SLIST)

Temp = S.touppercase ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Generics LinkedList Takes Time:

" Time " MillSeconds. Result: " TEMP);

}

Private void teststringb () {

Long StartTime = system.currenttimemillis ();

String Temp = NULL;

For (int Num = 0; Num

List slist = new arraylist ();

For (INT i = 0; I

Slist.add (m_teststring [i]);

}

For (String S: SLIST)

Temp = S.touppercase ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Generics ArrayList Takes Time:

" Time " MillSeconds. Result: " TEMP);

}

Private void teststringc () {

Long StartTime = system.currenttimemillis ();

String Temp = NULL;

For (int Num = 0; Num

ArrayList slist = new arraylist ();

For (int i = 0; i

Slist.add (m_teststring [i]);

For (Object S: SLIST)

Temp = (String) s) .touppercase ();

}

Long Time = System.currentTimeMillis () - StartTime;

System.out.println ("Using Regular ArrayList Takes Time:

" Time " MillSeconds. Result: " TEMP);

}

Public static void main (string [] args) throws exception {

IF (args.length! = 2) {

System.out.println ("USAGE: Javagenerics DataSize LoopNumber");

System.exit (0);

}

INT DATASIZE = INTEGER.PARSEINT (Args [0]); int loopnumber = integer.parseint (args [1]);

Javagenerics obj = new javagenerics (dataize, loopnumber);

System.out.println ("/ NPRIMARY TYPE INT TEST ...");

Obj.TestinTa ();

Obj.TestinTB ();

Obj.testintc ();

System.out.println ("/ NPRIMARY TYPE DOUBLE TEST ...");

Obj.testdoublea ();

Obj.testdoubleb ();

Obj.testdoublec ();

System.out.println ("/ NReference Type String Test ...");

Obj.teststringa ();

Obj.teststringb ();

Obj.teststringc ();

}

}

Next article We will introduce .NET 2.0 programs (using Visual Studio 2005 Beta1 IDE supporting generics). (T111)

.NET 2.0 program (Visual Studio 2005 Beta1 IDE supporting generics)

#Region Using Directives

Using system;

Using system.collections;

Using system.collections.Generic;

Using system.text;

#ndregion

Namespace generictest

{

Class CsharpGenerics

{

Private int [] m_testint = NULL;

Private double [] m_testdouble = null;

Private string [] m_teststring = null;

Private int m_datasize = 0;

Private int m_loopnum = 0;

Public CSHARPGENERICS (int Datasize, int loopnum)

{

THIS.M_DATASIZE = DataSize;

THIS.M_LOOPNUM = LOOPNUM;

Preparedata (m_datasize);

}

Private Void Preparedata (int Datasize)

{

m_testint = new int [DataSize];

m_testdouble = new double [dataize];

m_teststring = new string [dataize];

For (int i = 0; i

{

m_testint [i] = i;

m_testdouble [i] = i * 1.0d;

M_teststring [i] = m_testdouble [i] .tostring ();

}

}

Private void testinta ()

{

Long StartTime = system.environment.tickcount;

Long Totalint = 0;

For (int Num = 0; Num

{

LinkedList ilist = new limitedList (); for (int i = 0; i

IList.Addtail (M_Testint [i]);

Foreach (Int I in ILIS)

Totalint = i;

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Generics LinkedList Takes Time:

" Time " Millseconds. Result: " Totalint);

}

Private void testin ()

{

Long StartTime = system.environment.tickcount;

Long Totalint = 0;

For (int Num = 0; Num

{

List ilist = new list ();

For (int i = 0; i

IList.add (m_testint [i]);

Foreach (Int I in ILIS)

Totalint = i;

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Generics List Takes Time:

" Time " Millseconds. Result: " Totalint);

}

Private void testintc ()

{

Long StartTime = system.environment.tickcount;

Long Totalint = 0;

For (int Num = 0; Num

{

ArrayList ilist = new arraylist ();

For (int i = 0; i

IList.add (m_testint [i]);

Foreach (Object I in ILIST)

Totalint = Convert.TOINT32 (i);

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Regular Arraylist Takes Time:

" Time " Millseconds. Result: " Totalint);

}

Private void testdoublea ()

{

Long StartTime = system.environment.tickcount;

Double totaldouble = 0;

For (int Num = 0; Num

{

LinkedList DList = New LinkedList ();

For (int i = 0; i

Foreach (double i in dlist)

Totaldouble = i;

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Generics LinkedList Takes Time:

" Time " Millseconds. Result: " Totaldouble);

}

Private void testdoubleb ()

{

Long StartTime = system.environment.tickcount;

Double totaldouble = 0;

For (int Num = 0; Num

{

List dlist = new list ();

For (int i = 0; i

DList.Add (m_testdouble [i]);

Foreach (double i in dlist)

Totaldouble = i;

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Generics List Takes Time:

" Time " Millseconds. Result: " Totaldouble);

}

Private void testdoublec ()

{

Long StartTime = system.environment.tickcount;

Double totaldouble = 0;

For (int Num = 0; Num

{

Arraylist dlist = new arraylist ();

For (int i = 0; i

DList.Add (m_testdouble [i]);

Foreach (double i in dlist)

Totaldouble = Convert.Todouble (i);

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Regular Arraylist Takes Time:

" Time " Millseconds. Result: " Totaldouble);

}

Private void teststringa ()

{

Long StartTime = system.environment.tickcount;

String Temp = NULL;

For (int Num = 0; Num

{

LinkedList Slist = new linkedList ();

For (int i = 0; I

FOREACH (String I in Slist)

Temp = i.toupper ();

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Generics LinkedList Takes Time:

" Time " MillSeconds. Result: " TEMP);

}

Private void teststringb ()

{

Long StartTime = system.environment.tickcount;

String Temp = NULL;

For (int Num = 0; Num

{

List slist = new list ();

For (int i = 0; i

Slist.add (m_teststring [i]);

FOREACH (String I in Slist)

Temp = i.toupper ();

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Generics List Takes Time:

" Time " MillSeconds. Result: " TEMP);

}

Private void teststringc ()

{

Long StartTime = system.environment.tickcount;

String Temp = NULL;

For (int Num = 0; Num

{

ArrayList slist = new arraylist ();

For (int i = 0; i

Slist.add (m_teststring [i]);

FOREACH (String I in Slist)

Temp = (String) .toupper ();

}

Long time = system.environment.tickcount - starttime;

Console.writeline ("Using Regular Arraylist Takes Time:

" Time " MillSeconds. Result: " TEMP);

}

Public static void main (string [] args)

{

IF (args.length! = 2)

{

Console.writeline ("USAGE: GenericTest DataSize LoopNum);

Return;

}

INT DATASIZE = Convert.Toint32 (Args [0]);

INT loopNumber = Convert.Toint32 (Args [1]);

CsharpGenerics obj = new csharpGenerics (DATASIZE, LOOPNUMBER); Console.WriteLine ("/ NValue Type Int Test ...");

Obj.TestinTa ();

Obj.TestinTB ();

Obj.testintc ();

Console.Writeline ("/ NValue Type Test ...");

Obj.testdoublea ();

Obj.testdoubleb ();

Obj.testdoublec ();

Console.writeline ("/ NReference Type String Test ...");

Obj.teststringa ();

Obj.teststringb ();

Obj.teststringc ();

}

}

}

The above two programs are almost exactly the same. There are a few situations to explain here.

1. Both Java and .Net provide LinkedList for generics, so we can directly compare the performance of both.

2. Java and .net provide non-generics ArrayList, which is traditional ArrayList, so we can directly compare the performance of both.

3. Java offers generics and non-generics ArrayList, so we can compare the use of generics's performance. This is one of our focus today.

4. .NET provides non-generics ArrayList, but unfortunately there is no generics. According to Microsoft, we can use the corresponding Generics List class, which is very similar to traditional arraylist features and intentions. The difference in performance of ArrayList and generics List in the .NET is a focus on our test today.

5. Due to the Java's HotSpot work characteristics, I have an external loop to test, which is the purpose of making Java's HotSpot to optimize the program to achieve the best performance.

Here is the result of the test. The two sets of results are given here, respectively, the amount of data is 100,000, the number of cycles is 100 and the amount of data of 500,000, and the number of cycles is 20.

From the above chart, we can draw the following conclusions:

1. In all test items, .NET 2.0 performance is much higher than the performance of Java 1.5. Why have this result is not the topic we have to discuss today. We are concerned about the benefits of Generics bringing these two technologies.

2. In the four characteristics of the Generics listed earlier, type security, binary code reuse, and semantic clear, these three features are well reflected in Java and .NET.

3. Used generics, Java did not achieve any performance improvement. The most direct performance is that generics and non-Generics ArrayList have little differences in performance, whether the basic data type, such as integer, floating point, or reference String object. This may be unexpected to many people.

4. Used generics, .NET's performance has been upgraded. Especially the integer, the number of double precision floating point (Value Type), and the performance has been improved. For reference types, performance is also significantly improved.

The next article let us go deep into their internal and see what the mechanism of their internal work is like. (T111)

Also generics, why do the two have such a big difference in performance? Now let us go deep into their internal and see what the mechanism of their internal work is. First, then we use the anti-compilation tool to see what the Code generated by both. (This article uses DJ Java Decompiler and Lutz Roeder's Reflector.exe, as shown in the figure)

For example, for int and string paragraphs, we get this program (Double and Int, in order to save space, this is not specifically listed here).

Private void testinta ()

{

Long L = system.currenttimemillis ();

Long L1 = 0L;

For (int i = 0; i

{

LinkedList LinkedList = new linkedList ();

For (int J = 0; j

LinkedList.Add (Integer.Valueof (M_Testint [J]));

Iterator itrator = LinkedList.ITerator (); item.hasnext ();)

{

Integer Integer = (Integer) Iterator.next ();

L1 = integer.intValue ();

}

}

Long l2 = system.currenttimemillis () - L;

System.out.println ((New StringBuilder ()). Append ("Using Generics LinkedList Takes Time:

") .Append (l2) .append (" Millseconds. Result: ") .append (l1) .tostring ());

}

Private void testin ()

{

Long L = system.currenttimemillis ();

Long L1 = 0L;

For (int i = 0; i

{

ArrayList arraylist = new arraylist ();

For (int J = 0; j

ArrayList.Add (Integer.Valueof (m_testint [j]));

Itelist.iterator (); item.hasnext ();)

{

Integer Integer = (Integer) Iterator.next ();

L1 = integer.intValue ();

}

}

Long l2 = system.currenttimemillis () - L;

System.out.println (NEW STRINGBUILDER ()). Append ("Using Generics ArrayList Takes Time:

") .Append (l2) .append (" Millseconds. Result: ") .append (l1) .tostring ());

}

Private void testintc ()

{

Long l = system.currenttimemillis (); long l1 = 0L;

For (int i = 0; i

{

ArrayList arraylist = new arraylist ();

For (int J = 0; j

ArrayList.Add (Integer.Valueof (m_testint [j]));

Itelist.iterator (); item.hasnext ();)

{

Object obj = item.next ();

L1 = (Integer) .intValue ();

}

}

Long l2 = system.currenttimemillis () - L;

System.out.println (NEW STRINGBUILDER ()). Append ("Using Regular ArrayList Takes Time:

") .Append (l2) .append (" Millseconds. Result: ") .append (l1) .tostring ());

}

Private void teststringa ()

{

Long L = system.currenttimemillis ();

String s = null;

For (int i = 0; i

{

LinkedList LinkedList = new linkedList ();

For (int J = 0; j

LinkedList.Add (m_teststring [j]);

Iterator itrator = LinkedList.ITerator (); item.hasnext ();)

{

String S1 = (string) item.next ();

s = s1.touppercase ();

}

}

Long l1 = system.currenttimemillis () - L;

System.out.println ((New StringBuilder ()). Append ("Using Generics LinkedList Takes Time:

") .append (l1) .append (" Millseconds. Result: ") .append (s) .tostring ());

}

Private void teststringb ()

{

Long L = system.currenttimemillis ();

String s = null;

For (int i = 0; i

{

ArrayList arraylist = new arraylist ();

For (int J = 0; j

ArrayList.Add (m_teststring [j]);

Itelist.iterator (); item.hasnext ();)

{

String S1 = (string) item.next ();

s = s1.touppercase ();

}

}

Long l1 = system.currenttimemillis () - l; system.out.println ((New StringBuilder ()). Append ("Using Generics Arraylist Takes Time:

") .append (l1) .append (" Millseconds. Result: ") .append (s) .tostring ());

}

Private void teststringc ()

{

Long L = system.currenttimemillis ();

String s = null;

For (int i = 0; i

{

ArrayList arraylist = new arraylist ();

For (int J = 0; j

ArrayList.Add (m_teststring [j]);

Itelist.iterator (); item.hasnext ();)

{

Object obj = item.next ();

s = (String) .touppercase ();

}

}

Long l1 = system.currenttimemillis () - L;

System.out.println (NEW STRINGBUILDER ()). Append ("Using Regular ArrayList Takes Time:

") .append (l1) .append (" Millseconds. Result: ") .append (s) .tostring ());

}

Reflexible .NET executable, we get this program.

Private void testinta ()

{

Long Num1 = Environment.tickcount;

Long Num2 = 0;

INT NUM3 = 0;

While ((Num3

{

LinkedList list1 = new linkedlist ();

INT NUM4 = 0;

While ((NUM4

{

List1.addtail ((int) this.m_testint [Num4]));

Num4;

}

LinkedList.Enumerator enumerator1 = list1.getenumerator ();

Try

{

While (enumerator1.movenext ())

{

INT Num5 = enumerator1.current;

Num2 = Num5;

}

}

Finally

{

ENUMERATOR1.DISPOSPOSE ();

}

Num3;

}

Long Num6 = (Environment.TickCount - Num1);

Object [] objarray1 = new object [4];

Objarray1 [0] = "Using generics linkedlist takes time:";

Objarray1 [1] = NUM6;

Objarray1 [2] = "MillSeconds. Result:"

Objarray1 [3] = Num2; console.writeline (String.concat (objarray1));

}

Private void testin ()

{

Long Num1 = Environment.tickcount;

Long Num2 = 0;

INT NUM3 = 0;

While ((Num3

{

List list1 = new list ();

INT NUM4 = 0;

While ((NUM4

{

List1.add ((int) this.m_testint [Num4]));

Num4;

}

List.enumerator enumerator1 = list1.geetenumerator ();

Try

{

While (enumerator1.movenext ())

{

INT Num5 = enumerator1.current;

Num2 = Num5;

}

}

Finally

{

ENUMERATOR1.DISPOSPOSE ();

}

Num3;

}

Long Num6 = (Environment.TickCount - Num1);

Object [] objarray1 = new object [4];

Objarray1 [0] = "Using Generics List Takes Time:";

Objarray1 [1] = NUM6;

Objarray1 [2] = "MillSeconds. Result:"

Objarray1 [3] = NUM2;

Console.writeline (String.concat (objarray1));

}

Private void testintc ()

{

Long Num1 = Environment.tickcount;

Long Num2 = 0;

INT NUM3 = 0;

While ((Num3

{

ArrayList List1 = new arraylist ();

INT NUM4 = 0;

While ((NUM4

{

List1.add (this.m_testint [num4]);

Num4;

}

Foreach (Object Obj1 in List1)

{

Num2 = convert.toint32 (obj1);

}

Num3;

}

Long Num5 = (Environment.TickCount - Num1);

Object [] objarray1 = new object [4];

Objarray1 [0] = "Using Regular ArrayList Takes Time:";

Objarray1 [1] = NUM5;

Objarray1 [2] = "MillSeconds. Result:"

Objarray1 [3] = NUM2;

Console.writeline (String.concat (objarray1));

}

Private void teststringa ()

{

Long Num1 = Environment.tickcount;

String text1 = NULL;

INT NUM2 = 0;

While (Num2

{

LinkedList List1 = new linkedList ();

INT NUM3 = 0;

While (Num3

{

List1.addtail ((String) this.m_teststring [Num3]));

Num3;

}

LinkedList.Enumerator enumerator1 = list1.geetenumerator ();

Try

{

While (enumerator1.movenext ())

{

String text2 = enumerator1.current;

TEXT1 = text2.toupper ();

}

}

Finally

{

ENUMERATOR1.DISPOSPOSE ();

}

Num2;

}

Long Num4 = (Environment.TickCount - Num1);

Object [] objarray1 = new object [4];

Objarray1 [0] = "Using generics linkedlist takes time:";

Objarray1 [1] = NUM4;

Objarray1 [2] = "MillSeconds. Result:"

Objarray1 [3] = Text1;

Console.writeline (String.concat (objarray1));

}

Private void teststringb ()

{

Long Num1 = Environment.tickcount;

String text1 = NULL;

INT NUM2 = 0;

While (Num2

{

List list1 = new list ();

INT NUM3 = 0;

While (Num3

{

List1.add (String) this.m_teststring [num3]));

Num3;

}

List.enumerator enumerator1 = list1.GeteNumerator ();

Try

{

While (enumerator1.movenext ())

{

String text2 = enumerator1.current;

TEXT1 = text2.toupper ();

}

}

Finally

{

ENUMERATOR1.DISPOSPOSE ();

}

Num2;

}

Long Num4 = (Environment.TickCount - Num1);

Object [] objarray1 = new object [4];

Objarray1 [0] = "Using Generics List Takes Time:";

Objarray1 [1] = NUM4;

Objarray1 [2] = "MillSeconds. Result:"

Objarray1 [3] = Text1;

Console.writeline (String.concat (objarray1));

}

Private void teststringc () {

Long Num1 = Environment.tickcount;

String text1 = NULL;

INT NUM2 = 0;

While (Num2

{

ArrayList List1 = new arraylist ();

INT NUM3 = 0;

While (Num3

{

List1.add (this.m_teststring [num 3]);

Num3;

}

FOREACH (String Text2 in List1)

{

TEXT1 = text2.toupper ();

}

Num2;

}

Long Num4 = (Environment.TickCount - Num1);

Object [] objarray1 = new object [4];

Objarray1 [0] = "Using Regular ArrayList Takes Time:";

Objarray1 [1] = NUM4;

Objarray1 [2] = "MillSeconds. Result:"

Objarray1 [3] = Text1;

Console.writeline (String.concat (objarray1));

}

What questions are these procedures reflect?

Although Java 1.5 supports generics, we do to feel the benefits such as type safety from Generics for developers. But for Java virtual machines, Bytecode is still bytecode without any changes. If it is an integer, the floating point number These basic data types (Primary Data Type), Java or first turn them into an instance of the corresponding class, an object of Integer, Double class. When using it, you must also convert from the Object type to the corresponding type. In other words, Java's generics is just a "obvious method". When compiling, the compiler will automatically add our previously handwritten packaging / solution and type conversion code. So for specific operational procedures, there is no change in programs that have not previously used generics. We did not see any performance improvement, the reason is here.

.NET 2.0 is a brand new generics design. We have got all the benefits of the generics commitment. According to Microsoft, for integers, Value Type, because Generics avoids this unsained overhead of packaging / unpacking operation, the performance will have 2 to 3 times; for String and other Reference Type, due to the avoidable type conversion, performance will be improved around 20%. Our tests have basically proven this.

Seeing this, you may not help but ask, the same generics, why sun and microsoft will have a distinct approach?

Sun's generics originated from a project called "pizza). The design principle of this project is to make the Java program after adding Gnerics can run on the Java Virtual Machine without any changes to Java virtual machines. Under this guiding ideology, Gnerics's work is actually on the compiler. Type security checks by the compiler when compiling. For programs that pass syntax check, the package / unpacking operation and the program code such as the type conversion operation are automatically added. This compiled Bytecode and the previously did not have any differences, Java virtual machines don't know when they do these bytecodes, and GENERICs have happened. The benefits of this approach are obvious, that is, simple, easy to implement, has good compatibility for previous Java virtual machines. But its shortcomings are also very awkward, that is, from the core sacrificed the essence of generics, we have not felt the performance of generics should have increased. The .NET's generics is a new design that fundamentally reflects the essence of generics. Syntax security is implemented in the compiler level. But the compiler does not do more work in addition to the static checks you should, do not do more work (this can be seen from our anti-compiled program). The specific work is done by the .NET's public language runtime (CLR, COMMON LANGUAGUAGUE RUNTIME). When you use it for the first time, this is a List.

The CLR will make the Just-IN-Time-Compiler dynamically generate such a local machine code for the INT class List. This code will be saved, and similar requests later (List

This can reuse this already generated class code. For performance considerations, all procedures declared and used Value Type will have their respective dynamic class. For example, the CLR will generate Double and INT two dedicated LIST classes for our routines. In order to reduce the prime expans and increase the code reuse, all reference classes (REFERENCE TYPE, such as the string we use) share a special class, that is, all of the reference classes have only a local Machine code. Because the essence of the reference class is a pointer, this commonality allows them to share the same code. However, these reference classes have separate VTABLE, which avoids the needs of type conversion.

.NET This design is brand new, we feel its power. But this new generics will not be compatible with the previous CLR. The previous CLR (ie .NET version 1.0 and version 1.1) cannot handle this generics, this is a problem that needs to be pointed out. But as I personally feel, sacrificing this compatibility is worth it.

The last thing to explain is that .NET 2.0 is much higher than Java 1.5, which is very unexpected. The very simple function used in the program includes a huge performance gap in the process of PREPAREDATA (). I remember when .NET 1.0beta, I contracted .NET and the performance of Java at the time. At that time, they were still very similar. Of course, it is necessary to comprehensively evaluate. Net 2.0 and Java 1.5 performance needs more comprehensive and more systematic tests, this purpose is not here, so there is not much judge.

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

New Post(0)