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,884 questions

51,810 answers

573 users

How to measure execution time in C++

1 Answer

0 votes
#include <iostream>
#include <chrono>

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    
    for (int i = 0; i < 1000000000; i++);
    
    auto end = std::chrono::high_resolution_clock::now();

    auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start).count(); 
    std::cout << duration << " sec" << '\n';
    
    duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    std::cout << duration << " ms" << '\n';
    
    duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << duration << " µs" << '\n';

    duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
    std::cout << duration << " [ns]" << '\n';
}
 
 
 
 
 
/*
run:
 
1 sec
1888 ms
1888610 µs
1888610879 [ns]
 
*/

 



answered Feb 21, 2022 by avibootz
edited Feb 21, 2022 by avibootz

Related questions

1 answer 292 views
4 answers 284 views
2 answers 137 views
1 answer 123 views
123 views asked May 29, 2023 by avibootz
1 answer 227 views
1 answer 221 views
221 views asked Apr 5, 2019 by avibootz
...