NET must be faster than Java?

Source: Internet
Author: User
Document directory
  • Gettime
  • Gettime

I don't remember which article I 've read, but I said it was on Windows. net is faster than Java programs. but I never paid attention to it. I thought it was definitely like this. this is just a few days of research. net stringbuilder, when preparing to compare the following speed with Java's stringbuffer, suddenly found that there are more stringbuilder classes in jdk5.0. therefore, three of them are compared.
Considering the influence of JIT, the time difference of 10 times is obtained and then the average value is used for comparison.

 

Let's take a look at how the three of them are defined in their respective documents:

. Net stringbuilder

The string object cannot be changed. Every time you use a method in the system. string class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the string repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use System. Text. stringbuilder class. For example, when many strings are connected together in a loop, using the stringbuilder class can improve performance.

Java stringbuilder

A variable character sequence. This class providesStringBufferCompatible APIs, but cannot be synchronized. This class is designed for useStringBufferIs used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, it is betterStringBufferFast.

Java stringbuffer

A variable string of thread-safe characters. AStringBut cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods.

Java and. net use date and datetime to obtain the time difference, so it is necessary to mention the key methods of these two classes.

Java date. gettime ()


Gettime
public long getTime()
Returns the value since 00:00:00 GMT, January 1, January 1, 1970. DateThe number of milliseconds the object represents.

Return Value:
The number of milliseconds this date represents since 00:00:00 GMT, January 1, January 1, 1970.

. Net datetime. ticks

[C #]PublicLongTicks {Get ;}
 

Attribute Value

The number of scales of the date and time of the instance. The value is between minvalue and maxvalue.

Remarks

The value of this attribute is the number represented at an interval of January 1, 0001 milliseconds since midnight, January 1, 100.

Well, one response is millisecond, And the other response is subtle, so it is necessary to know the conversion between milliseconds and milliseconds.

1 second = 1000 milliseconds (MS) 1 millisecond = 1/1, 000 seconds (s)
1 second = 1,000,000 microseconds (μs) 1 microseconds = 1/1, 000,000 seconds (s)
1 second = 1,000,000,000 nanoseconds (NS)
1 nanosecond = 1/1, 000,000,000 seconds (s)
1 second = 1,000,000,000,000 second (PS)
The string object cannot be changed. Every time you use a method in the system. string class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the string repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use System. Text. stringbuilder class. For example, when many strings are connected together in a loop, using the stringbuilder class can improve performance.

Java stringbuilder

A variable character sequence. This class providesStringBufferCompatible APIs, but cannot be synchronized. This class is designed for useStringBufferIs used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, it is betterStringBufferFast.

Java stringbuffer

A variable string of thread-safe characters. AStringBut cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods.

Java and. net use date and datetime to obtain the time difference, so it is necessary to mention the key methods of these two classes.

Java date. gettime ()


Gettime
public long getTime()
Returns the value since 00:00:00 GMT, January 1, January 1, 1970. DateThe number of milliseconds the object represents.

Return Value:
The number of milliseconds this date represents since 00:00:00 GMT, January 1, January 1, 1970.

. Net datetime. ticks

[C #]PublicLongTicks {Get ;}
 

Attribute Value

The number of scales of the date and time of the instance. The value is between minvalue and maxvalue.

Remarks

The value of this attribute is the number represented at an interval of January 1, 0001 milliseconds since midnight, January 1, 100.

Well, one response is millisecond, And the other response is subtle, so it is necessary to know the conversion between milliseconds and milliseconds.

1 second = 1000 milliseconds (MS) 1 millisecond = 1/1, 000 seconds (s)
1 second = 1,000,000 microseconds (μs) 1 microseconds = 1/1, 000,000 seconds (s)
1 second = 1,000,000,000 nanoseconds (NS)
1 nanosecond = 1/1, 000,000,000 seconds (s)
1 second = 1,000,000,000,000 second (PS)

C # program: The returned time is milliseconds.

Using system;
Using system. text;
Class class1 {
Public static void main (string [] ARGs ){

Stringbuilder S = new stringbuilder ();
Double [] tosum = new double [10];
Datetime d1 = new datetime ();
Datetime D2 = new datetime ();
For (Int J = 0; j <= 9; j ++)
{
D1 = datetime. now;
For (INT I = 0; I <= 40000; I ++)
S. append ("APPEND ");
D2 = datetime. now;
Tosum [J] = d2.ticks-d1.ticks;
}

Double sum = 0;
For (Int J = 0; j <= 9; j ++)
{
Sum = sum + tosum [J];
}

Console. writeline ("Time consumed by stringbuilder:" + sum/100); // obtain the average value and conversion unit
}
}

Result:

 

Java program: returns millisecond

Import java. util .*;
Class estring {
Public static void main (Java. Lang. String [] ARGs ){


Stringbuilder S = new stringbuilder ();
Double [] tosum = new double [10];

For (Int J = 0; j <= 9; j ++ ){
Date d1 = new date ();
For (INT I = 0; I <= 40000; I ++)
S. append ("APPEND ");
Date D2 = new date ();
Tosum [J] = d2.gettime ()-d1.gettime ();
}

Double sum = 0;
For (Int J = 0; j <= 9; j ++ ){
Sum = sum + tosum [J];
}

System. Out. println ("stringbuilder time consumed:" + sum/10 );



Stringbuffer SB1 = new stringbuffer ();
For (Int J = 0; j <= 9; j ++ ){
Date test1 = new date ();
For (INT I = 0; I <= 40000; I ++)
Sb1.append ("APPEND ");
Date Test2 = new date ();
Tosum [J] = test2.gettime ()-test1.gettime ();
}

Sum = 0;
For (Int J = 0; j <= 9; j ++ ){
Sum = sum + tosum [J];
}

System. Out. println ("stringbuffer consumed time:" + sum/10 );
}
}

 

Result:

For such a result. I don't want to talk about anything ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.