wangyan188 发表于 2018-12-16 07:15:14

Java, Python, Ruby, PHP, C等语言性能对比

代码功能:循环拼接字符串,并替换子字符串  
硬件环境: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
  
#include #include #include #include int main(){setbuf(stdout,NULL); //disable output bufferingchar *str=malloc(8);strcpy(str,"abcdefgh");str=realloc(str,strlen(str)+8);strcat(str,"efghefgh");   //sprintf(str,"%s%s",str,"efghefgh");int imax=1024/strlen(str)*1024*4;printf("%s","exec.tm.sec\tstr.length\n"); //fflush(stdout);time_t starttime=time(NULL);char *gstr=malloc(0);int i=0;char *pos;int lngth;char *pos_c=gstr;int str_len=strlen(str);
  

  
    while(i++ < imax+1000){
  
      lngth=strlen(str)*i;
  
      gstr=realloc(gstr,lngth+str_len);
  
      strcat(gstr,str);    //sprintf(gstr,"%s%s",gstr,str);
  
      pos_c+=str_len;
  

  
      pos=gstr;
  
      while(pos=strstr(pos,"efgh")){
  
            memcpy(pos,"____",4);
  
      }
  

  
      if(lngth % (1024*256)==0){
  
            printf("%dsec\t\t%dkb\n",time(NULL)-starttime,lngth/1024); //fflush(stdout);
  
      }
  
    }//printf("%s\n",gstr);}
  
C++ (source) Result: C++ g++ (Debian 4.4.3-7) 4.4.3
  
#include #include #include using namespace std;main (){
  
string str = "abcdefgh";
  
    str += "efghefgh";
  
int imax = 1024 /str.length() * 1024 *4;
  
time_t currentTime = time(NULL);
  
cout
页: [1]
查看完整版本: Java, Python, Ruby, PHP, C等语言性能对比