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

51,811 answers

573 users

How to measure execution time of a code block in C++

1 Answer

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

constexpr int LEN = 10000000;

void setNumbers(int arr[]) {
    std::srand(std::time(nullptr));
    
    for (size_t i = 0; i < LEN; i++) {
        arr[i] = std::rand();
    }
}

int main() {
    int *arr = new int[LEN];

    auto start = std::chrono::high_resolution_clock::now();
    
    setNumbers(arr);
    
    auto end = std::chrono::high_resolution_clock::now();

    std::chrono::duration<double, std::milli> duration_float = end - start;
    auto int_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

    std::cout << duration_float.count() << " milliseconds";

    delete [] arr;
}
 
 
 
 
/*
run:
 
183.452 milliseconds
 
*/

 



answered May 16, 2021 by avibootz
edited May 16, 2023 by avibootz

Related questions

2 answers 256 views
1 answer 227 views
2 answers 239 views
1 answer 155 views
155 views asked Feb 21, 2022 by avibootz
4 answers 285 views
1 answer 86 views
2 answers 137 views
...