Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Issue using times function in Intel MAC
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="cazabam" data-source="post: 239413" data-attributes="member: 17895"><p>avcabob has a point, that the two are several clock ticks apart (possibly several hundred, depending on what comes back from sysconf. How different are the two numbers that are being returned?</p><p></p><p>However, the immediate problem I see with your implementation is that you have not taken into account the microsecond count. This can account for a difference of up to 1 second of clock ticks. Try this instead:</p><p></p><p>[CODE]</p><p>// Get the output from the times() function for comparison</p><p>clock_t clk;</p><p>struct tms tm;</p><p>clk = times(&tm);</p><p>printf("Output from times(): %u\n", clk);</p><p></p><p>// Get the time in seconds</p><p>struct timeval tv;</p><p>gettimeofday(&tv, NULL);</p><p></p><p>// ... and the ticks per sec</p><p>long ticks_per_sec = sysconf(_SC_CLK_TCK);</p><p></p><p>// The whole seconds ...</p><p>unsigned long ticks = tv.tv_sec * ticks_per_sec;</p><p>printf("Exclude usec portion: %u\n", ticks);</p><p></p><p>// ... and the microsecond portion</p><p>ticks += (ticks_per_sec * tv.tv_usec) / 1000000;</p><p>printf("Include usec portion: %u\n", ticks);</p><p>[/CODE] </p><p></p><p>On my machine, this gives the following output:</p><p></p><p>[CODE]Output from times(): 3764629764</p><p>Exclude usec portion: 3764629704</p><p>Include usec portion: 3764629764[/CODE]</p><p></p><p>As you can see, adding the microsecond portion brought the values together. Of course, it may be a clock tick or two out depending on what else it happening.</p></blockquote><p></p>
[QUOTE="cazabam, post: 239413, member: 17895"] avcabob has a point, that the two are several clock ticks apart (possibly several hundred, depending on what comes back from sysconf. How different are the two numbers that are being returned? However, the immediate problem I see with your implementation is that you have not taken into account the microsecond count. This can account for a difference of up to 1 second of clock ticks. Try this instead: [CODE] // Get the output from the times() function for comparison clock_t clk; struct tms tm; clk = times(&tm); printf("Output from times(): %u\n", clk); // Get the time in seconds struct timeval tv; gettimeofday(&tv, NULL); // ... and the ticks per sec long ticks_per_sec = sysconf(_SC_CLK_TCK); // The whole seconds ... unsigned long ticks = tv.tv_sec * ticks_per_sec; printf("Exclude usec portion: %u\n", ticks); // ... and the microsecond portion ticks += (ticks_per_sec * tv.tv_usec) / 1000000; printf("Include usec portion: %u\n", ticks); [/CODE] On my machine, this gives the following output: [CODE]Output from times(): 3764629764 Exclude usec portion: 3764629704 Include usec portion: 3764629764[/CODE] As you can see, adding the microsecond portion brought the values together. Of course, it may be a clock tick or two out depending on what else it happening. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Issue using times function in Intel MAC
Top