D.Nik - 9 months ago 24

Java Question

This is my code and I am getting a huge output. My code should output the time in milliseconds. Am I doing something wrong?

`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);

Answer

Below is some code that will run, and print out the time it took for your function to run. The problem you are having is that you are asking for the system time, which is some massive number, because it is how long the computer has been running. To get the time your function took to run we simply ask for the time before we run let's call in t1. Then we run the function. After then function has completed running we ask for the time again let's call it t2. Then we take t2 - t1 this difference is the time it took your function to run, and the time for the calculation to be performed (minimal). Also note that in my example the time outputted will be zero, as the array is so small, and the calculation is so fast.

```
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;
}
}
```