我的程序需要帮助

发布于 2024-10-05 20:07:45 字数 2805 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-10-12 20:07:45

System.currentTimeMillis() 就可以解决这个问题:

long start = System.currentTimeMillis();
// do stuff
long timeTaken = System.currentTimeMillis() - start;

System.currentTimeMillis() would do the trick:

long start = System.currentTimeMillis();
// do stuff
long timeTaken = System.currentTimeMillis() - start;
如此安好 2024-10-12 20:07:45

很可能一次调用 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 in System.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 two nanoTime() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文