Core function

    1. struct timespec ts_now, ts_sec;
    2. if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) { cout << "Error reading the time" << endl; }
    3. auto time1_ns = (long long) ts_now.tv_sec * 1000000000 + (long long) ts_now.tv_nsec;

    Sample Example: Check should we uncomment print

    1. #include <iostream>
    2. #define test=false;
    3. using namespace std;
    4. int main() {
    5. int nonsense = 0;
    6. struct timespec ts_now, ts_sec;
    7. if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) { std::cout << "Error reading the time" << std::endl; }
    8. auto ts_now_long = (long long) ts_now.tv_sec * 1000000000 + (long long) ts_now.tv_nsec;
    9. //submit the order
    10. for(auto _ = 0; _ < 100000; _++){
    11. nonsense += 1;
    12. #ifdef FULLLOG
    13. std::cout << "test" << endl;
    14. #endif
    15. #if false
    16. cout <<"tes" << endl;
    17. #endif
    18. if(false)
    19. {
    20. printf("Test");
    21. }
    22. }
    23. if (clock_gettime(CLOCK_MONOTONIC, &ts_sec)) { std::cout << "Error reading the time" << std::endl; }
    24. auto ts_sec_long = (long long) ts_sec.tv_sec * 1000000000 + (long long) ts_sec.tv_nsec;
    25. cout << "nonsense is " << nonsense << endl;
    26. cout << "Time spend with if(false) is " << ts_sec_long - ts_now_long << endl;
    27. if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) { std::cout << "Error reading the time" << std::endl; }
    28. ts_now_long = (long long) ts_now.tv_sec * 1000000000 + (long long) ts_now.tv_nsec;
    29. int nonsense2 = 0;
    30. //submit the order
    31. for(auto _ = 0; _ < 100000; _++){
    32. nonsense2 += 1;
    33. }
    34. if (clock_gettime(CLOCK_MONOTONIC, &ts_sec)) { std::cout << "Error reading the time" << std::endl; }
    35. ts_sec_long = (long long) ts_sec.tv_sec * 1000000000 + (long long) ts_sec.tv_nsec;
    36. cout << "Time spend is with empty while loop " << ts_sec_long - ts_now_long << endl;
    37. cout << "nonsense is " << nonsense << endl;
    38. }