代码以毫秒为单位打印时间

时间:2016-06-25 03:10:49

标签: java

这是我的代码,我得到了巨大的输出。我的代码应以毫秒为单位输出时间。我做错了吗?

Random rand = new Random(); 
int num = rand.nextInt(100); 
static long linear = System.currentTimeMillis();
//Linear Search in Unsorted array
private static int linearSearch(int list[],int key)
{

//finds an element in a list
   for(int i=0;i<list.length;i++)
   {
       //if element is found, element is then returned
       if(list[i]==key)
           return i;
   }
   //if element is not found, -1 is returned
   return -1;
}
System.out.println("Linear Search Time: " + linear);

4 个答案:

答案 0 :(得分:0)

在我看来,您正试图找到使用linearSearch函数在列表中查找给定键所需的时间。最好的方法是在调用函数之前存储它,并在从函数返回之后,然后获得差异以查看它花费的时间。除非你有相当大的列表,否则这可能会返回0ms,就像下面的例子一样,因为计算机太快了。你可以创建一个while循环并创建一个巨大的数组来查看一些值。

import java.util.Random;
public class HelloWorld{
    Random rand = new Random(); 
    int num = rand.nextInt(100); 
    static long linear = System.currentTimeMillis();
    //Linear Search in Unsorted array
     public static void main(String []args){
        System.out.println("Hello World");
        System.out.println("Linear Search Time: " + linear);

        int[] list = {1 ,3 ,4,5,6,7};
        int key = 7;
        long start_linear = System.currentTimeMillis();
        linearSearch(list,key);
        long end_linear = System.currentTimeMillis();
        System.out.println("linearSearch took: " + (end_linear - start_linear) + "ms" );
     }
    private static int linearSearch(int list[],int key)
    {

    //finds an element in a list
       for(int i=0;i<list.length;i++)
       {
           //if element is found, element is then returned
           if(list[i]==key)
               return i;
       }
       //if element is not found, -1 is returned
       return -1;
    }
}

答案 1 :(得分:0)

计算执行方法之前和之后的时间差 linearSearch(int list[],int key)

像这样写下最后一行

System.out.println("Linear Search Time: " + ( System.currentTimeMillis() - linear));

答案 2 :(得分:0)

currentTimeMillis()返回UTC时间1970年1月1日午夜以来的毫秒数。

您所做的就是打印当前时间。搜索完成后,再次获取当前时间并第一次从.currentTimeMillis()中减去你的内容。

static long linearFinish = System.currentTimeMillis();
System.out.println("Linear Search Time: " + (linearFinish - linear));

答案 3 :(得分:0)

下面是一些将运行的代码,并打印出运行函数所需的时间。您遇到的问题是您要求系统时间,这是一个很大的数字,因为它是计算机运行的时间。为了获得运行函数的时间,我们只需要在t1中运行let调用之前询问时间。然后我们运行该功能。在功能完成运行之后我们又要求时间让它称之为t2。然后我们取t2 - t1这个差异是你的函数运行的时间,以及执行计算的时间(最小)。另请注意,在我的示例中,输出的时间将为零,因为数组太小,计算速度非常快。

import java.util.Random;

public class App {

Random rand = new Random(); //stays same
int num = rand.nextInt(100); //stays same
static long linear = 0;//more optimal to move where initial time is set (t1)

//main where things are run ;)
public static void main(String[] args) {

    //could be placed outside in the class
    int[] array = {1,2,3,4,5};//some array of numbers
    int key = 5; //a key 

    linear = System.currentTimeMillis();//t1 set
    linearSearch(array, 5);//calling of your search function
    linear = System.currentTimeMillis() - linear; //obtaining the difference between the time, and t2 = System.currentTimeMillis()
    //the function started and when it finished
    System.out.println("Linear Search Time: " + linear);//printing out the difference, which is the time taken
    //also note that the number will likely be zero, as this function will run extremely fast for most arrays.

}

//your function
//Linear Search in Unsorted array
private static int linearSearch(int list[],int key)
{

//finds an element in a list
   for(int i=0;i<list.length;i++)
   {
       //if element is found, element is then returned
       if(list[i]==key)
           return i;
   }
   //if element is not found, -1 is returned
   return -1;
}

}