Skip to main content
added 6 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am trying to find a way to convert printable data types into a string in the fastest way possible.

I originally read that a static variable of stringstreamstringstream that does all of the formatting is one of the fastest ways, since constantly constructing temporary variables of stringstreamstringstream is slow.

The problem with using stringstream.str()stringstream.str() is because it creates a copy of the entire string, which may lead to performance issues, so I made this function to get rid of the need of stringstreamstringstream.str().str()

#include <iostream>
#include <sstream>
#include <streambuf>
#include <vector>
#include <string>

#define ALL(c) (c).begin(), (c).end()

stringstream str_buffer;

string extract()
{
    streambuf& buffer = *str_buffer.rdbuf();
    vector<char> sequence(str_buffer.tellp());
    buffer.sgetn(sequence.data(), str_buffer.tellp());

    str_buffer.seekp(0);
    buffer.pubseekpos(0);
    return string(ALL(sequence));
}

To use this function, just do str_buffer << var; string as_str = extract(); to get var as a string.

However, I have not yet compared timing benchmark results of this function and simply using stringstream.str()stringstream.str(), so I hope that my function is faster if it is in theory. Any suggestions to further improve?

I am trying to find a way to convert printable data types into a string in the fastest way possible.

I originally read that a static variable of stringstream that does all of the formatting is one of the fastest ways, since constantly constructing temporary variables of stringstream is slow.

The problem with using stringstream.str() is because it creates a copy of the entire string, which may lead to performance issues, so I made this function to get rid of the need of stringstream.str()

#include <iostream>
#include <sstream>
#include <streambuf>
#include <vector>
#include <string>

#define ALL(c) (c).begin(), (c).end()

stringstream str_buffer;

string extract()
{
    streambuf& buffer = *str_buffer.rdbuf();
    vector<char> sequence(str_buffer.tellp());
    buffer.sgetn(sequence.data(), str_buffer.tellp());

    str_buffer.seekp(0);
    buffer.pubseekpos(0);
    return string(ALL(sequence));
}

To use this function, just do str_buffer << var; string as_str = extract(); to get var as a string.

However, I have not yet compared timing benchmark results of this function and simply using stringstream.str(), so I hope that my function is faster if it is in theory. Any suggestions to further improve?

I am trying to find a way to convert printable data types into a string in the fastest way possible.

I originally read that a static variable of stringstream that does all of the formatting is one of the fastest ways, since constantly constructing temporary variables of stringstream is slow.

The problem with using stringstream.str() is because it creates a copy of the entire string, which may lead to performance issues, so I made this function to get rid of the need of stringstream.str().

#include <iostream>
#include <sstream>
#include <streambuf>
#include <vector>
#include <string>

#define ALL(c) (c).begin(), (c).end()

stringstream str_buffer;

string extract()
{
    streambuf& buffer = *str_buffer.rdbuf();
    vector<char> sequence(str_buffer.tellp());
    buffer.sgetn(sequence.data(), str_buffer.tellp());

    str_buffer.seekp(0);
    buffer.pubseekpos(0);
    return string(ALL(sequence));
}

To use this function, just do str_buffer << var; string as_str = extract(); to get var as a string.

However, I have not yet compared timing benchmark results of this function and simply using stringstream.str(), so I hope that my function is faster if it is in theory. Any suggestions to further improve?

edited tags
Link
Source Link
ljyip
  • 41
  • 2

Fast Stream Formatting Into String

I am trying to find a way to convert printable data types into a string in the fastest way possible.

I originally read that a static variable of stringstream that does all of the formatting is one of the fastest ways, since constantly constructing temporary variables of stringstream is slow.

The problem with using stringstream.str() is because it creates a copy of the entire string, which may lead to performance issues, so I made this function to get rid of the need of stringstream.str()

#include <iostream>
#include <sstream>
#include <streambuf>
#include <vector>
#include <string>

#define ALL(c) (c).begin(), (c).end()

stringstream str_buffer;

string extract()
{
    streambuf& buffer = *str_buffer.rdbuf();
    vector<char> sequence(str_buffer.tellp());
    buffer.sgetn(sequence.data(), str_buffer.tellp());

    str_buffer.seekp(0);
    buffer.pubseekpos(0);
    return string(ALL(sequence));
}

To use this function, just do str_buffer << var; string as_str = extract(); to get var as a string.

However, I have not yet compared timing benchmark results of this function and simply using stringstream.str(), so I hope that my function is faster if it is in theory. Any suggestions to further improve?