使用Ruby实现算法时遇到的性能问题

昨天作了一道 Topcoder的题目,是一道典型的图搜索问题。由于正在学习Ruby,所以用Ruby实现了一下。
 
测试时发现规模小的时候比如100*100以下的图,运行时间还可以接受。而题目要求是500*500的图,这种规模下,运行时间已经超出了我的耐心。
 
一般来说, Topcoder的题目都是能够十多秒运行出结果的,否则就是没有做对。
 
本来以为是我的算法写的有问题,所以反复检查我的算法,并没有发现更有效率的途径。
 
开始怀疑ruby的性能问题,用Java把其中的堆数据结构重新实现了一次,然后和ruby实现的堆进行比较。
 
结果让人大吃一惊,竟然比ruby快20-30倍。我的测试并不权威,所以具体数据我就不贴出来了,以免发生非议。程序在后面,有兴趣的自己测试一下。
 
用Java实现是可以满足时间要求的,所以问题是在性能上。
 
结论:世间没有药能治百病,编程语言也是如此。脚本语言有着数倍于强类型语言的编程效率,同样也有着接近一个数量级的效率低下。
 
我这个测试并不代表现实世界脚本语言应用程序的效率,因为大部分脚本应用程序属于IO密集型,比如网站,解释速度不是瓶颈。
 
在关键任务上,可以使用ruby作为粘合剂,而算法用C或者C++实现。充分利用各种语言的优势。
 
 附:堆数据结构的 ruby 和 java 实现
 

Ruby实现:

 
 
class Heap
    def initialize(list)
        @heap =[]
        insert(list)
    end
   
    def push(item)
        @heap.push(item)
        go_up(@heap.size-1)
    end
   
    def insert (list)
        list.each do |item|
            push(item)
        end
    end
   
    def pop_each
        while data = pop
            break if yield(data)
        end
    end
   
    def pop
        if  @heap.size>1
            result = @heap.first
            @heap[0] = @heap.pop;
            go_down(0)
            return result
        elsif @heap.size==1
            return @heap.pop
        else
            nil
        end
    end
   
    private
   
    def go_up(pos)
        if parent(pos) >= 0 && @heap[pos]<@heap[parent(pos)]
            swap(pos,parent(pos))
            go_up(parent(pos))
        end
    end
   
    def go_down(pos)
        tmp = min(pos)
        if tmp!=pos
            swap(pos,tmp)
            go_down(tmp)
        end
    end
   
    def min(root)
        minPos(right_child(root) ,minPos(left_child(root)  , root) )
    end
   
    def minPos (left, right)
        return left > 0 ? (@heap[left]< @heap[right] ? left : right) : right
    end
   
    def parent(pos)
        pos > 0 ? (pos-1)/2 :-1
    end
   
    def left_child(pos)
        pos*2+1 < @heap.size ? pos*2+1 : -1
    end
   
    def right_child(pos)
        pos*2 +2 < @heap.size ? pos*2+2 : -1
    end
   
    def swap(left,right)
        tmp =@heap[left]
        @heap[left]=@heap[right]
        @heap[right]=tmp
    end
   
end
 

 java实现:

 

/**
 *
 */
package test;
import java.util.ArrayList;
import java.util.List;
/**
 * @author dongbin
 *
 */
public class Heap {
 List<Comparable> heap = new ArrayList<Comparable>();
 public Heap(List<Comparable> data) {
  insert(data);
 }
 private void insert(List<Comparable> data) {
  for (Comparable object : data) {
   push(object);
  }
 }
 public void push(Comparable obj) {
  heap.add(obj);
  goUp(heap.size()-1);
 }
 
 public Object pop(){
  if(heap.size()>1){
   Object result = heap.get(0);
   heap.set(0,heap.remove(heap.size()-1));
   goDown(0);
   return result;
  }else if (heap.size()==1){
   return heap.remove(0);
  }else{
   return null;
  }
 }
 private int min(int root) {
  return minPos(right_child(root), minPos(left_child(root), root));
 }
 private int minPos(int left, int right) {
  return left > 0 ? (heap.get(left).compareTo(heap.get(right)) < 0 ? left
    : right) : right;
 }
 private void goUp(int pos) {
  if (parent(pos) >= 0
    && heap.get(pos).compareTo(heap.get(parent(pos))) < 0) {
   swap(pos,parent(pos));
   goUp(parent(pos));
  }
 }
 
 private void goDown(int pos){
  int min = min(pos);
  if(min!=pos){
   swap(pos,min);
   goDown(min);
  }
 }
 
 private void swap(int left , int right){
  heap.set(right,heap.set(left,heap.get(right)));
 }
 private int parent(int pos) {
  return pos > 0 ? (pos - 1) >> 1 : -1;
 }
 private int right_child(int pos) {
  return (pos << 1) + 2 < heap.size() ? (pos << 1) + 2 : -1;
 }
 private int left_child(int pos) {
  return (pos << 1) + 1 < heap.size() ? (pos << 1) + 1 : -1;
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值