单线程环境下Java/PHP/Python性能测试(循环输出)

来源:互联网 发布:21天学通java 编辑:程序博客网 时间:2024/05/19 07:10

Java代码:

public class Test {public static void main(String[] args) {int count = 100000;if(args != null && args.length > 0) {count = Integer.parseInt(args[0]);}long start = System.currentTimeMillis();for (int i = 0; i < count; i++) {System.out.println("Hello World " + i);}System.out.println("Total: " + (System.currentTimeMillis() - start));}}

PHP代码:

<?php$count = 100000;if(is_array($argv) && count($argv) > 1) {$count = intval($argv[1]);}$start = microtime_float();for($i=0; $i<$count; $i++) {echo "Hello World $i\n";}echo "Total: " + intval((microtime_float()-$start)*1000);function microtime_float(){    list($usec, $sec) = explode(" ", microtime());    return ((float)$usec + (int)$sec);}?>
JDK版本:1.6.32

PHP版本:5.3.10

测试结果:

Linux环境下PHP绝对优势,Window下PHP仍然略微占优。总体感觉还是PHP更快一些。

当然这只是测试了两种语言在循环输出的性能,在企业级应用中还需要考虑很多其他的因数。


再试一下Python:

import timestr_raw = raw_input("Loop(default = 100000):")if(str_raw == ''):  str_raw = 100000count = int(str_raw)starttime = time.clock() for i in range(1, count):  print "Hello Word", ielse:  print "End"print round(time.clock() - starttime, 3) * 1000

Window下测试的结果不是很理想,速度要慢很多。


更多资料:

http://www.keakon.net/2009/12/07/Java%E3%80%81PHP%E3%80%81Python%E4%B8%8EMySQL%E4%BA%A4%E4%BA%92%E7%9A%84%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95


原创粉丝点击