I’m trying to pull the two components out of a timeval struct and place them into strings.
I’m not having much luck with this. I’ve attempted casting and converting first to a long and then to a string. I need the most efficient way to do this.
Any ideas? I do NOT want to convert to another data structure first (localtime, etc). I need the seconds and the microseconds in their original state.
EDIT: I know stringstream is an option here — I’m just not sure how efficient that is. Every microsecond counts here, so I’m looking for the fastest implementation.
Boost’s lexical_cast should be reasonably fast.
Edit:
Let me elaborate. Here’s an example of its usage:
For more complicated string formatting, the Boost documentation recommends the basic
std::stringstream. Something like:Reasonably fast, readable, safe and standard. You might be able to get a little more speed by using
sprintffrom the cstdio header. (preferably sprintf_s if available) There’s no explicit support forlongvariables in printf, but these days on 32-bit+ machines they’re usually the same size so you can use the%dspecifier to handle them: