Saturday

C++ Nanotime in nanoseconds

Source code: 

#include <iostream> 
#include <chrono> 

using namespace std;

int main(int argc, char* argv[])
{   
    auto startTime = chrono::high_resolution_clock::now(); 
    for (int i=0; i<123456789; ++i)
    { 
        //for loop for nothing 
    }
    chrono::nanoseconds duration = chrono::high_resolution_clock::now()
                                   - startTime;
    cout << "Total time taken is " 
         << duration.count() 
         << " nanoseconds.\n"; 

    return 0;
}

Sample output:

Total time taken is 387022000 nanoseconds.

Notes:

387022000 nanoseconds
= 387022000 * 10^-9 seconds
= 387022000 * 10^-9 * 10^3 * 10^-3 seconds
= 387022000 * 10^-9 * 10^3 milliseconds
= 387022000 * 10^-6 milliseconds
= 387.022 milliseconds 

No comments:

Measure execution time with Julia, example using sorting algorithms

# random integers between 1 and 100 inclusive, generate thousands of them x = rand ( 1 : 100 , 100000 ) @time sort (x; alg=InsertionSort, r...