1

So I have to write a program in java that has to search for an object(String) in an array forwards and backwards and see which one is faster. When I am testing, it takes a lot longer going forward for the program to find an object at the beginning of the array then going backwards. I am confused as it should be faster since it is at the beginning of the array. Can someone explain to me what I am doing wrong?

import java.util.Scanner;
import java.util.Arrays;

public class LinearStringSearch{

    public static long startTime;
    public static long endTime;
    public static long startTimeB;
    public static long endTimeB;


    public static void printArray(String arr[]){
        for(int i = 0; i < arr.length; i++){
            System.out.println(arr[i]);
        }
    }


    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a word and we will check our database for it");
        String a = input.nextLine();
        String [] words = new String[10];
        for (int i = 0; i < words.length; i++){
            words[i] = "Test " + (i + 1);
        }


        long startTime = System.nanoTime();
        for(int i = 0; i < words.length; i++){
            if (a.equals(words[i])){
            System.out.println("It is located at index: " + i);    
            break;
            }
        }
        long elaspedTime = System.nanoTime() - startTime;

        System.out.println("Going from the front, it took " + elaspedTime);

        long startTimeBack = System.nanoTime();
        for (int i = words.length-1; i >= 0; i--){
            if (a.equals(words[i])){
            System.out.println("It is located at index: " + i);    
            break;
            }


        }
        long elapsedTimeB = System.nanoTime() - startTimeBack;

        System.out.println("Going from the back, it took " + elapsedTimeB);

    }

}
Leo Zhao
  • 544
  • 7
  • 18

2 Answers2

1

With an array size of 10, it's probably executing too fast to see meaningful results. If you change the array size to 1,000,000 (as in below) and search for "Test 1", you will see noticeable results:

String [] words = new String[1000000];

Here is the output:

Please enter a word and we will check our database for it
Test 1
It is located at index: 0
Going from the front, it took 198038
It is located at index: 0
Going from the back, it took 17801149
Edwin Torres
  • 2,774
  • 1
  • 13
  • 15
  • I changed the value and it works! Thanks so much, why is that thouugh? Shouldn't it not matter? – ArtificialBeavers May 31 '14 at 01:02
  • It's probably too fast to get consistency. Keep in mind that your computer is doing other things besides executing your Java program. With really fast search times, anything could throw it off. – Edwin Torres May 31 '14 at 01:06
0

In general, it is not possible with a benchmark like this to know how the code will perform within a real program. The JVM may start executing your program with the bytecode interpreter and then, if much of time is spent in the method, decide to JIT it and jump into optimized machine code (that is on-stack replacement, see the hotspot glossarry).

For more details, see the discussion and answer on the article how do i write correct micro benchmark in java .

For java, there are some benchmarking frameworks that help you with some aspects of proper benchmarking, e.g. multiple runs and result averaging. Take a look on JUnitBenchmarks and Caliper.

Community
  • 1
  • 1
cruftex
  • 5,545
  • 2
  • 20
  • 36