[Spice-devel] [PATCH spice-streaming-agent 2/9] FrameLog: Use std::chrono instead of gettimeofday for time

Lukáš Hrázký lhrazky at redhat.com
Thu Jun 14 09:57:00 UTC 2018


On Thu, 2018-06-14 at 04:45 -0400, Frediano Ziglio wrote:
> > 
> > Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
> 
> Patch looks good but would be good to state the reasons.
> Portability?
> Just style?

I just prefer to use std::chrono since it's provided by the standard
library. It's not just style, it's about it being idiomatic in C++11.

> Has the new implementation the same precision and efficiency?

I haven't done an awful lot of research, but the system_clock has
microsecond precision and from my quick test it does output very
similar numbers to the gettimeofday when called one right after the
other.

The efficiency should be on par, haven't tested that on my system,
google searching didn't turn up any issues with it.

I think in these days the C++ standard library implementations should
be of sufficient quality :)

I'll extend the commit message then.

> > ---
> >  src/frame-log.cpp | 9 ++++-----
> >  1 file changed, 4 insertions(+), 5 deletions(-)
> > 
> > diff --git a/src/frame-log.cpp b/src/frame-log.cpp
> > index b0bd09e..4418d09 100644
> > --- a/src/frame-log.cpp
> > +++ b/src/frame-log.cpp
> > @@ -9,9 +9,9 @@
> >  #include "error.hpp"
> >  #include "hexdump.h"
> >  
> > +#include <chrono>
> >  #include <cstdarg>
> >  #include <string.h>
> > -#include <sys/time.h>
> >  
> >  
> >  namespace spice {
> > @@ -67,10 +67,9 @@ void FrameLog::log_frame(const void* buffer, size_t
> > buffer_size)
> >   */
> >  uint64_t FrameLog::get_time()
> >  {
> > -    struct timeval now;
> > -    gettimeofday(&now, NULL);
> > -
> > -    return (uint64_t)now.tv_sec * 1000000 + (uint64_t)now.tv_usec;
> > +    auto now = std::chrono::system_clock::now().time_since_epoch();
> > +    return
> > std::chrono::duration_cast<std::chrono::microseconds>(now).count();
> >  }
> >  
> > +
> 
> Spurious line change

Oops.

> >  }} // namespace spice::streaming_agent
> 
> Frediano


More information about the Spice-devel mailing list