验证处理时间的有效方法

我正在尝试找到使用不同块用法的最佳性能方式/方法。我正在运行下面的回文程序,用 3 种不同的方式测量每个程序的执行时间。但我总是得到'0'作为输出。我的代码有什么问题。请帮我。


主要的


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

回文使用 StringBuilderReverse


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.";

        }

回文使用ForLoop


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.";

            }

回文UsingWhile


    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.";



一只名叫tom的猫
浏览 91回答 1
1回答

慕标5832272

您的时间单位太大,无法捕获所花费的时间。您应该采用更小的时间单位,例如纳秒,并尝试多次运行相同的检查,例如 10000 次。带有纳秒的示例代码。&nbsp; &nbsp; &nbsp; &nbsp; long startTime, endTime, duration;&nbsp; &nbsp; &nbsp; &nbsp; startTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(palindromeUsingStringBuilderReverse(str));&nbsp; &nbsp; &nbsp; &nbsp; endTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; duration = endTime - startTime;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duration for using string builder : " + duration);&nbsp; &nbsp; &nbsp; &nbsp; startTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(palindromeUsingForLoop(str));&nbsp; &nbsp; &nbsp; &nbsp; endTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; duration = endTime - startTime;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duration for using for loop : " + duration);&nbsp; &nbsp; &nbsp; &nbsp; startTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(palindromeUsingWhile(str));&nbsp; &nbsp; &nbsp; &nbsp; endTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; duration = endTime - startTime;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duration for using while loop : " + duration);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java