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

  • SEARCH
  • Home
  • 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 7991809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:23:50+00:00 2026-06-04T13:23:50+00:00

The following code converts a std::string to a boost::posix_time::ptime . After profiling I saw

  • 0

The following code converts a std::string to a boost::posix_time::ptime.

After profiling I saw that most of the time spent in that function (about 90%) is wasted with the allocation of the memory for the time_input_facet. I have to admit that I don’t fully understand the following code, and specially why the time_input_facet has to be allocated on the free memory.

using boost::posix_time;

const ptime StringToPtime(const string &zeitstempel, const string &formatstring)
{
    stringstream ss;
    time_input_facet* input_facet = new time_input_facet();
    ss.imbue(locale(ss.getloc(), input_facet));
    input_facet->format(formatstring.c_str());

    ss.str(zeitstempel);
    ptime timestamp;

    ss >> timestamp;
    return timestamp;
}

Do you see any way to get rid of the allocation?

  • 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-06-04T13:23:52+00:00Added an answer on June 4, 2026 at 1:23 pm

    Make the input_facet static inside the function:

    static time_input_facet *input_facet = new time_input_facet();
    

    This will construct the facet only on the first function call and will reuse the facet. I believe that the facet allows multiple consequent calls on the same object.

    Updated: you also don’t need to construct both the stringstream and locale. Just do it once either in a separate function or here in the static initialization, and use the stream consequently.

    Updated2:

    const ptime StringToPtime(const string &zeitstempel, const string &formatstring)
    {
      static stringstream ss;
      static time_input_facet *input_facet = NULL;
      if (!input_facet)
      {
        input_facet = new time_input_facet(1);
        ss.imbue(locale(locale(), input_facet));
      }
    
      input_facet->format(formatstring.c_str());
    
      ss.str(zeitstempel);
      ptime timestamp;
    
      ss >> timestamp;
      ss.clear();
      return timestamp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that takes a double value and converts it to
The following code throws an compile-time error like Cannot convert type 'string' to 'int'
I am trying to do the following: boost::unordered_map<boost::flyweight<std::string>, boost::flyweight<std::string> > map; boost::flyweight<std::string> foo(name); map[foo]
To convert from a system::String to an std::string, I use the following code: IntPtr
At the following first line of code I get this error: cannot convert ‘std::string’
Please see the following code: #include <iostream> #include <string> using namespace std; enum dataType
Consider the following code. #include <iostream> #include <string> struct SimpleStruct { operator std::string ()
I get the following errors with my code. cannot convert parameter 1 from 'std::string'
With the following code, if I attempt to convert a template array to std::string,
I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;

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.