-3

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. and i have another error in the last line (long can't be converted to string). what i can do to set the result in jtextbox? and it still calculate the whole running time. what i want is to calculate only the encryption time to compare between different keys.

     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);
ays
  • 17
  • 6
  • 2
    Have you tried just... measuring it? There are a lot of inbuilt methods to get the current time. Just take a stamp before and after and calculate the difference. – Ben Mar 26 '18 at 11:14
  • 1
    Try `System.currentTimeMillis()` – NewUser Mar 26 '18 at 11:14
  • @Ben i tried. but it calculate the whole running time. how i can take stamp before and after? – ays Mar 26 '18 at 11:17
  • Guava Stopwatch. – Ben Watson Mar 26 '18 at 11:17
  • 2
    Take a timestamp before you start your "I want to measure this" part of the code. And after it. I don't really see how you could think of doing it differently. – Ben Mar 26 '18 at 11:18
  • @NewUser i did :( but i need something to make it only calculating that particular method. not the other things in program like when the user type something. you know – ays Mar 26 '18 at 11:18
  • https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – daniu Mar 26 '18 at 11:19
  • 1
    @ays None of us can see your code. We don't know what you tried or why it didn't work. We can't tell you exactly what lines to add where because we haven't got your code. – khelwood Mar 26 '18 at 11:19
  • @BenWatson so i make stopwatch as break once the result is out? and i want that time to be shown in another textbox. how i can do that? – ays Mar 26 '18 at 11:20
  • @khelwood sorry I'm out. ;') but I'll add that once i get homo. besides i will try stopwatch as BenWatson says. thank you guys – ays Mar 26 '18 at 11:23

3 Answers3

1

Simple way to do this by calculating startTime and endTime

public void someMethod() {
    long startTime = System.currentTimeMillis();

    // Your actual code

    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;

    System.out.println("Total Time :: " + totalTime + "ms");
}

To convert long to String, please use this function String.valueOf(long)

Satz
  • 307
  • 3
  • 19
  • that how my code look likes. the problem that code will go call method from outside the button. and i noticed it calculate the whole running time – ays Mar 26 '18 at 11:25
  • It will be better if you post some sample code like where is your call method going. – Satz Mar 26 '18 at 11:35
  • @ays You want to calculate time only for this method right? `byte[] encrypted = encrypt(plain, K);` Then insert `startTime` code before that line. – Satz Mar 27 '18 at 11:18
  • yes exactly. Thank you so much. But another problem is that when i enter same text and same key it doesn’t give exactly the same time :( is there might b wrong with my code or that is fine? – ays Mar 30 '18 at 17:09
  • @ays time may very for every input depends upon the performance (we can say depends upon memory, CPU process like that). So no wrong with your code. – Satz Mar 31 '18 at 15:24
  • aha i see thank you. No matter what it will give different results but close because it depends on the CPU? – ays Apr 01 '18 at 18:23
0
 Stopwatch timer = new Stopwatch().start();

 //your code

  timer.stop();
  System.out.println(timer.getElapsedTime());

or

  long startTime = System.currentTimeMillis();

//your code

  long stopTime = System.currentTimeMillis();
  long elapsedTime = stopTime - startTime;
  System.out.println(elapsedTime);

}

Vazgen Torosyan
  • 1,255
  • 1
  • 12
  • 26
  • I hope there is no Stopwatch class in java. But its available in [Apache](https://stackoverflow.com/questions/8255738/is-there-a-stopwatch-in-java) – Satz Mar 26 '18 at 11:23
  • Stopwatch is Guava class https://google.github.io/guava/releases/18.0/api/docs/com/google/common/base/Stopwatch.html but you can do with second method – Vazgen Torosyan Mar 26 '18 at 11:26
  • Heard downloading libraries from apache is hard to work with. i tried to download one with binaryCodec :( – ays Mar 26 '18 at 11:26
  • @VazgenTorosyan still same result. calculate the whole running time :( – ays Mar 27 '18 at 04:25
0
long start = System.nanoTime();
callToYourMethod();
long end = System.nanoTime();
System.out.println(end - start);
JTeam
  • 1,455
  • 1
  • 11
  • 16