Topic: 多线程下怎么计算程序段的运行时间???

  Print this page

1.多线程下怎么计算程序段的运行时间??? Copy to clipboard
Posted by: zhangminamen
Posted on: 2005-11-16 19:22

//怎么分别计算出插入排序和冒泡排序所以用的时间
import java.lang.System;
class ThreadTime
{
  static InsertTime it;
  static BubbleTime bt;
  public static void main(String args[])
  {
    int num[]={1,2,5,6,8,7,4,9,3,10};
    it=new InsertTime(num);
    //it.InsertTime(num);
    bt=new BubbleTime(num);
    //bt.BubbleTime(num);
    it.start();
    bt.start();
  }
}
class InsertTime extends Thread
{
  //private ThreadTime tht;
  //static Timer t=new Timer();
  //long t1;
  //long t0;
  int[] num=new int[10];
  InsertTime(int num[])
  {
    this.num=num;
  }
  long t0=System.currentTimeMillis();
  void insertSort()//插入排序
  {
    //t.startTimer();
    int temp;
    for(int i=1;i<10;i++)
    {
      for(int j=i;j>=0;j--)
      {
        if(num[j]>num[j-1])
        {
          temp=num[j];
          num[j]=num[j-1];
          num[j-1]=num[j];
        }
      }
    }
    //t1=System.currentTimeMillis();
    /*try
    {
      sleep(1000);
    }
    catch(InterruptedException e)
    {
      System.out.println(e.getMessage());
    }
    System.out.println(t1-t0);*/
  }
  public void run()
  {
    //insertSort(int num[]);
    for(int k=0;k<10;k++)
    {
      System.out.print(" "+num[k]);
      try
      {
        sleep(1000);
      }
      catch(InterruptedException e)
      {
        System.out.println(e.getMessage());
      }
    }
    System.out.println();
    System.out.println(t1-t0);
  }
}
class BubbleTime extends Thread
{
  //private ThreadTime tht;
  private int temp;
  //static Timer t=new Timer();
  int[] num=new int[10];
  BubbleTime(int num[])
  {
    this.num=num;
  }
  void bubbleSort()//冒泡排序
  {
    //t.startTimer();
    for(int i=0;i<10;i++)
      for(int j=0;j<9;j++)
      {
        if(num[j]<num[j+1])//按从大到小排序
        {
          temp=num[j];
          num[j]=num[j+1];
          num[j+1]=temp;
        }
      }
    //System.out.println(t.elapsedTimer());
  }
  public void run()
  {
    //bubbleSort(int num[]);
    for(int k=0;k<10;k++)
    {
      System.out.print(" "+num[k]);
      try
      {
        sleep(1000);
      }
      catch(InterruptedException e)
      {
        System.out.println(e.getMessage());
      }
    }
    System.out.println();
  }
}

2.Re:多线程下怎么计算程序段的运行时间??? [Re: zhangminamen] Copy to clipboard
Posted by: zhangminamen
Posted on: 2005-11-17 08:34

为什么没有人回答我的问题???


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923