forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
46 lines (35 loc) · 1.68 KB
/
Main.java
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
import java.lang.reflect.Method;
import java.lang.Class;
// 比较这个工程中 Solution1, Solution2, Solution3, Solution4 和 Solution5 的算法运行效率
public class Main {
public static void testPerformace(String algoClassName, String algoName, String s){
try{
Class algoClass = Class.forName(algoClassName);
Object solution = algoClass.newInstance();
// 通过排序函数的Class对象获得排序方法
Method algoMethod = algoClass.getMethod(algoName, String.class);
long startTime = System.currentTimeMillis();
// 调用算法
Object resObj = algoMethod.invoke(solution, s);
long endTime = System.currentTimeMillis();
int res = (Integer)resObj;
System.out.print(algoClassName + " : res = " + res + " ");
System.out.println("Time = " + (endTime-startTime) + " ms" );
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
int n = 10000000;
StringBuilder s = new StringBuilder(n);
for(int i = 0 ; i < n ; i ++)
s.append((char)(Math.random()*95 + 32));
System.out.println("Test: 10,000,000 length of completely random string:");
testPerformace("Solution1", "lengthOfLongestSubstring", s.toString());
testPerformace("Solution2", "lengthOfLongestSubstring", s.toString());
testPerformace("Solution3", "lengthOfLongestSubstring", s.toString());
testPerformace("Solution4", "lengthOfLongestSubstring", s.toString());
testPerformace("Solution5", "lengthOfLongestSubstring", s.toString());
}
}