Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how I can make my program to calculate the running time of a method once the user click JButton until the result shown in JTextBox? i don't want the whole program running time.thanks the code is this.

Java
long startTime = System.currentTimeMillis();
     byte[]  plain = plaintext.getText().getBytes();
     byte[] K = key.getText().getBytes();
     byte[] encrypted = encrypt(plain, K);
     String a = bytesToHex(encrypted);
     encryptedtext.setText(a);

    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    time.setText(elapsedTime);


What I have tried:

i tried adding the method between the running time method
Posted
Updated 26-Mar-18 18:38pm
v2
Comments
Richard MacCutchan 27-Mar-18 4:45am    
What is the problem with this code?
Member 13725790 28-Mar-18 1:38am    
The wrong that it’s calculate the whole running program. What i want is to calculate only a particular method. Which is encryption because later i want to compare between the running time of method when the user enter different keys and different text size
Richard MacCutchan 28-Mar-18 3:43am    
Then add the time collectors at just the beginning and end of the method you want to measure. It's really not that complicated.
Member 13725790 28-Mar-18 9:26am    
I did but still same result 😪
Richard MacCutchan 28-Mar-18 9:30am    
What result? What is difficult about the following sequence:
Capture current time.
Run method
Capture current time.

The difference between the two times is the time taken for the method. You may need to use the milliseconds version to get reasonable accuracy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900