在Eclipse中运行一个程序,怎样测试其运行时间的长短。
   如要加代码,应加什么代码知道具体算法的运行时间复杂度。
   谢谢

解决方案 »

  1.   

    开始前
    long s1 = System.currentTimeMillis()
    结束后
    long s2 = System.currentTimeMillis()System.out.prinltn(s2-s1)得到多少毫秒。
      

  2.   

    你到底是求时间还是时间复杂度?时间复杂度一般是看出来的吧
    求时间可以用
    long startTime = System.nanoTime();
       // ... the code being measured ...
       long estimatedTime = System.nanoTime() - startTime;
    API里这段就是用来求代码运行时间的
      

  3.   

    package java04;import java.util.Arrays;public class A
    {
        public static void main(String args[]){
            int[] array = new int[]{1,2,2,4,5,5,5,8,9,9};
            System.out.println(Arrays.toString(filter(array)));
        }
        /*
        *主要思路就是先把结果放在原数组前面,并得到结果数组长度,最后复数组到结果
        */
        public static int[] filter(int[] array){
       
            int temp = array[0];
            int pos = 1;
            for(int i = 1;i<array.length;i++){
                if(temp!=array[i]){
                    temp=array[i];
                    array[pos++]=temp;//在原数组不重复的按顺序放置元素
                }
            }
            int[] result = new int[pos];//开辟新数组
            System.arraycopy(array, 0, result, 0, pos);//拷贝原数组前部分到结果
            return result;    }}
    如上面的代码,要想知道程序的运行时间应如何改代码呢/
    具体要添加到代码应加在什么地方.谢谢
      

  4.   


    import java.util.Arrays; public class A 

        public static void main(String args[]){ 
            int[] array = new int[]{1,2,2,4,5,5,5,8,9,9};
            long startTime = System.nanoTime(); 
            System.out.println(Arrays.toString(filter(array))); 
            long estimatedTime = System.nanoTime() - startTime; 
            System.out.println(estimatedTime);
        } 
        /* 
        *主要思路就是先把结果放在原数组前面,并得到结果数组长度,最后复数组到结果 
        */ 
        public static int[] filter(int[] array){ 
      
            int temp = array[0]; 
            int pos = 1; 
            for(int i = 1;i <array.length;i++){ 
                if(temp!=array[i]){ 
                    temp=array[i]; 
                    array[pos++]=temp;//在原数组不重复的按顺序放置元素 
                } 
            } 
            int[] result = new int[pos];//开辟新数组 
            System.arraycopy(array, 0, result, 0, pos);//拷贝原数组前部分到结果 
            return result;     } }