effective way to validate the processing time

mmar :

am trying to find the best way/approach for performance of using different block usage. i am running the below palindrome program with 3 different way measuring the executing time for each. but i always get '0' as output. what is wrong in my code. please help me.

main

    long startTime, endTime, duration;

    startTime = Instant.now().toEpochMilli();
    System.out.println(palindromeUsingStringBuilderReverse(str));
    endTime = Instant.now().toEpochMilli();
    duration = (endTime - startTime);
    System.out.println("Duration for using string builder : " + duration);

    startTime = Instant.now().toEpochMilli();
    System.out.println(palindromeUsingForLoop(str));
    endTime = Instant.now().toEpochMilli();
    duration = (endTime - startTime);
    System.out.println("Duration for using for loop : " + duration);


    startTime = Instant.now().toEpochMilli();
    System.out.println(palindromeUsingWhile(str));
    endTime = Instant.now().toEpochMilli();
    duration = (endTime - startTime);
    System.out.println("Duration for using while loop : " + duration);

palindromeUsingStringBuilderReverse

StringBuilder bdr = new StringBuilder(str);
        if (str.equalsIgnoreCase(bdr.reverse().toString())) {
            return "The given string is a Palindrome.";
        } else {
            return "This is not a Palindrome string.";
        }

palindromeUsingForLoop

String revStr = "";
for (int i=str.length()-1; i>=0; i--) {
                revStr = revStr + str.charAt(i);
                }

    if (str.equalsIgnoreCase(revStr)) {
                return "The given string is a Palindrome.";
            } else {
                return "This is not a Palindrome string.";
            }

palindromeUsingWhile

    int i = 0, j = str.length() - 1;
    while (i < str.length()) {
        if (str.charAt(i) != str.charAt(j)) {
            return "This is not a Palindrome string.";
        } else {
            i++;
            j--;
        }
    }
    return "The given string is a Palindrome.";

output on execution

Enter a word to check its a palindrome or not : madammadammadammadammadammadammadammadammadammadam
The given string is a Palindrome.
Duration for using string builder : 0
The given string is a Palindrome.
Duration for using for loop : 0
The given string is a Palindrome.
Duration for using while loop : 0

Process finished with exit code 0
Abhishek Garg :

Your time unit is too big to capture the time taken. You should take a smaller time-unit like a nanosecond and try to run the same check many times such as 10000 times.

Sample code with nanoseconds.

        long startTime, endTime, duration;
        startTime = System.nanoTime();
        System.out.println(palindromeUsingStringBuilderReverse(str));
        endTime = System.nanoTime();
        duration = endTime - startTime;
        System.out.println("Duration for using string builder : " + duration);

        startTime = System.nanoTime();
        System.out.println(palindromeUsingForLoop(str));
        endTime = System.nanoTime();
        duration = endTime - startTime;
        System.out.println("Duration for using for loop : " + duration);

        startTime = System.nanoTime();
        System.out.println(palindromeUsingWhile(str));
        endTime = System.nanoTime();
        duration = endTime - startTime;
        System.out.println("Duration for using while loop : " + duration);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135240&siteId=1