Yesterday's article titled writing with JScript.net. after writing the article ". net application", I have some doubts about its running efficiency. Therefore, we need to perform the following tests. Of course, our predecessors have done many tests, but the bigtall test methods are somewhat different. Here I use two Fibonacci algorithms: Recursive Implementation and iterative implementation. The reasons for adopting Fibonacci are as follows:
- It is an O (2n) algorithm with a large enough computing workload.
- For the same computing, the main difference between Recursion and iteration is stack processing. We can also compare the efficiency differences between calling functions in different languages.
- The code is simple and the algorithm is easy to understand. The differences between different test codes are also small, and it is not easy to dispute.
The following conditions are taken into account during testing:
- The first system load is from the disk, and the last few times is directly from the disk cache. Each test runs for four consecutive times. The first time is not counted.
- Because the implementation efficiency of the IO library is different, there is no IO call in the algorithm code, pure computing code.
The languages involved in the comparison include c, c #, standard javascript, and JScript.net. Later I felt that I was not addicted to Java 6. In addition, bigtall writes a computation time Applet and batch processing. A total of 12 pieces of code are represented as follows:
Fibc. c |
Fib2c. c |
Long Fib (long n) { If (n <= 1 ){ Return n; } Else { Return Fib (n-1) + Fib (n-2 ); } } Void main () { Int I; For (I = 0; I <10; I ++) Fib (30 ); } |
Long Fib (long n) { Int I; Long a = 0, B = 1, c = 0; If (n <= 1 ){ Return n; } Else { For (I = 2; I <= n; I ++ ){ C = a + B; A = B; B = c; } Return c; } } Void main () { Int I; For (I = 0; I <26925370; I ++) Fib (30 ); } |
Fibcs. cs |
Fib2cs. cs |
Public class { Static long Fib (long n) { If (n <= 1 ){ Return n; } Else { Return Fib (n-1) + Fib (n-2 ); } } Public static void Main () { For (int I = 0; I <10; I ++) Fib (30 ); } } |
Public class { Static long Fib (long n) { Int I; Long a = 0, B = 1, c = 0; If (n <= 1 ){ Return n; } Else { For (I = 2; I <= n; I ++ ){ C = a + B; A = B; B = c; } Return c; } } Public static void Main () { For (int I = 0; I <26925370; I ++) Fib (30 ); } } |
Fibjava. java |
Fib2java. java |
Public class fibjava { Static long Fib (long n) { If (n <= 1 ){ Return n; } Else { Return Fib (n-1) + Fib (n-2 ); } } Public static void main (String [] args) { For (int I = 0; I <10; I ++) Fib (30 ); } } |
Public class fib2java { Static long Fib (long n) { Int I; Long a = 0, B = 1, c = 0; If (n <= 1 ){ Return n; } Else { For (I = 2; I <= n; I ++ ){ C = a + B; A = B; B = c; } Return c; } } Public static void main (String [] args) { For (int I = 0; I <26925370; I ++) Fib (30 ); } } |
Fibjs1.js |
Fib2js1. js |
Function Fib (n) { If (n <= 1 ){ Return n; } Else { Return Fib (n-1) + Fib (n-2 ); } } For (var I: int = 0; I <10; I ++) Fib (30 ); |
Function Fib (n) { Var I; Var a = 0, B = 1, c = 0; If (n <= 1 ){ Return n; } Else { For (I = 2; I <= n; I ++ ){ C = a + B; A = B; B = c; } Return c; } } For (var I: int = 0; I <26925370; I ++) Fib (30 ); |
Fibjs2.js |
Fib2js2. js |
Function Fib (n: int): int { If (n <= 1 ){ Return n; } Else { Return Fib (n-1) + Fib (n-2 ); } } For (var I: int = 0; I <10; I ++) Fib (30 ); |
Function Fib (n: int): int { Var I: int; Var a: int = 0, B: int = 1, c: int = 0; If (n <= 1 ){ Return n; } Else { For (I = 2; I <= n; I ++ ){ C = a + B; A = B; B = c; } Return c; } } For (var I: int = 0; I <26925370; I ++) Fib (30 ); |
Ptime. cs |
Cp. bat |
Public class { Public static void Main () { System. Console. Write (System. DateTime. Now. Ticks ); System. Console. Write (','); } } |
@ Echo off Cl/O2 fiber c. c Cl/O2 fiber 2C. c Csc/o +/debug-fiber cs. cs Csc/o +/debug-fib2cs. cs Jsc/fast-/debug-fiber js1.js Jsc/fast-/debug-Fig. js Jsc/fast +/debug-Fig Jsc/fast +/debug-Fig. js "% JAVA_HOME % \ bin \ javac"-g: none maid. java "% JAVA_HOME % \ bin \ javac"-g: none maid. java Echo compiled Call: run fibc Call: run fib2c Call: run fibcs Call: run fib2cs Call: run fig Call: run fig Call: run fig Call: run fig Call: run "% JAVA_HOME % \ bin \ java" fibjava Call: run "% JAVA_HOME % \ bin \ java" fib2java Echo finish! Goto end : Run Echo ======================================== Ptime & % 1% 2 & ptime & echo % 1% 2 1 Ptime & % 1% 2 & ptime & echo % 1% 2 2 Ptime & % 1% 2 & ptime & echo % 1% 2 3 Ptime & % 1% 2 & ptime & echo % 1% 2 4 : End |
The test results are shown in the following table. The four blue groups of data in the table are respectively 1, 2, 3, and 4, and the black data is the average of the test results of the last three groups, the green data shows the ratio of time consumed by the C language. The Red Vertical ratio of the last line is the ratio of time consumed by recursive and iterative algorithms in the same language. Time unit: 1% seconds:
|
C |
C # |
Java |
Js |
JScript.net |
Recursion (10 times) |
119 49 48 49 |
486 439 461 441 |
2258 520 467 464 |
75397 75327 76424 74228 |
1571 1501 1502 1499 |
48.67 |
447 |
483.67 |
75326.33 |
1500.67 |
1 |
9.18 |
9.94 |
1547.70 |
30.83 |
Iteration (26,925,370 times) |
120 49 47 46 |
5261 5041 5040 5039 |
7880 7769 7762 7766 |
125786 127117 127273 127541 |
9196 9101 9086 9121 |
47.33 |
5040 |
7765.67 |
127310.33 |
9102.67 |
1 |
106.48 |
164.06 |
2689.65 |
192.31 |
Vertical Ratio |
0.97 |
11.28 |
16.06 |
1.69 |
6.07 |
From this, we can see that if the horizontal comparison is based on the C language running speed, the speed of C # and java is nearly 10 times slower during recursive operations, JScript.net is nearly 31 times slower. js is 1500 times slower because it uses runtime binding. Once function calls are eliminated, the running time of Iteration Algorithms Using pure computing code is much different in different languages, and C # code is obviously faster than java (the difference in loading the basic class library is not considered here, because the M $ pair. net pre-installed), the worst is still javascript, but it seems that the later binding without calling seems faster. However, it is surprising that the compilation Optimization of JScript.net is good, and the speed is very fast.
Before vertical comparison, we need to analyze the algorithm. Through simple code, we know that the number of recursive calls of fib (30) is 2692537, and the number of 10 repeat is 26925370. This is the difference between Recursion and Iteration Algorithms, But we set the number of iterations to 26925370 to eliminate the difference in function calls and highlight the linear running difference of code. Through code analysis, we can obtain the statistical table of code features:
|
Recursion |
Iteration |
Assignment Statement |
3 |
120 |
Variable Allocation |
2 |
4 |
Function call |
2 |
0 |
Return |
2 |
2 |
Condition judgment |
1 |
30 |
Jump |
1 |
30 |
Total |
11 |
186 |
Compared with recursive algorithms, iterative algorithms have a large amount of code, and their code size is about 186/11 = 16.9 times of recursion. However, in addition to java, this ratio is smaller than the runtime. C language is even shorter. If the test error is not considered, the only reasonable explanation should be the code optimization problem, because the compiler and CPU have the ability to optimize the code, but obviously no matter which optimization, it cannot be optimized across function calls. C # is faster than java. Does it mean the C # optimizer is better than java? However, the two ratio values of JS Code are a little difficult for me, but they are not uninterpretable, because there are too many optimizations In the JavaScript code.
Conclusion:
- C, as an old medium-and high-level language, has no advantage.
- Recursion is rarely used, especially JavaScript, and function calls are compressed as much as possible.
- The main purpose of this article is that if JScript.net is used as a general application, the efficiency should be acceptable, but do not perform numerical calculations.
- I have read any articles about java or C # running efficiency that can reach 70% of the C language. Now it seems that there is water. If the compiler efficiency is compared, I think the gap is still obvious. It seems that the gunman's article should be vigilant!
In addition, I would like to give you a small request. If anyone is familiar with python, ruby, perl, and so on, how can I perform a test using the same algorithm?
The algorithm in this article refers to the discussion on the solution of the series of the Fibonacci (Fibonacci.
==========================================================
At, 26925370, the number of iterations was modified to, and the test time and conclusions were corrected. Thank you very much for reminding me to assemble your head. Thank you! Sorry for the previous misdirection!