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 of a function in C++

4 Answers

0 votes
#include <iostream>
#include <chrono>
#include <thread>
 
void sleep(int sec) {
    std::this_thread::sleep_for(std::chrono::seconds(sec));
}
 
int main() {
 
    auto start = std::chrono::high_resolution_clock::now();
     
    sleep(4);
     
    auto end = std::chrono::high_resolution_clock::now();
 
    auto time_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(end - start);
 
    std::cout << time_in_seconds.count() << " seconds";
}
  
  
  
  
/*
run:
  
4 seconds
  
*/

 



answered May 16, 2021 by avibootz
edited May 24, 2023 by avibootz
0 votes
#include <iostream>
#include <chrono>
#include <thread>
  
void sleep(int sec) {
    std::this_thread::sleep_for(std::chrono::seconds(sec));
}
  
int main() {
  
    auto start = std::chrono::high_resolution_clock::now();
      
    sleep(4);
      
    auto end = std::chrono::high_resolution_clock::now();
  
    std::chrono::duration<double, std::milli> timi_in_milliseconds = end - start;
  
    std::cout << timi_in_milliseconds.count() << " ms";
}
   
   
   
   
/*
run:
   
4000.15 ms
   
*/

 



answered May 16, 2021 by avibootz
edited May 24, 2023 by avibootz
0 votes
#include <iostream>
#include <chrono>
#include <thread>
  
void sleep(int sec) {
    std::this_thread::sleep_for(std::chrono::seconds(sec));
}

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    
    sleep(4);
   
    auto finish = std::chrono::high_resolution_clock::now();
    
    auto time_in_nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count();
    
    std::cout << time_in_nanoseconds << " ns";
}

   
   
   
/*
run:
   
4000103259 ns
   
*/

 



answered May 24, 2023 by avibootz
0 votes
#include <iostream>
#include <chrono>
#include <thread>
   
void sleep(int sec) {
    std::this_thread::sleep_for(std::chrono::seconds(sec));
}
 
int main() {
    auto start = std::chrono::high_resolution_clock::now();
     
    sleep(4);
    
    auto finish = std::chrono::high_resolution_clock::now();
     
    auto time_in_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count();
     
    std::cout << time_in_microseconds << " µs";
}
 
    
    
    
/*
run:
    
4000056 µs
    
*/

 



answered May 24, 2023 by avibootz

Related questions

1 answer 155 views
155 views asked Feb 21, 2022 by avibootz
1 answer 292 views
1 answer 221 views
221 views asked Apr 5, 2019 by avibootz
2 answers 130 views
1 answer 116 views
1 answer 215 views
2 answers 137 views
...