Java, Python, Ruby, PHP, C等语言性能对比
2016-08-19 10:32
936 查看
代码功能:循环拼接字符串,并替换子字符串
**硬件环境:**Intel Core2 Duo T7500@2.20Ghz CPU; 2 GB RAM; OS Debian GNU/Linux 2.6.32 i686
代码性能对比图
内存对比图:
~~~.c
var imax = 1024 / str.length * 1024 * 4;
var time = new Date();
print(“exec.tm.sec\tstr.length”);
var gstr = “”;
var i=0;
var lngth;
while (i++ < imax+1000) {
gstr += str;
gstr = gstr.replace(/efgh/g, “__“);
lngth=str.length*i;
if ((lngth % (1024*256)) == 0) {
var curdate=new Date();
print(parseInt(((curdate.getTime()-time.getTime())/1000))+”sec\t\t”+lngth/1024+”kb”);
}
}
~~~
Java (source) Results: Java (OpenJDK) “1.6.0 18”,
Java (Sun) “1.6.0 16”,
Java (gcj) (Debian 4.4.3-1) 4.4.3
~~~.java
public class java_test {
}
~~~
Perl5 (source); Result: This is perl, v5.10.1
~~~.perl
my str=′abcdefgh′.′efghefgh′;myimax=1024/length($str)*1024*4; # 4mb
my $starttime=time();
print “exec.tm.sec\tstr.length\n”;
my gstr=”;myi=0;
while(i++<imax+1000){ #adding 1000 iterations to delay exit. This will allow to capture memory usage on last step
}
~~~
PHP (source); Result: PHP 5.3.1-5 with Suhosin-Patch (cgi-fcgi)
~~~.php
import time
import sys
str=’abcdefgh’+’efghefgh’
imax=1024/len(str)*1024*4 # 4mb
starttime=time.time();
print “exec.tm.sec\tstr.length”
sys.stdout.flush()
gstr=”
i=0
while (i < imax+1000):
i=i+1
gstr+=str
gstr=re.sub(‘efgh’,’__‘,gstr)
lngth=len(str)*i
if(lngth % (1024*256) == 0):
print int(time.time()-starttime),”sec\t\t”,(lngth/1024),”kb”
sys.stdout.flush()
~~~
Ruby (source); Result: ruby 1.8.7
~~~.ruby
str=’abcdefgh’+’efghefgh’;
imax=1024/str.length*1024*4; # 4mb
starttime=Time.new;
print(“exec.tm.sec\tstr.length\n”);
gstr=”;
i=0;
while i < imax+1000
i=i+1;
gstr+=str;
gstr=gstr.gsub(/efgh/, “__“)
end
Lua (source); Result: Lua 5.1.4
~~~.lua
str=’abcdefgh’..’efghefgh’;
imax=1024/string.len(str)*1024*4; – 4mb
starttime=os.time();
print “exec.tm.sec\tstr.length”;
gstr=”;
i=0;
while i < imax+1000 do
i=i+1;
gstr=gstr..str;
gstr=string.gsub(gstr,”efgh”,”__“);
lngth=string.len(str)*i;
if(math.mod(lngth,1024*256)==0) then
print(os.time()-starttime..”sec\t\t”..(lngth/1024)..”kb”);
end
end
~~~
tcl (source); Result: tcl 8.4.19
~~~.tcl
append str “efghefgh”
set imax [expr {1024/[string length $str]*1024*4}]
set starttime [clock clicks -milliseconds]
puts “exec.tm.sec\tstr.length”;
set gstr “”
set i 0
while {i<[expr {imax+1000}]} {
incr i
append gstr str;regsub−allefghgstr __ gstr
set lngth [expr {[string length str]∗i}]
if {[expr {lngthputs“[exprint([expr[clockclicks−milliseconds]−starttime] / 1000)]sec\t\t[expr {$lngth/1024}]kb”
}
}
exit
~~~
选语言就像选恋人,情人眼里出西施,你中意的,就是最好的,所以对以上结果别太较真。
转自:http://www.yinqisen.cn/blog-685.html
**硬件环境:**Intel Core2 Duo T7500@2.20Ghz CPU; 2 GB RAM; OS Debian GNU/Linux 2.6.32 i686
代码执行时长
代码性能对比图
最慢: Java gcj (native executable) 较慢: Java (openJDK); Java (Sun); Lua 稍慢: tcl; Javascript (spidermonkey) 较快: Python; Ruby; PHP; C++; Javascript V8; C; Perl5 最快的是Perl,最慢的gcj,你能接受吗?
内存使用情况
内存对比图:
测试源码:
C (source); Result: C gcc (Debian 4.4.4-1) 4.4.4~~~.c
include
include
include
include
include
include
include
!/usr/local/bin/js
var str = “abcdefgh”+”efghefgh”;var imax = 1024 / str.length * 1024 * 4;
var time = new Date();
print(“exec.tm.sec\tstr.length”);
var gstr = “”;
var i=0;
var lngth;
while (i++ < imax+1000) {
gstr += str;
gstr = gstr.replace(/efgh/g, “__“);
lngth=str.length*i;
if ((lngth % (1024*256)) == 0) {
var curdate=new Date();
print(parseInt(((curdate.getTime()-time.getTime())/1000))+”sec\t\t”+lngth/1024+”kb”);
}
}
~~~
Java (source) Results: Java (OpenJDK) “1.6.0 18”,
Java (Sun) “1.6.0 16”,
Java (gcj) (Debian 4.4.3-1) 4.4.3
~~~.java
public class java_test {
public static final void main(String[] args) throws Exception { String str = "abcdefgh"+"efghefgh"; int imax = 1024 / str.length() * 1024 * 4; long time = System.currentTimeMillis(); System.out.println("exec.tm.sec\tstr.length\tallocated memory:free memory:memory used"); Runtime runtime = Runtime.getRuntime(); System.out.println("0\t\t0\t\t"+runtime.totalMemory()/1024 +":"+ runtime.freeMemory()/1024+":"+(runtime.totalMemory()-runtime.freeMemory())/1024); String gstr = ""; int i=0; int lngth; while (i++ < imax+1000) { gstr += str; gstr = gstr.replaceAll("efgh", "____"); lngth=str.length()*i; if ((lngth % (1024*256)) == 0) { System.out.println(((System.currentTimeMillis()-time)/1000)+"sec\t\t"+lngth/1024+"kb\t\t"+runtime.totalMemory()/1024+":"+runtime.freeMemory()/1024+":"+(runtime.totalMemory()-runtime.freeMemory())/1024); } } }
}
~~~
Perl5 (source); Result: This is perl, v5.10.1
~~~.perl
!/usr/bin/perl
$|=1; #disable output buffering, this is necessary for proper output through pipemy str=′abcdefgh′.′efghefgh′;myimax=1024/length($str)*1024*4; # 4mb
my $starttime=time();
print “exec.tm.sec\tstr.length\n”;
my gstr=”;myi=0;
while(i++<imax+1000){ #adding 1000 iterations to delay exit. This will allow to capture memory usage on last step
$gstr.=$str; $gstr=~s/efgh/____/g; my $lngth=length($str)*$i; ## my $lngth=length($gstr); # Perhaps that would be a slower way print time()-$starttime,"sec\t\t",$lngth/1024,"kb\n" unless $lngth % (1024*256); #print out every 256kb
}
~~~
PHP (source); Result: PHP 5.3.1-5 with Suhosin-Patch (cgi-fcgi)
~~~.php
!/usr/bin/python3 -u
import reimport time
import sys
str=’abcdefgh’+’efghefgh’
imax=1024/len(str)*1024*4 # 4mb
starttime=time.time();
print “exec.tm.sec\tstr.length”
sys.stdout.flush()
gstr=”
i=0
while (i < imax+1000):
i=i+1
gstr+=str
gstr=re.sub(‘efgh’,’__‘,gstr)
lngth=len(str)*i
if(lngth % (1024*256) == 0):
print int(time.time()-starttime),”sec\t\t”,(lngth/1024),”kb”
sys.stdout.flush()
~~~
Ruby (source); Result: ruby 1.8.7
~~~.ruby
!/usr/bin/ruby
$stdout.sync=true;str=’abcdefgh’+’efghefgh’;
imax=1024/str.length*1024*4; # 4mb
starttime=Time.new;
print(“exec.tm.sec\tstr.length\n”);
gstr=”;
i=0;
while i < imax+1000
i=i+1;
gstr+=str;
gstr=gstr.gsub(/efgh/, “__“)
lngth=str.length*i; if(lngth % (1024*256)==0) print(((Time.new-starttime).ceil).to_s+"sec\t\t",(lngth/1024).to_s,"kb\n"); end
end
puts gstr;
~~~Lua (source); Result: Lua 5.1.4
~~~.lua
!/usr/bin/lua
io.stdout:setvbuf “no”; – io.flush();str=’abcdefgh’..’efghefgh’;
imax=1024/string.len(str)*1024*4; – 4mb
starttime=os.time();
print “exec.tm.sec\tstr.length”;
gstr=”;
i=0;
while i < imax+1000 do
i=i+1;
gstr=gstr..str;
gstr=string.gsub(gstr,”efgh”,”__“);
lngth=string.len(str)*i;
if(math.mod(lngth,1024*256)==0) then
print(os.time()-starttime..”sec\t\t”..(lngth/1024)..”kb”);
end
end
~~~
tcl (source); Result: tcl 8.4.19
~~~.tcl
!/usr/bin/tclsh
set str “abcdefgh”append str “efghefgh”
set imax [expr {1024/[string length $str]*1024*4}]
set starttime [clock clicks -milliseconds]
puts “exec.tm.sec\tstr.length”;
set gstr “”
set i 0
while {i<[expr {imax+1000}]} {
incr i
append gstr str;regsub−allefghgstr __ gstr
set lngth [expr {[string length str]∗i}]
if {[expr {lngthputs“[exprint([expr[clockclicks−milliseconds]−starttime] / 1000)]sec\t\t[expr {$lngth/1024}]kb”
}
}
exit
~~~
结论
各语言随着版本升级,也在不断优化,想要用好每个语言的特性,需要摸透她的脾气,这个需要不断的加深了解。选语言就像选恋人,情人眼里出西施,你中意的,就是最好的,所以对以上结果别太较真。
转自:http://www.yinqisen.cn/blog-685.html
相关文章推荐
- 用数据阐述Java, Python, Ruby, PHP, C等各语言性能对比
- Java, Python, Ruby, PHP, C等语言性能对比
- 脚本语言性能比较:Ruby,Io,PHP,Python,Lua,Java,Perl...
- 脚本语言性能比较:Ruby,Io,PHP,Python,Lua,Java,Perl...
- 脚本语言性能比较:Ruby,Io,PHP,Python,Lua,Java,Perl...
- C,Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript性能比较
- 2017年的golang、python、php、c++、c、java、Nodejs性能对比
- JAVA、PHP、RUBY、PYTHON、HTML等语言视频教程合集,很全面,值得收藏。
- C,Ruby, Io, PHP, Python, Lua, Java, Perl, Applescript, TCL, ELisp, Javascript, OCaml, Ghostscript性能比较
- 【转】语言简评:比较Perl、PHP、Python、Java和Ruby
- 在线编程语言模拟(Java,C,Python,R语言,Ruby,PHP,Perl,Go等)
- golang、python、php、c++、c、java、Nodejs性能对比
- 2017年的golang、python、php、c++、c、java、Nodejs性能对比(golang python php c++ java Nodejs Performance)
- C、C++、Java、JavaScript、PHP、Python、Ruby 这些语言分别主要用来开发什么?
- 常用语言对比,真是好东西。JavaScript, PHP, Python, Ruby
- 强弱类型,动态静态语言比较(JAVA,C,C++,Python,Ruby,PHP,Perl)
- 后台开发语言的比较 java php python asp.net ruby on rails
- 在线编程语言模拟(Java,C,Python,R语言,Ruby,PHP,Perl,Go等)
- 2017年的golang、python、php、c++、c、java、Nodejs性能对比[续]
- JAVA、PHP、RUBY、PYTHON、HTML等语言视频教程合集,很全面,值得收藏。