Skip to main content
Post Undeleted by Lukasz
added 163 characters in body
Source Link
Lukasz
  • 7.7k
  • 4
  • 44
  • 52

1st approach:

long startTime = System.nanoTime(); while(System.nanoTime() - startTime < MAX_TIME_IN_NANOSECONDS){  // your code ...   long estimatedTime = System.nanoTime() - startTime; } 

2nd approach

Start your code in thread.

Sleep main thread for as long as you need.

Kill (stop, interrupt) your thread.

long startTime = System.nanoTime(); // your code ...   long estimatedTime = System.nanoTime() - startTime;  

1st approach:

long startTime = System.nanoTime(); while(System.nanoTime() - startTime < MAX_TIME_IN_NANOSECONDS){  // your code ... } 

2nd approach

Start your code in thread.

Sleep main thread for as long as you need.

Kill (stop, interrupt) your thread.

Post Deleted by Lukasz
Source Link
Lukasz
  • 7.7k
  • 4
  • 44
  • 52

long startTime = System.nanoTime(); // your code ... long estimatedTime = System.nanoTime() - startTime;