- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
When I come to a clock store, I can see many clocks per second, but not a 1000. Maybe, 14?
Admin
I fail to see this as a WTF. In my opinion it is good practice to use the CLOCKS_PER_SEC define, that's why it was provided. Otherwise you would code to the ASSUMPTION, that there are 1000 clock ticks to the second. Even if the behaviour of clock() probably won't change, it is documented in CLOCKS_PER_SEC, so it should be used.
Admin
I agree with TopTension. clock() is a standard C function, and CLOCKS_PER_SEC is not even guaranteed to be constant across processes on a system (by C; some standards constrain it, like POSIX defining it to always be 1000000).
Admin
Of course nothing stops you doing something like this:
Addendum 2023-03-22 07:25: Well, unless it's a per-process variable, in which case the
#if
won't compile...Admin
Define
and you're golden.Admin
The function is used as documented. The 1000 / 1000 part will be optimized out by the compiler. No WTF here.
WTF would be if they didn't use the CLOCKS_PER_SEC constant and the wonder why it fails to work on a different platform.
Admin
Indeed; it could use a high performance counter or based on CPU clock cycles or weird other internal oscillators. That's by the way why it was defined back in the days in the first place, there were dozens of different implementations for clock() depending on the hardware the program was running :-)
Admin
Looking at "man 3 clock" (https://man7.org/linux/man-pages/man3/clock.3.html) the original author does exactly what the user manual tells him to do.
Most likely
clock()
does not return milliseconds, it returns the number of OS ticks that have passed since boot. MS Windows happens to have a tick length of 1 ms which is fortunate. Other OS-s (or even Windows on other plaforms) are not guaranteed to use a 1 ms tick.Admin
Instead of dividing by CLOCKS_PER_SEC and multiplying by 1000 (MS_PER_SEC), they should define and use a constant CLOCKS_PER_MS, set to (at compile time) CLOCKS_PER_SEC / 1000, and in this case, equals 1.
Admin
I agree with lots of other people. This is how
clock()
is documented on my (macOS) system:I'd write the line of code in exactly the same way, except I'd define a constant to eliminate the hard coded
1000
as suggested by prueg.Admin
Emphasis there on "variable". The C11 standard defines it thus:
It doesn't even say "constant expression". By the C standard, there's no guarantee that two usages of it will have the same value. There's nothing (in the C standard) to say a C library can't implement a function that allows you to change
CLOCKS_PER_SEC
.Admin
Made something similar myself once. Only it wasn't about system clock, it was about sampling rate of some data. That piece was vital, because some processing functions worked with sampling points, while other only knew seconds or milliseconds - these all were automatically converted when working through GUI, but coding a macro was a pain in the ass.
Admin
CLOCKS_PER_SEC / 1000
could run into round-off error. It's better to keep the division and multiplication separate.Admin
I'm willing to best that this is one of the mildest things that would break at this point if they ever try to update to a newer version with different behavior for some of those constants/functions.
Admin
It's not a big problem when you make it a constant with a
double
value.Admin
TRWTF is all the people proposing "#define MS_PER_SEC 1000"
Will the second ever have a different number of milliseconds in it?
Defining it as a symbol just gives your brain something else to read and parse whereas 1000 is easily understood as "milli".
Admin
I remember when clocks was tied to the LTC [AC mains frequency] on certain systems... So CLOCKS_PER_SEC was 50 or 60 depending on compilation local < ahh fun days of early 1980's embedded systems]
Admin
And of course all of us (including me) proposing to remove the divide-and-multiply if CLOCKS_PER_SEC is exactly 1000 are wrong, because it changes the semantics of the expression by eliminating the round-down aspect.
Admin
It is a problem if you just declare the constant as double since type conversions happens after the integer division. You'd need CLOCKS_PER_SEC / 1000.0 or an explicit cast before the division. And, as has been mentioned, it's not necessarily a constant. You could write a function returning this value. Yeah, that's so much better than the original code.
Admin
Nope, there's a cast to double before the division.
Wow, so that's 3 Non-WTFs in the posted code, compared to the article and comments. Must be a new record!
Admin
Yup, not a WTF, that's how CLOCKS_PER_SEC is supposed to be used.
Admin
Re the easy-reader note, I do make a practice of including units in the variable name when the value is morally dimensioned but is a floating-point var for whatever reason. It happens semi-frequently in engineering software.