The illustration would have been better, if I'd put repetitions into the loop rather than leaving my hard-coded value. Try the following with command line parameters 1024, 2048 and 4096. 1024 repeatitions of a string 1024 characters long fits into the large 1M buffer, 2048 repetitions requires one flush of the buffer. You should get the idea.
It is informative - this time 8-)
--------8<--------
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
int main(int argc,const char* argv[])
{
if (argc != 2)
return std::cerr << "Usage: " << *argv << " {repetitions}\n\n\tTry 1024,2048,4096",1;
int repetitions = atoi(*++argv);
// String to log
std::vector<char> test(1025,'x');test[1024] = '\0';
// Normal logging
{
DWORD start = GetTickCount();
{
std::ofstream fout("logfile_normal.txt")
DWORD start = GetTickCount();
for (int i = 0;i < repetitions;i++)
fout << &test[0];
DWORD finish = GetTickCount();
std::cout << "Normal run took " << finish-start << " millisecs\n";
}
DWORD finish = GetTickCount();
std::cout << "Normal run took " << finish-start << " millisecs including ctor and dtor\n";
}
// Fast logging
{
DWORD start = GetTickCount();
const std::streamsize BufferSize = 1024*1024; /* Buffer size of 1M */
std::vector<char> buf(BufferSize);
{
std::ofstream fout("logfile_fast.txt");
fout.rdbuf()->pubsetbuf(&b
DWORD start = GetTickCount();
for (int i = 0;i < repetitions;i++)
fout << &test[0];
DWORD finish = GetTickCount();
std::cout << "Fast run took " << finish-start << " millisecs\n";
}
DWORD finish = GetTickCount();
std::cout << "Fast run took " << finish-start << " millisecs including ctor and dtor\n";
}
}
--------8<--------
Main Topics
Browse All Topics





by: rstaveleyPosted on 2004-08-13 at 02:38:29ID: 11791312
ifstream and ofstream are streams, which means that they buffer and therefore ought to be fast compared with unbuffered file reads/writes. You could increase the size of the buffer, I guess. Try the following, which you'll need to tweak for UN*X, because it uses a Windoze timer:
;
uf[0],buf. size());
--------8<--------
#include <windows.h>
#include <iostream>
#include <fstream>
#include <vector>
int main(int argc,const char* argv[])
{
if (argc != 2)
return std::cerr << "Usage: " << *argv << " {repetitions}\n\n\tTry 1024,2048,4096",1;
int repetitions = atoi(*++argv);
// String to log
std::vector<char> test(1025,'x');test[1024] = '\0';
// Normal logging
{
DWORD start = GetTickCount();
{
std::ofstream fout("logfile_normal.txt")
DWORD start = GetTickCount();
for (int i = 0;i < 1024;i++)
fout << &test[0];
DWORD finish = GetTickCount();
std::cout << "Normal run took " << finish-start << " millisecs\n";
}
DWORD finish = GetTickCount();
std::cout << "Normal run took " << finish-start << " millisecs including ctor and dtor\n";
}
// Fast logging
{
DWORD start = GetTickCount();
const std::streamsize BufferSize = 1024*1024; /* Buffer size of 1M */
std::vector<char> buf(BufferSize);
{
std::ofstream fout("logfile_fast.txt");
fout.rdbuf()->pubsetbuf(&b
DWORD start = GetTickCount();
for (int i = 0;i < 1024;i++)
fout << &test[0];
DWORD finish = GetTickCount();
std::cout << "Fast run took " << finish-start << " millisecs\n";
}
DWORD finish = GetTickCount();
std::cout << "Fast run took " << finish-start << " millisecs including ctor and dtor\n";
}
}
--------8<--------
Notice that you get a performance hit whenever the buffer gets flushed to disk, which is why I added a timer for the dtor in each case.