8

Context: I'm writing a high performance C++11 application, one part of it is to remove inactive connections. For that, I'm storing a "last activity" timestamp in my connection object, which I update when an action is taken. I then have a timer which runs every few seconds, loops over all sessions, and removes inactive ones.

Currently I'm using this code to get the current timestamp:

timestamp = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count()

I'm wondering if there is any faster way to do it? By faster I mean the performance of getting the timestamp itself, not the resolution of the timestamp.

The resolution is not really important for my specific application, it could go as low as a second. Also, utc/local doesn't matter, I am only using the timestamp to compare it to other timestamps, accquired by the same method.

I would like to keep it cross-platform, but platform-specific optimizations with conditional compilation are also welcome.

22
  • 8
    Why do you not simply do timestamp = system_clock::now();?
    – nwp
    Oct 16, 2017 at 13:45
  • 6
    Have you measured that the timestamp-fetching is a bottleneck? Have you examined the (optimized!) generated machine code? Oct 16, 2017 at 13:45
  • 5
    On a 64-bit system thew native word is eight bytes. Copying eight bytes is negligible, even on 32-bit systems. That's why it's so important to measure and check the generated code. Assumptions like yours leads to premature optimizations which are bad. Concentrate on writing good, readable, working and maintainable code. Then measure. And then find the bottlenecks, and only optimize the worst of them, with plenty of comments and documentation and testing. Oct 16, 2017 at 14:00
  • 4
    If performance is really an issue, and accuracy isn't, then you might not need to use a timestamp at all. Rather just keep a counter for each connection, and whenever activity occurs for the connection, reset the counter to zero. Whenever your timer goes off, have it increment the counter for each connection, and disconnect any connection whose counter value rises above (N) (for whatever value of N you find works best) Oct 16, 2017 at 14:01
  • 2
    @GediminasMasaitis In cases like this, it might be better to design an implementation that's easy to change later and simply use a "good enough" approach to differ the optimization work to when you notice a problem. For example, define a type alias or template your code to allow the type of clock used and just use one of the standard clocks. Later you'll be able to change the type of clock used, including a user-defined class with a compatible interface which can encapsulate any alternative solution you find. Most of the time you'll find that you never optimize it cause it's never a problem. Oct 16, 2017 at 14:04

2 Answers 2

5

If performance is really an issue, and accuracy isn't, then you might not need to use a timestamp at all. Rather just keep a counter for each connection, and whenever activity occurs for the connection, reset the counter to zero. Whenever your timer goes off, have it increment the counter for each connection, and disconnect any connection whose counter value rises above (N) (for whatever value of N you find works best)

1
  • 1
    And if you don't want the cleanup task timer precision to matter, have it record the time since it last ran and increment counters by that much. Now you are generating a decent approximation of the OP's solution, and only calculating time once every few seconds on a non-performance criticlal thread. Oct 16, 2017 at 18:15
0

You can maintain a thread local variable per thread, and add a timerfd per thread (or you can use select/epoll timeout parameter) to wake the thread every 1 millsecond, when the select/epoll_wait return, you call now() to get the timestamp, and save the timestamp to the thread local variable.

you can also use a global variable, for example, std::atomic, and update it when select/epoll (in any threads) return. If you still think it's a bottleneck, use a single thread with timerfd and update the timestamp every 1 millsecond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.