blob: 20fdd848ef4e954ee348a26cb4851bab0dbe74c4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/**
*
*/
package de.fhswf.in.inf.java1.aufgabe4;
import java.util.Arrays;
/**
* Tests the different methods to created a prefilled String.
*
* @author $Author: $
* @version $Revision: $, $Date: $ UTC
*/
public final class Fill
{
private Fill()
{
}
/**
*
* Creates a string of given length filled with the given char.
*
* @param length
* Length of the returned String.
* @param c
* Char the String is filled with.
* @return Returns the generated String.
*/
public static String createStringFilledWith(int length, char c)
{
// String that will be returned
String result = "";
for (int i = 0; i < length; i++)
{
// Every iteration, a new string is created.
result += c;
}
// Return the build string
return result;
}
/**
*
* Creates a string of given length filled with the given char.
*
* @param length
* Length of the returned String.
* @param c
* Char the String is filled with.
* @return Returns the generated String.
*/
public static String createStringFilledWith2(int length, char c)
{
// charArray that will be returned
char[] result = new char[length];
Arrays.fill(result, c);
// Return the build string
return result.toString();
}
/**
*
* Creates a string of given length filled with the given char.
*
* @param length
* Length of the returned String.
* @param c
* Char the String is filled with.
* @return Returns the generated String.
*/
public static String createStringFilledWith3(int length, char c)
{
// String that will be returned
StringBuilder result = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
// Every iteration, a new string is created.
result.append(c);
}
// Return the build string
return result.toString();
}
/**
*
* Calls the function createStringFilledWith() and measures its runtime.
*
* @param args
* Console line arguments.
*/
public static void main(String[] args)
{
final int anzDurchlaefe = 100000000;
long start;
start = System.currentTimeMillis(); // Gets current time in �s
Fill.createStringFilledWith(anzDurchlaefe, 'x'); // Function call
// Calculates the time runtime of creatStringFilledWith()
System.out.println("Elapsed time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis(); // Gets current time in �s
Fill.createStringFilledWith2(anzDurchlaefe, 'x'); // Function call
// Calculates the time runtime of creatStringFilledWith2()
System.out.println("Elapsed time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis(); // Gets current time in �s
Fill.createStringFilledWith3(anzDurchlaefe, 'x'); // Function call
// Calculates the time runtime of creatStringFilledWith3()
System.out.println("Elapsed time: "
+ (System.currentTimeMillis() - start) + " ms");
}
}
|