发新话题
打印

三大编程语言性能PK:Java, C/C++和Ruby

三大编程语言性能PK:Java, C/C++和Ruby

 你可能会觉得下面的图表比较有意思,因为它是分别用三种编程语言(Ruby, Java, C/C++)写的埃拉托色尼质数过滤算法(译注:Sieve of Eratosthenes)的性能分析图,如图:(本文的最后附有相应代码)

  好,很明显Ruby是慢的,而且慢了大概有1.5个数量级(译注:即约30倍)。这对于Ruby爱好者来说可不是个好消息。不过换个角度看,呼!Ruby与五、六年前的顶级电脑一样快。还记得第一次在时钟周期不到一兆赫的机器上跑程序的神奇情景吗...我们还为此兴奋得直往山顶跑!

  注意一下,这三条曲线是同样形状的,我们可以从上篇blog中了解到为何曲线会呈线性。最后要关注的是,Java的曲线以极其微小的优势快于C++。你可以抱怨那是因为没有用gcc编译器优化编译的缘故(我用的是cygwin(译注:gcc编译器移植到windows的版本)),可是,如果现在还有任何C++程序员还会嘲笑Java的性能的话,我劝你最好还是再重新掂量掂量吧。

  而对于那些因为他们自己的开发环境比Ruby快上30倍而洋洋自得的Java程序员来说,我肯定更优的ruby实时编译器即将问世了。不管如何,相比那快上1.5个数量级的情形来说,我本人还是更喜欢干净、简洁、易维护的代码。

  Ruby

require 'benchmark'
def sievePerformance(n)
r = Benchmark.realtime() do
sieve = Array.new(n,true)
sieve[0..1] = [false,false]

2.upto(Integer(Math.sqrt(n)) do |i|
if sieve
(2*i).step(n,i) do |j|
sieve[j] = false
end
end
end
end
r
end

  Java

public class GeneratePrimes {
 public static double generate(int max) {
  long start = System.currentTimeMillis();
  boolean sieve[] = new boolean[max];
  Arrays.fill(sieve, true);
  sieve[0] = false;
  sieve[1] = false;
  for (int i = 2; i < Math.sqrt(max); i++) {
   if (sieve) {
    for (int j = 2*i; j < sieve.length; j+=i) {
     sieve[j]= false;
    }
   }
  }
  return (System.currentTimeMillis() - start)/1000.0;
}

  C++

#include <iostream>>
#include <math.h>
#include <sys/time.h>

using namespace std;

double generate(int max) {
 struct timeval start;
 struct timezone tz;
 gettimeofday(&start, &tz);

 bool *sieve = new bool[max];
 for (int i=0; i<max; i++) sieve = true;
 sieve[0] = false;
 sieve[1] = false;
 for (int n=2; n<sqrt(max); n++) {
  if (sieve[n]) {
   for (int j=2*n; j<max; j+=n)
    sieve[j] = false;
  }
 }

 struct timeval end;
 gettimeofday(&end, &tz);

 double startSecond = start.tv_usec/1000000.0;
 double endSecond = (end.tv_sec - start.tv_sec) + end.tv_usec/1000000.0;
 return endSecond - startSecond;
}

int main(int ac, char** av) {
 for (int i=100000; i<=5000000; i+=100000) {
  double time = generate(i);
  cout << time << endl;
 }
}

这世界总有你不明白

TOP

担心

TOP

差距不是太大吧,毕竟不如java、c/c++等成熟啊
还是看好ruby

TOP

dofus 3

so do  you sell dofus kamas,if you have cheap dofus kamas , please connect me as soon as possible!
hihi^^
i want to buy more and more , more and more!
so please take your time !
do you have dofus money, dofus gold ? i want them , i love them !
i have change a new envionment , it's new ,so it's interesting and changllege to me!
in a world ,i love dofus kamas , cheap dofus kamas !

TOP

深圳搬家公司-深圳搬厂公司-这样发信息也能提升网站排名

深圳搬家公司-深圳搬厂公司-深圳顺发顺搬迁公司TeL:0755-21529158
深圳顺发顺搬家公司是一家经深圳工商局和深圳运输局批准成立的私营独资性有限公司,本深圳搬家公司成立较早,各种搬运设施先进齐全.公司为客户提供:深圳个人及家庭搬家,深圳公司搬迁,深圳写字楼搬迁,深圳企业搬迁策划,深圳厂房搬迁,深圳单位整体...
www.sfsok.com/ 22K 2008-4-25 - 百度快照
深圳市顺发顺搬迁公司是专业的深圳搬家公司

TOP

0579-87368579厂价供水步行球

15058657179嘉鹏健身器材有限公司是生产|丽妍堂|丽妍堂排汗机|丽妍堂真轻松排汗机|丽妍堂蒸汽机|丽妍堂真轻松蒸汽机|派宁蒸汽清洁机|派宁牌蒸汽清洁机|派宁蒸汽清洗机|派宁蒸汽清洗器|派宁蒸汽清洁机价格|水上步行球|水上步行球价格|水上步行器|水上悠波球|水上滚筒|水上滚筒价格|详情网站:www.zgjp2008.com=www.yph168.com 浙江嘉鹏健身器材有限公司地址:浙江永康市科技五金工业园。24小时订购电话:0579-87368579  QQ:976242168  893670131

TOP

are you come here see?i am waiting for you ^^

buy cheap dofus kamas
buy cheap cabal online alz
buy cheap knight online gold
buy 9dragons gold
buy cheap lotro online gold
ahha.so much cheap game here.,come on,and you will find yo are a good shopping here^^

TOP

流动推车式电动大拉丝棉花糖机

流动推车式电动大拉丝棉花糖机

08年最新款式,改变了传统大拉丝机器的笨重机身,更加小巧,方便携带、适合流动作业,却具有同等拉丝效果!高盆设计免除繁琐防风罩,操作过程可加糖并可无限拉长丝!最新离心偏轴物理技术设计,免除皮带或齿轮传动,目前最先进的独特传动方式,全国独有!
注意:本系列机机头和传动设计,仿制品没有防甩头设计,并无传动轴设计,更无弹簧防偏头设计!

  
优秀特点:
1.内含大容量电瓶设计,带充电器并自动充电功能,充电一夜可连续工作1-2天,机内含液化气钢瓶;
2.可调节机头转数,独特数字转数记忆,,最新安全机头设计,离心偏轴出糖器,防止出现偏头甩糖不均匀;无皮带式传动,绝不飞头!
3.节能斜口设计机头,不仅节省液化气而且不造成糖的浪费,,拉丝绝不断丝设计,可以在拉丝过程中不断加糖,直至做到最大的棉花糖;
4.电子打火设计,火力调节及火力记忆设计;
5.,可以在任何地点作业,行李外观设计,折叠把手,流动作业时可以随时回避城市管理人员!
规格:950X320X320mm
转动系统:照明电或内置电瓶  内置燃气加热系统 充电器 变压器 调速装置
原料:白砂糖 绵白糖以及冰糖(采用冰糖为原料是独特设计可制造独特蓬松效果)
彩色多口味棉花糖制作工艺随机赠送,色彩、口味不再单一!
单价:1720元(含独特精致2公斤液化气罐和12V电瓶及充电器装置,开机即可使用)    量大优惠
欢迎您的来电:024-82116562 QQ:491753951 http://www.086hh.com
沈阳宏海

TOP

机票代理加盟网站

机票机票北京特价机票千叶航空全国航空服务加盟网站。可以链接.如有需要请联系.qq.464713000公司
址:www.jipiaovip.com 希望能对版主的出行有帮助.谢谢!
人之初,性本善!北京机票

TOP

干燥设备 万佳干燥设备有限公司

常州万佳干燥设备有限公司专业从事干燥设备的研发与制造,常州万佳干燥设备有限公司是一家具有多年干燥设备制造经验的公司。
万佳人并着没有什么“干”不了的信念在为大家做出最好的干燥设备
如果你有需要干燥设备,那么我相信常州万佳干燥设备有限公司将是您最好的选择,常州万佳干燥设备有限公司也将成为你最好的合作伙伴。
专业从事网站优化,SEO,搜索引擎优化等专业网络技术

TOP

age of conan

There is much controversy today about whether television has destroyed communication among family and friends. Some people argue that the communication is baffled by television. Others criticize that television improve the relationship of family and friends. I totally agree with the latter one. <a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>cheap AOC gold</a>
Watching TV is a time for the whole family to stop from whatever they are doing to get together. While watch TV, they start to talk about the content of the movie, which is also a kind of communication. Imagining the TV disappears,<a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan account</a>
we would do reading in rooms, while our family would do laundry, do washing dish or going shopping. It simply provides a chance for us to stay together. <a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a>
In addition, the TV supplies us with much information, which we have not known in our daily lives. Some of the constructive movies show on TV, like a family story, a love story, or so on, reinforce the relationship of a family and friends.<a href=http://www.fast-wowgold.com/age-of-conan-gold.htm>age of conan power leveling</a> At last, it is a kind of recreation that a family or friends can choose to have. Communication can be undertaken by many means, for example, going shopping together, entering a bar, talking intimately about ourselves.

TOP

发新话题