This test data is true, but only as a reference. You are welcome to discuss.
In addition to comparison, it is also available for php,jsp programmers to optimize their programs.
I. Arithmetic operations
A. Testing standards
Loop n times, and do integer self add operation.
B. Testing
Test code:
Test Type |
JSP int integral type |
JSP Long int |
JSP Long Integer 2 |
Php |
PHP2 |
Test code |
int i=0; while (i<XXXX) { i++; } |
long l=0; while (l<XXXX) { l++; } |
long l=0; while (l<XXXX) { l=l+1; } |
$i=0; while ($i<XXXX) { $i++; } |
$i=0; while ($i<XXXX) { $i=$i+1; } |
Test result (in milliseconds)
The test results show that M-N indicates that the main fluctuation range is between M and N; n indicates a very significant number or average of occurrences, and M (x,y) indicates that x,y occurs occasionally. The following are the same.
XXXX Take value |
JSP int integral type |
Jsplong Integral type |
Jsplong Integral type 2 |
Php |
PHP2 |
1000 |
0 |
0 |
0 |
0-1 |
0-1 |
10000 |
0 |
0 |
0 |
3-4 |
6-7 |
100000 |
0 |
0 |
0 (16,32) |
34-35 |
51-52 |
1000000 |
0 (10) |
0 (16,32) |
0 (16,32) |
348-368 |
527-534 |
10000000 |
0 (13) |
16-32 |
32-78 |
3547-3585 |
5239-5390 |
100000000 |
0 (16) |
266-281 |
265-281 |
35309-35486 |
1000000000 |
0 (16,743) |
2625-2676 |
2203-3687 |
C. Conclusion
Java arithmetic operation, optimization is very good, it is estimated that the direct corresponding CPU instructions, the number of large, arithmetic operation performance is very stable.
It should be noted that long has an operating time longer than the int (after all, a 32-bit system), so do not use long when it is not needed, but use int instead.
PHP is a weak type of variable, the arithmetic operation is very unsatisfactory, not directly corresponding to the calculation of machine code arithmetic.
PHP arithmetic operation of the process of guessing as follows, before the operation needs to detect whether the variable is a numeric value, and to convert the Non-numeric value, for example, PHP can do this operation: 100+ "Zhoutang", the result is equal to 100. It is because of the weak type language, coupled with the detection and conversion This leads to poor performance in arithmetic operations.
PHP's single operation time is relatively stable, about 10,000 times the arithmetic plus calculation of the time spent about 3.5MS
Different methods of operation also have an effect on performance.
Two. String operation
A. Testing standards
string concatenation operation contrast.
B. Testing
Test code:
Test Type |
Jsp |
Php |
Test code |
String str=""; while (str.length()< XXXX) { str+="a"; } |
$str=""; while (strlen($str)< XXXX) { $str.="a"; } |
Test result (in milliseconds)
XXXX Take value |
Jsp |
Php |
1000 |
0-16 |
1 |
10000 |
656-703 |
9-10 |
100000 |
105078-105235 |
95-103 |
C. Conclusion
Java strings are handled with objects, and it is clear that Java is far worse than PHP in the comparison of strings. (Because string operation, a lot of to do the connection, here only compares the concatenation operation, another rough comparison substring, the difference is not very big)
The string operation of PHP, estimated many are directly called C String functions, so the efficiency is much higher.
In the Web development, string concatenation operation is very frequent (including many times to generate SQL string), so in arithmetic operations and string operations two aspects, Java (JSP) advantage is not obvious, and PHP strengths.