Developing FIFO queue, need something better than time(NULL)

Nov 30, 2012 at 6:48pm
So per the title, I'm developing a FIFO queue (well more also but I just found the problem), and I've realized that time(NULL) is actually not accurate enough for what I'm doing. I have many entries that were actually entered at different times but so close to each other that time(NULL) doesn't pick up on the difference. I'm working on some page replacement algorithms, what do you recommend I use instead?

EDIT:
Nevermind, just used a counter as an artificial timer and all is working.
Last edited on Nov 30, 2012 at 6:58pm
Nov 30, 2012 at 7:09pm
You'll need a system specific type:

*nix

timeval sruct, as used with

int gettimeofday(timeval *tp, NULL);

timeval allows for micro-sec precision, but the system accuracy is prob. more like 10 milli-sec.

Win32/Win64

FILETIME struct, as used by

1
2
3
void WINAPI GetSystemTimeAsFileTime(
  _Out_  LPFILETIME lpSystemTimeAsFileTime
);


The FILETIME struct uses 100-nanosecond intervals, but the accuracy of the time is nowhere near that. Prob. similar to the *nix case (approx. 10 milli-sec)

Andy
Last edited on Nov 30, 2012 at 7:10pm
Nov 30, 2012 at 8:00pm
Thanks for the answer, but I was looking for a system-independent solution, as it needs to run on at least Windows and Linux.
Nov 30, 2012 at 8:33pm
> I was looking for a system-independent solution

> just used a counter as an artificial timer and all is working

Best if all you want to do is time based sequencing.

Otherwise (you want actual timepoints), std::chrono::high_resolution_clock is the standard clock with the smallest period.
http://en.cppreference.com/w/cpp/chrono/high_resolution_clock

1
2
3
4
5
6
7
8
#include <chrono>
#include <iostream>

int main()
{
    const auto period = typename std::chrono::high_resolution_clock::period() ;
    std::cout << "period is: " << period.num << " / " << period.den << " seconds\n" ;
}
Nov 30, 2012 at 8:34pm
Well as of right now I'm only recording number of page faults, though I did not know about the chrono header. I may use this to record more information.
Dec 1, 2012 at 5:03am
> I may use this to record more information.

Prefer std::chrono::steady_clock for this (unless its resolution is not fine enough).

Dec 1, 2012 at 12:59pm
Prefer std::chrono::steady_clock ...

If you have a C++11 compliant compiler.
Dec 1, 2012 at 2:00pm
Or simply clock() for c++98

By the way, time() and clock() measure different things.
I suppose that you want `user' time
Topic archived. No new replies allowed.