我的程序需要帮助
我在 Java 中有这个程序 import java.util.*;
public class Euclid {
private static final String EXCEPTION_MSG =
"Invalid value (%d); only positive integers are allowed. ";
public static int getGcd( int a, int b)
{
if (a < 0)
{
throw new IllegalArgumentException(String.format(EXCEPTION_MSG, a));
}
else
if (b < 0)
{
throw new IllegalArgumentException(String.format(EXCEPTION_MSG, b));
}
while (b != 0)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class EuclidGui
{
private static final String PROMPT_A = "#1";
private static final String PROMPT_B = "#2";
private static final String BUTTON_TEXT = "Get GCD >>";
private static final String EXCEPTION_TITLE = "Input Exception";
private static final String INSTRUCTIONS = "Type to integer and press 'Get GCD'";
private static final String DIALOG_TITLE = "Euclid's Algorithm";
private static final int FIELD_WIDTH = 6;
public static void main (String[] args)
{
final JTextField valueA = new JTextField (FIELD_WIDTH);
final JTextField valueB = new JTextField (FIELD_WIDTH);
final JTextField valueGcd = new JTextField (FIELD_WIDTH);
JLabel labelA = new JLabel(PROMPT_A);
JLabel labelB = new JLabel(PROMPT_B);
JButton computeButton = new JButton(BUTTON_TEXT);
Object[] options = new Object[] {labelA, valueA, labelB, valueB, computeButton, valueGcd};
valueGcd.setEditable (false);
computeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{ try
{
int a = Integer.parseInt(valueA.getText());
int b = Integer.parseInt(valueB.getText());
int gcd = Euclid.getGcd(a , b);
valueGcd.setText(Integer.toString(gcd));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), EXCEPTION_TITLE, JOptionPane.ERROR_MESSAGE);
}
}
});
JOptionPane.showOptionDialog(null, INSTRUCTIONS, DIALOG_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, null);
}
}
我想在其上添加计算时间代码,但我不知道如何添加,以及我使用的正确代码是什么。如果有人有想法,请帮助我。
I have this program in Java import java.util.*;
public class Euclid {
private static final String EXCEPTION_MSG =
"Invalid value (%d); only positive integers are allowed. ";
public static int getGcd( int a, int b)
{
if (a < 0)
{
throw new IllegalArgumentException(String.format(EXCEPTION_MSG, a));
}
else
if (b < 0)
{
throw new IllegalArgumentException(String.format(EXCEPTION_MSG, b));
}
while (b != 0)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class EuclidGui
{
private static final String PROMPT_A = "#1";
private static final String PROMPT_B = "#2";
private static final String BUTTON_TEXT = "Get GCD >>";
private static final String EXCEPTION_TITLE = "Input Exception";
private static final String INSTRUCTIONS = "Type to integer and press 'Get GCD'";
private static final String DIALOG_TITLE = "Euclid's Algorithm";
private static final int FIELD_WIDTH = 6;
public static void main (String[] args)
{
final JTextField valueA = new JTextField (FIELD_WIDTH);
final JTextField valueB = new JTextField (FIELD_WIDTH);
final JTextField valueGcd = new JTextField (FIELD_WIDTH);
JLabel labelA = new JLabel(PROMPT_A);
JLabel labelB = new JLabel(PROMPT_B);
JButton computeButton = new JButton(BUTTON_TEXT);
Object[] options = new Object[] {labelA, valueA, labelB, valueB, computeButton, valueGcd};
valueGcd.setEditable (false);
computeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{ try
{
int a = Integer.parseInt(valueA.getText());
int b = Integer.parseInt(valueB.getText());
int gcd = Euclid.getGcd(a , b);
valueGcd.setText(Integer.toString(gcd));
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), EXCEPTION_TITLE, JOptionPane.ERROR_MESSAGE);
}
}
});
JOptionPane.showOptionDialog(null, INSTRUCTIONS, DIALOG_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, null);
}
}
and I want to add calculation time code on it but I have no Idea how, and what is the correct code that I use.Please help me if any one have an Idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
System.currentTimeMillis() 就可以解决这个问题:
System.currentTimeMillis() would do the trick:
很可能一次调用
getGcd
所花费的时间很短,以至于您将很难可靠且准确地测量它。System.currentTimeMillis ()
方法将为您提供以毫秒为单位的挂钟时间。然而,毫秒时钟的粒度可能太粗了。 (阅读 javadoc!)。System.nanoTime ()
方法给出系统时间(正如 javadoc 所说)“纳秒精度,但不一定是纳秒精度。”。在某些操作系统的多核机器上,
nanoTime()
也存在问题。例如,我听说不同的内核可以有独立的 nanoTime 时钟,这些时钟可以相互漂移。这可能会导致System.nanoTime()
返回非单调的值;例如,如果当前线程被操作系统重新安排为在两次nanoTime()
调用之间在不同的内核上运行。如果我这样做,我将对
getGcd()
的调用放入一个运行 10,000 或 100,000 次的循环中,测量循环的时间,并将测量的时间除以相关系数。The chances are that a single call to
getGcd
will take so little time that you will have difficulty measuring it reliable and accurately.The
System.currentTimeMillis()
method will give you the wall clock time measured in milliseconds. However, the granularity of the millisecond clock is probably too coarse. (Read the javadoc!).The
System.nanoTime()
method gives a system time with (as the javadoc says) "nanosecond precision, but not necessarily nanosecond accuracy.".There are also issues with
nanoTime()
on multicore machines with some operating systems. For instance, I've heard that different cores can have independent nanoTime clocks that can drift with respect to each other. This can result inSystem.nanoTime()
returning values that are non-monotonic; e.g. if the current thread is rescheduled by the OS to run on a different core between twonanoTime()
calls.If I was doing this, I'd put the calls to
getGcd()
into a loop that ran it 10,000 or 100,000 times, measure the time for the loop, and divide the measured time by the relevant factor.