codecamp

关于性能

这里有一个性能测试网站

我对于网站测试的结果,我总结的情况就是两点.
1. 排在后面的基本都是动态类型语言,静态类型语言相对容易优化到性能差不多的结果.
2. 同一个语言代码写得好差产生的性能差异,远远比各种语言最好的代码性能差异大.

总的来说,程序员越自由,程序性能就越差

不过也有反例,我们之前那个程序就是.

//java版本
public static void main(String[] args) {
    List<String> paramsList = new LinkedList<String>() {{
        add(">= 3");
        add("< 7");
    }};
    JavaRangeMatcher matcher = new JavaRangeMatcher(paramsList);
    Random random = new Random();
    long timeBegin = System.currentTimeMillis();
    for (int i = 0; i < 100000000; i++) {
        int input = random.nextInt() % 10;
        matcher.check(input);
    }
    long timeEnd = System.currentTimeMillis();
    System.out.println("java 消耗时间: " + (timeEnd - timeBegin) + " 毫秒");
    //java 消耗时间: 3263 毫秒
}
//scala版本
def main(args: Array[String]) {
  val requirements = Seq(">= 3", "< 7")
  val rangeMatcher = RangeMatcher(requirements)
  val timeBegin = System.currentTimeMillis()
  0 until 100000000 foreach {
    case _ =>
      rangeMatcher.check(Random.nextInt(10))
  }
  val timeEnd = System.currentTimeMillis()
  println(s"scala 消耗时间 ${timeEnd - timeBegin} 毫秒")
  //scala 消耗时间 2617 毫秒
}

想想这是为什么?

函数是一等公民
推荐资源
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }