-
October 24th, 2024, 11:59 PM
#1
clock() precision may not be good enough for what I need
Hello,
I am trying to time a section of code where records are processed. I have tried the following, which seems too simple to not work,
Code:
#include <ctime>
// to output row number
int record_count = 0;
// time when processing starts
clock_t start_clock, finish_clock;
// process time for current compound
long unsigned int clocks_to_finish = 0;
while( ***process record*** ) {
// increment record count
record_count++;
// record clock at start of record processing
start_clock = clock();
***
// record clock at end of record processing
finish_clock = clock();
// time to finish current record
clocks_to_finish = finish_clock - start_clock;
// print difference
cout << record_count << " clocks to finish record = " << clocks_to_finish << endl;
}
In most cases, clocks_to_finish is 0. In some cases it is 1. I know that clock() precision isn't great but this program processes 1700+ records in about 1 minute 15 seconds. There should be at least 0.3 seconds or so per record so I was hoping that it would be good enough to provide some information. It may be that there are a couple of records that are taking up allot of the total time and most of them are quite quick. Even so, I should still be able to see a large value for some records.
Is there a solution like GetTickCount() that would provide better precision (I am in Linux)? Is the problem my implementation?
Thanks,
LMHmedchem
-
October 25th, 2024, 12:36 AM
#2
Re: clock() precision may not be good enough for what I need
Possibly the wrong tool for the job.
Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main ( ) {
struct timespec ts_start, ts_end;
printf("Sleep for 2 seconds\n");
clock_t start = clock();
clock_gettime(CLOCK_REALTIME,&ts_start);
sleep(2); // Your workload
clock_gettime(CLOCK_REALTIME,&ts_end);
clock_t end = clock();
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Elapsed CPU seconds=%f\n", elapsed);
double wall = (ts_end.tv_sec-ts_start.tv_sec) * 1000000000.0 +
(ts_end.tv_nsec-ts_start.tv_nsec);
wall /= 1000000000; // back to seconds
printf("Elapsed wall seconds=%f\n", wall);
}
$ ./a.out
Sleep for 2 seconds
Elapsed CPU seconds=0.000044
Elapsed wall seconds=2.000088
The clock() function returns an approximation of processor time used by the program.
If your "***" work involves sending some kind of message to another program to do the actual work, then your program is going to effectively go to sleep and the clock() stops for the duration.
There are many clock types that clock_gettime can use, but all come with various caveats.
You should read the manual page to find out which clock type is best for you.
-
October 27th, 2024, 04:38 AM
#3
Re: clock() precision may not be good enough for what I need
@DerellLicht - The OP specially mentions that the solution is for Linux.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 27th, 2024, 12:28 PM
#4
Re: clock() precision may not be good enough for what I need
Originally Posted by 2kaud
@DerellLicht - The OP specially mentions that the solution is for Linux.
OOPS!! Sorry, I missed that entirely...
nevermind...
-
October 27th, 2024, 02:33 PM
#5
Re: clock() precision may not be good enough for what I need
Try the functions in <chrono>
Code:
#include <chrono>
using namespace std::chrono
//
auto start = high_resolution_clock::now();
// code to time
auto finish = high_resolution_clock::now();
auto diff = finish - start;
// get times in desired units ... examples
auto millisec = std::chrono::duration_cast<std::chrono::milliseconds>(diff);
auto microsec = std::chrono::duration_cast<std::chrono::microseconds>(diff);
auto nanosec = std::chrono::duration_cast<std::chrono::nanoseconds>(diff);
// number of milli seconds in : millisec.count()
// number of micro seconds in : microsec.count()
// number of nano seconds in : nanosec.count()
// for my test routine ...
// number of millisec : 6
// number of microsec : 6752
// number of nanosec : 6752200
Last edited by Philip Nicoletti; November 3rd, 2024 at 05:34 PM.
Reason: fix comment
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|