86
Этот
метод можно использовать только для измерения затраченного времени
на выполнение операций и никак не связан с системным временем
и текущим
мировым временем.
Возвращаемое значение представлено в виде наносекунд, с момента фиксации,
на любой произвольный момент времени. Например, чтобы определить, сколько
времени
занимает
некоторый
код
,
чтобы
выполнить
/*
* long startTime = System.nanoTime();
* // ... the code being measured ...
* long estimatedTime = System.nanoTime() - startTime;
*
*
* @return The current value of the system timer, in
nanoseconds.
* @since 1.5
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Reversing a long String via a String vs.
a StringBuffer
public class StringsBenchMark {
public static void main(String[] args) {
long beginTime, elapsedTime;
//
Build a long string
String str = "";
int size = 16536;
char ch = 'a';
beginTime = System.nanoTime();
// Reference time in nanoseconds
for (int count = 0; count < size; ++count) {
str += ch;
++ch;
if (ch > 'z') {
ch = 'a';
}
}
elapsedTime = System.nanoTime() - beginTime;
System.out.println("Elapsed Time is " + elapsedTime/1000 + " usec (Build String)");
// Reverse a String by building another String character-by-character
in the reverse order
String strReverse = "";
beginTime = System.nanoTime();
for (int pos = str.length() - 1; pos >= 0 ; pos--) {
87
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
strReverse += str.charAt(pos); // Concatenate
}
elapsedTime = System.nanoTime() - beginTime;
System.out.println("Elapsed Time is " + elapsedTime/1000 + " usec (Using String to reverse)");
// Reverse a String via an empty StringBuffer by appending characters in the reverse order
beginTime = System.nanoTime();
StringBuffer sBufferReverse = new StringBuffer(size);
for (int pos = str.length() - 1; pos >= 0 ; pos--) {
sBufferReverse.append(str.charAt(pos)); // append
}
elapsedTime = System.nanoTime() - beginTime;
System.out.println("Elapsed Time is " + elapsedTime/1000 + " usec (Using StringBuffer to reverse)");
// Reverse a String by creating a StringBuffer with the given String and invoke its reverse
()
beginTime = System.nanoTime();
StringBuffer sBufferReverseMethod = new StringBuffer(str);
sBufferReverseMethod.reverse(); // use reverse()
method
elapsedTime = System.nanoTime() - beginTime;
System.out.println("Elapsed Time is " + elapsedTime/1000 + " usec (Using StringBuffer's reverse() method)");
// Reverse a String via an empty StringBuilder by appending characters in the reverse
order
beginTime = System.nanoTime();
StringBuilder sBuilderReverse = new StringBuilder(size);
for (int pos = str.length() - 1; pos >= 0 ; pos--) {
sBuilderReverse.append(str.charAt(pos));
}
elapsedTime = System.nanoTime() - beginTime;
System.out.println("Elapsed Time is " + elapsedTime/1000 + " usec (Using StringBuilder to reverse)");
// Reverse a String by creating a StringBuilder with the given String and invoke its reverse
()
beginTime = System.nanoTime();
StringBuffer sBuilderReverseMethod = new StringBuffer(str);
sBuilderReverseMethod.reverse();
elapsedTime = System.nanoTime() - beginTime;
System.out.println("Elapsed Time is " + elapsedTime/1000 + " usec (Using StringBuidler's reverse() method)");
}
}
Elapsed Time is 332100 usec (Build String)
Elapsed Time is 346639 usec (Using String to reverse)
Elapsed Time is 2028 usec (Using StringBuffer to reverse)
Elapsed Time is 847 usec (Using StringBuffer's reverse()
method)
Elapsed Time is 1092 usec (Using StringBuilder to reverse)
88
Elapsed Time is 836 usec (Using StringBuidler's reverse()
method)
Обратите
внимание
,
что
StringBuilder
в
2
раза
быстрее
,
чем
StringBuffer,
и
в
300
раз
быстрее
,
чем
String.
Метод
reverse()
работает
быстрее
всего
,
и
занимает
примерно
одинаковое
время
как
для
StringBuilder,
так
и
для
StringBuffer.
Пример
выполнения
программы
:
Elapsed Time is 332100 usec (Build String)
Elapsed Time is 346639 usec (Using String to reverse)
Elapsed Time is 2028 usec (Using StringBuffer to reverse)
Elapsed Time is 847 usec (Using StringBuffer's reverse()
method)
Elapsed Time is 1092 usec (Using StringBuilder to reverse)
Elapsed Time is 836 usec (Using StringBuidler's reverse()
method)
Достарыңызбен бөлісу: