1 回答
TA贡献1966条经验 获得超4个赞
您的时间单位太大,无法捕获所花费的时间。您应该采用更小的时间单位,例如纳秒,并尝试多次运行相同的检查,例如 10000 次。
带有纳秒的示例代码。
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);
添加回答
举报