开发者

I need help in my program

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_开发者_JS百科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.


System.currentTimeMillis() would do the trick:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜