其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·前两天看到的#pragma用法
·用C写的简单学生成绩管理系统
·射频芯片nRF401天线设计的分析
·入门系列--OpenGL最简单的入门
·简单的CreateRemoteThread例程-初学者必看
·BCB数据库图像保存技术
·GNU中的Makefile
·使用AutoMake轻松生成Makefile
·数据结构

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
三种传奇语言的速度比较(4)

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

代码C:

/*
 * Created on 2005-3-21
 */

/**
 * @author jtzhu
 */
using System;

internal class NoSuchElementException:System.Exception{};
public class list {
     internal class Element
     {
          internal int datum;
      internal Element next;
      internal Element(int datum, Element next)
  {
    this.datum = datum;
    this.next  = next;
       }
     }

     internal class Iter
     {
      Element curr;
      internal Iter(Element head)
      {
          this.curr = head;
      }
      internal bool hasNext()
      {
       return curr != null;
      }
      internal int next()
      {
       if(curr == null)
        throw new NoSuchElementException();
       int result = curr.datum;
       curr = curr.next;
       return result;
      }
     };
     Element head;
     Element tail;
     list()
     {
      head = tail = null;
     }
     bool isEmpty()
     {
      return head == null;
     }
     void purge()
     {
      Element curr = head;
      Element next;
      while(curr!=null)
      {
       next = curr.next;
       curr = next;
      }

      head = tail = null;
     }
     void append(int datum)
     {
      if(head == null)
      {
       head = tail = new Element(datum,null);
      }
      else
      {
       tail.next = new Element(datum, null);
       tail = tail.next;
      }
     }
     void extract(int datum)
     {
      if(isEmpty())
       throw new NoSuchElementException();
      Element curr = head;
      Element pre  = null;
      while(curr != null && curr.datum != datum)
      {
       pre = curr;
       curr = curr.next;
      }
      if(curr == null)
       throw new NoSuchElementException();
      if(curr == head)
      {
       Element next = head.next;
       head = next;
      }
      else
      {
       pre.next = curr.next;
       }
      if(curr == tail)
       tail = pre;
     }
     void assign(list another)
     {
      if(this == another)
       return;
      Iter iter = another.iterator();
      purge();
      while(iter.hasNext())
      {
       append(iter.next());
      }
     }
     Iter iterator()
     {
      return new Iter(head);
     }
    public static void Main() {
     list from = new list();
     list to = new list(); 
     const int length = 100000;
     const int innerLoops = 100;
     for(int i=0;i<length;i++)
      from.append(i);

     DateTime start,end;
     double timeuse;
     for(int i=0;i<3;i++)
     {
      Console.Write("loop "+i+":\n");
      timeuse = 0;
      for(int k=0;k<innerLoops;k++)
      {
       to.purge();
       start = DateTime.Now;
       for(int j=0;j<length;j++)
        to.append(j);
       end = DateTime.Now;
       timeuse+= (end-start).TotalMilliseconds;
      }
      Console.Write("append: "+length/timeuse*innerLoops/1000+"M/s\t");

      timeuse = 0;
      for(int k=0;k<innerLoops;k++)
      {
       to.purge();
       start = DateTime.Now;
       to.assign(from);
       end = DateTime.Now;
       timeuse+= (end-start).TotalMilliseconds;
      }
      Console.Write("assign: "+innerLoops/timeuse*1000+"/s\t");

      start = DateTime.Now;
      for(int j=length/2;j<(length/2+1000);j++) 
       to.extract(j);
      end = DateTime.Now;
      timeuse=(end-start).TotalMilliseconds;
      Console.Write("extract: "+1000/timeuse+"k/s\t");

      timeuse = 0;
      for(int k=0;k<innerLoops;k++)
      {
       to.assign(from);
       start = DateTime.Now;
       to.purge();
       end = DateTime.Now;
       timeuse+= (end-start).TotalMilliseconds;
      }
      Console.Write("purge: "+innerLoops/timeuse*1000+"/s\n");
     }
    }
}


编译方式:

csc list.cs




相关文章

相关软件




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有