Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to measure time elapsed in Java

2 Answers

0 votes
import java.util.concurrent.TimeUnit;
  
public class MeasureTimeElapsed_Java {
    public static void main(String args[]) throws InterruptedException {
        long startTime = System.currentTimeMillis();
    
        // code to be measured 
        TimeUnit.SECONDS.sleep(2);
        // code to be measured 
          
        long endTime = System.currentTimeMillis();
        long elapsedTime = endTime - startTime;
  
        System.out.println("Elapsed time in milliseconds: " + elapsedTime);
    }
}
   
   
   
   
/*
run:
   
Elapsed time in milliseconds: 2000
   
*/

 



answered Oct 14, 2023 by avibootz
edited Aug 4, 2024 by avibootz
0 votes
import java.util.concurrent.TimeUnit;
import java.time.Duration;
import java.time.Instant;

public class MyClass {
    public static void main(String args[]) throws InterruptedException {
        Instant start = Instant.now();
  
        // code to be measured 
        TimeUnit.SECONDS.sleep(2);
        // code to be measured 
        
        Instant end = Instant.now();
        Duration elapsed = Duration.between(start, end);
        
        System.out.println("Elapsed time in milliseconds: " + elapsed.toMillis());
    }
}
 
 
 
 
/*
run:
 
Elapsed time in milliseconds: 2000
 
*/

 



answered Oct 14, 2023 by avibootz

Related questions

1 answer 75 views
75 views asked Oct 17, 2024 by avibootz
1 answer 92 views
92 views asked Aug 4, 2024 by avibootz
1 answer 89 views
89 views asked Aug 4, 2024 by avibootz
2 answers 176 views
3 answers 215 views
215 views asked Mar 13, 2023 by avibootz
2 answers 223 views
...