Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 862841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:12:36+00:00 2026-05-15T09:12:36+00:00

I’m trying to pull the two components out of a timeval struct and place

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:12:37+00:00Added an answer on May 15, 2026 at 9:12 am

    Boost’s lexical_cast should be reasonably fast.

    Edit:
    Let me elaborate. Here’s an example of its usage:

    std::string strSeconds = lexical_cast<std::string>(time.tv_sec);
    std::string strMicroSec = lexical_cast<std::string>(time.tv_usec);
    

    For more complicated string formatting, the Boost documentation recommends the basic std::stringstream. Something like:

    std::stringstream ss;
    ss << time.tv_sec << " seconds, " << (time.tv_usec/1000L) << " milliseconds";
    return ss.str();
    

    Reasonably fast, readable, safe and standard. You might be able to get a little more speed by using sprintf from the cstdio header. (preferably sprintf_s if available) There’s no explicit support for long variables in printf, but these days on 32-bit+ machines they’re usually the same size so you can use the %d specifier to handle them:

    std::string tvtostr(timeval time) {
        // unless corrupted, the number of microseconds is always less than 1 second
        assert(time.tv_sec >= 0 && time.tv_usec >= 0 && time.tv_usec < 1000000000L);
        static_assert(sizeof(long)==4 && sizeof(int)==sizeof(long), 
            "assuming 32 bit ints and longs" ); 
    
        // space for one unbounded positive long, one long from 0 to 999,
        // the string literal below, and a '\0' string terminator
        boost::array<CHAR, 10+3+23+1> buffer; 
    
        sprintf_s(buffer.data(), buffer.size(), "%d seconds, %d milliseconds", 
            time.tv_sec, (time.tv_usec/1000L) );
    
        return buffer.data();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.