[ create a new paste ] login | about

Link: http://codepad.org/UGubU7cw    [ raw code | output | fork ]

ovanesm - C++, pasted on Mar 1:
#include <boost/array.hpp>
#include <boost/progress.hpp>


#include <locale>
#include <iostream>
#include <sstream>


namespace
{
	std::ostream& print_number(std::ostream& os, long num)
	{
		using namespace std;
		
		use_facet< num_put<char> >(os.getloc()).put(os.rdbuf(), os, ' ', num);
		return os;
	}	

	struct stream_buffer_inisilizer
	{
		stream_buffer_inisilizer(std::iostream& stream, std::streambuf& buf)
			: stream_(stream), old_(stream_.rdbuf(&buf))
		{}

		~stream_buffer_inisilizer()
		{
			stream_.rdbuf(old_);
		}

	private:
		std::iostream&	stream_;
		std::streambuf* old_;
	};

}

int main()
{
	using namespace std;
	using namespace boost;

	const size_t run_loops = 10000000;

	{
		progress_timer pt;

		stringstream ss;
		
		array<char, 4096> buffer; //={0} do not initialize array with 0 if it should be fast ;)

		stringbuf buf;
		buf.pubsetbuf(&buffer[0], buffer.size());

		stream_buffer_inisilizer init(ss, buf);
		for(size_t i=0; i<run_loops; ++i)
		{
			print_number(ss, i);
			//assumption:
			// it is important to have a proper buffer->stream synchronization function
			// at the beginning we deal with smaller number and therefore more number fit
			// into the buffer, afterwards numbers grow and therefore less of them fit in
			// !not sure about that comment!
			if(i%2048==0) 
				buf.pubsync();
		}
	}

	{
		progress_timer pt;
		stringstream ss;
		for(size_t i=0; i<run_loops; ++i)
			ss << i;
	}

	//cout << ss.str();

	return 0;
}


Output:
1
Timeout


Create a new paste based on this one


Comments: