我的测量执行时间的代码存在问题
所以,我试图测量一些排序方法的执行时间。
这是我的代码:
public static void main(String[] args)
{
...
MeasureExecutionTime(new Runnable() { public void run() { insertionSort(C); } }, "insertionSort()");
}
=====================
private static void MeasureExecutionTime(Runnable r, String s)
{
startTime = System.nanoTime();
try
{
r.run();
}
finally
{
endTime = System.nanoTime();
}
elapsedTime = endTime - startTime;
System.out.println(s + " takes " + elapsedTime + " nano-seconds which is " + formatTime(elapsedTime));
}
=====================
public static String formatTime(long nanoSeconds)
{
long hours, minutes, remainder, totalSecondsNoFraction;
double totalSeconds, seconds;
totalSeconds = (double) nanoSeconds / 1000000000.0;
String s = Double.toString(totalSeconds);
String [] arr = s.split("\\.");
totalSecondsNoFraction = Integer.parseInt(arr[0]);
hours = totalSecondsNoFraction / 3600;
remainder = totalSecondsNoFraction % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1]));
StringBuilder result = new StringBuilder(".");
String sep = "", nextSep = " and ";
if(seconds > 0)
{
if(seconds > 1) result.insert(0, " seconds").insert(0, seconds);
else result.insert(0, " second").insert(0, seconds);
sep = nextSep;
nextSep = ", ";
}
if(minutes > 0)
{
if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
sep = nextSep;
nextSep = ", ";
}
if(hours > 0)
{
if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
else result.insert(0, sep).insert(0, " hour").insert(0, hours);
}
return result.toString();
}
我的问题是:
运行此程序后,我输入 int[1000000]
作为输入,它会在大约 12-13 分钟内执行 insertionSort()
然后返回:
insertionSort() takes 767186856920 nano-seconds which is 12 minutes and 470.18685692 seconds.
为什么它给出 470秒?我的代码有什么问题?
=========================
编辑:
替换 seconds = Double.parseDouble(Long.toString(( long)Seconds) + Double.parseDouble("." + arr[1]));
与 Seconds = Seconds + Double.parseDouble("." + arr[1]);
,上一个问题消失了,但又出现了一个问题:
insertionSort() takes 22864 nano-seconds which is 2.000002864 seconds.
应该是0.000022864秒。
========================== =
编辑2:
我可能会发现错误。当nanoSeconds
很大时,arr[1]
就可以了,但是当nanoSeconds
很小时,arr[1]
将转换为指数形式,即14931纳秒=> 4.931E-6 秒。
。我该如何解决这个问题?
==========================
EDIT3:
好的,我找到了解决方案:
if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
else seconds += Double.parseDouble("." + arr[1]);
So, I am trying to measure the execution time of some sorting methods.
Here is my code:
public static void main(String[] args)
{
...
MeasureExecutionTime(new Runnable() { public void run() { insertionSort(C); } }, "insertionSort()");
}
=====================
private static void MeasureExecutionTime(Runnable r, String s)
{
startTime = System.nanoTime();
try
{
r.run();
}
finally
{
endTime = System.nanoTime();
}
elapsedTime = endTime - startTime;
System.out.println(s + " takes " + elapsedTime + " nano-seconds which is " + formatTime(elapsedTime));
}
=====================
public static String formatTime(long nanoSeconds)
{
long hours, minutes, remainder, totalSecondsNoFraction;
double totalSeconds, seconds;
totalSeconds = (double) nanoSeconds / 1000000000.0;
String s = Double.toString(totalSeconds);
String [] arr = s.split("\\.");
totalSecondsNoFraction = Integer.parseInt(arr[0]);
hours = totalSecondsNoFraction / 3600;
remainder = totalSecondsNoFraction % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1]));
StringBuilder result = new StringBuilder(".");
String sep = "", nextSep = " and ";
if(seconds > 0)
{
if(seconds > 1) result.insert(0, " seconds").insert(0, seconds);
else result.insert(0, " second").insert(0, seconds);
sep = nextSep;
nextSep = ", ";
}
if(minutes > 0)
{
if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
sep = nextSep;
nextSep = ", ";
}
if(hours > 0)
{
if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
else result.insert(0, sep).insert(0, " hour").insert(0, hours);
}
return result.toString();
}
My problem is:
After run this program, and I enter int[1000000]
as an input, it executes insertionSort()
in about 12-13 minutes and then returns:
insertionSort() takes 767186856920 nano-seconds which is 12 minutes and 470.18685692 seconds.
why it gives 470 seconds? what is wrong in my code?
=========================
EDIT:
After replacing seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1]));
with seconds = seconds + Double.parseDouble("." + arr[1]);
, the previous issue is gone, but another issue showed up:
insertionSort() takes 22864 nano-seconds which is 2.000002864 seconds.
It should be 0.000022864 seconds.
=========================
EDIT2:
I might discover the error. when nanoSeconds
is large, arr[1]
will be ok but when nanoSeconds
is small, arr[1]
will convert to exponential form, i.e 14931 nano-seconds => 4.931E-6 seconds.
. How can I solve this issue?
==========================
EDIT3:
Ok, I found the solution:
if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
else seconds += Double.parseDouble("." + arr[1]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题就在这里:
假设进入这一行的
秒
是12
,而arr[1]
是“456”
。那么为什么不这样做:
The problem is here:
Say
seconds
is12
entering this line andarr[1]
is"456"
. ThenWhy not just do:
我认为您正在连接两个字符串“47”和“0.186...”。
I think you are concatenating two string "47" and "0.186...".
问题是这一行:
第二个
parseDouble
返回“0.18685692”。由于arr[1]
已经是小数点右侧的字符串,只需使用:The problem is this line:
The second
parseDouble
is returning "0.18685692". Sincearr[1]
is already the string to the right of the decimal point, just use:连接字符串时会添加一个额外的零。为了避免这种问题,最好使用格式化程序来创建字符串。请参阅此处的详细信息 http://download.oracle。 com/javase/1.5.0/docs/api/java/util/Formatter.html。
此外,手动计算正确答案总是有帮助的。这使得问题更容易找到。
You are adding an extra zero when you concatenate the strings. To avoid this kind of problem it would be better to use a formatter to create you strings. See details here http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html.
Also, it always helps to calculate the correct answer by hand. This made the problem much easier to find.