论坛首页 编程语言技术论坛

Ruby 1.9 / Ruby 1.8 = ?

浏览 2945 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-01-03   最后修改:2009-01-03

从MSN上看到这个签名,耐不住性子简单测试下,结果确实挺令人兴奋。

 

下载Ruby 1.9.1 rc1
发布地址 :http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/323668

下载地址:ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.9.1-rc1.tar.bz2
SIZE: 6181532 bytes
MD5: d440c030131903e72a6152149a097af3
SHA256: 35acfb6b8d9dd9159ef308ac763c629092cda2e8c9f41254e72a7b9fa454c27f

 

安装
1.解压
2.进入目录
3.配置 configure ,防止和原有版本冲突,务必添加 "prefix=/custom_path"
4.make && make install
5.编辑 “.bashrc” 文件 : alias ruby9="custom_path/bin/ruby"

 

测试代码(暴力搜索求解soduku):

$count = 0

def valid?(state, x, y)
  # check in col and row
  0.upto(8) do |i|
    return false if i != y and state[x][i] == state[x][y]
    return false if i != x and state[i][y] == state[x][y]
  end

  # check in box
  x_from = (x / 3) * 3
  y_from = (y / 3) * 3
  x_from.upto(x_from + 2) do |xx|
    y_from.upto(y_from + 2) do |yy|
      return false if (xx != x or yy != y) and state[xx][yy] ==
state[x][y]
    end
  end

  true
end



def next_state(state, x, y)
  $count = $count + 1
  y = 0 and x = x + 1 if y == 9
  return true if x == 9

  unless state[x][y].zero?
    return false unless valid?(state, x, y)
    return next_state(state, x, y + 1)
  else
    1.upto(9) do |i|
    state[x][y] = i
      return true if valid?(state, x, y) and next_state(state, x, y + 1)
    end
  end

  state[x][y] = 0
  false
end


start =
[
  [ 0, 0, 0, 4, 0, 5, 0, 0, 1 ],
  [ 0, 7, 0, 0, 0, 0, 0, 3, 0 ],
  [ 0, 0, 4, 0, 0, 0, 9, 0, 0 ],
  [ 0, 0, 3, 5, 0, 4, 1, 0, 0 ],
  [ 0, 0, 7, 0, 0, 0, 4, 0, 0 ],
  [ 0, 0, 8, 9, 0, 1, 0, 0, 0 ],
  [ 0, 0, 9, 0, 0, 0, 6, 0, 0 ],
  [ 0, 8, 0, 0, 0, 0, 0, 2, 0 ],
  [ 4, 0, 0, 2, 0, 0, 0, 0, 0 ]
]

start_time = Time.new

if next_state(start, 0, 0)
  puts "time elapsed: #{Time.new - start_time} sec."
  puts "count: #{$count}"
  start.each do |val|
    puts val.join(" ")
  end
else
  puts "Not solveable!"
end

 

CPP代码:

/* 
 * File:   newmain.cpp
 * Author: xhan
 *
 * Created on 2009年1月3日, 下午3:11
 */

#include <stdlib.h>
#include <cstdio>
#include <iostream>
using namespace std;
/*
 *a  simple soduku calculater
 */
int matrix[9][9]=
{
    { 0, 0, 0, 4, 0, 5, 0, 0, 1 },
    { 0, 7, 0, 0, 0, 0, 0, 3, 0 },
    { 0, 0, 4, 0, 0, 0, 9, 0, 0 },
    { 0, 0, 3, 5, 0, 4, 1, 0, 0 },
    { 0, 0, 7, 0, 0, 0, 4, 0, 0 },
    { 0, 0, 8, 9, 0, 1, 0, 0, 0 },
    { 0, 0, 9, 0, 0, 0, 6, 0, 0 },
    { 0, 8, 0, 0, 0, 0, 0, 2, 0 },
    { 4, 0, 0, 2, 0, 0, 0, 0, 0 }
};
int cnt =0;

int valid(int x, int y)
{
    int posVal = matrix[x][y];
    for(int i=0; i <9 ; ++i)
    {
        //col and row
        if(i != y && posVal == matrix[x][i])
            return 0;
        if(i != x && posVal == matrix[i][y])
            return 0;
    }
    // check in 3*3  area
    int xfrom = (x/3)*3;
    int yfrom = (y/3)*3;
    for(int j=xfrom; j < xfrom+3 ; ++j)
        for(int k=yfrom; k < yfrom+3 ; ++k)
            if( posVal == matrix[j][k] && (j != x || k != y) )
                return 0;

     return true;
 }

// recursion calculate each sequencing value whether is valid
int check(int x,int y)
{
    ++cnt;
    //next row
    if(y==9){
        y=0;
        ++x;
    }
    //get solution
    if( x == 9)
    {
        return true;
    }
    if(matrix[x][y] != 0)
    {
        if( valid(x,y) )
            return check(x,y+1);
        else
            return false;
    }
    else
        for(int rnd=1; rnd <=9 ;++rnd)
        {
            matrix[x][y] = rnd;
            if(valid(x,y))
                if(check(x,y+1))
                    return true;
        }
    matrix[x][y] = 0;
    return false;
}

int main(int argc, char** argv) {

     if(check(0,0))
     {
        for(int j=0; j<9; ++j)
            for(int k=0; k<9; ++k){
                printf(" %d",matrix[j][k]);
                if(k ==8)
                    putchar('\n');
            }
        cout<<"end count:"<<cnt<<endl;
     }
     else
        puts("cannot get the solution!");
    
    return (EXIT_SUCCESS);
}

 最终测试结果(本人机器CPU AMD3000 +1G DDRII):

ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux] real    14.155s
ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203) [i686-linux] real    4.390s
G++ real    0m0.119s

 

有此可见,ruby新版性能有了极大的飞跃,不过还是有很大的改进空间。

   发表时间:2009-01-03  
引用
ruby1.8.7 4.390s
ruby1.9.1 14.155s


是不是颠倒了?

 

0 请登录后投票
   发表时间:2009-01-03   最后修改:2009-01-03
hanwei59 写道

引用
ruby1.8.7  4.390s
ruby1.9.1  14.155s
是不是颠倒了?

阿哦,确实颠倒了~~
0 请登录后投票
   发表时间:2009-01-04  
ruby1.9 的综合测试性能一般是ruby 1.8的3-5倍左右,所以性能提升的确是巨大的。现在就看Rails能提升多少了。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics