5. The program is implemented as follows (very simple, no longer explained):
WriteFileDemo.cpp: Defines the entry point of the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace Std;
#include <windows.h>
#include <WinBase.h>
#include <ctime>
Target Write file
Const char* G_psoutfile = "D:\\test_file\\1.txt";
Buffer size for one write
Const long write_buff_size = 10*1024*1024; 300MB bytes
Number of writes
#define MAX_WRITE_CNT 5
Buffer each byte is initialized to the ' a ' character
void Initbuf (char* pszbuf, int icnt)
{
for (int i = 0; i < icnt; ++i)
{
Pszbuf[i] = 97;
}
}
Loop Write file
void Writefilefun (char* szbuf)
{
Ofstream ofout (G_psoutfile);
int i = 0;
while (I < max_write_cnt)
{
Ofout << szbuf << Endl;
++i;
}
}
Test Write disk speed
void Writefiletestfun ()
{
Heap memory request, obviously stack memory inappropriate
char* sztenmbbuf = (char*) malloc (write_buff_size);
(void) Initbuf (sztenmbbuf, write_buff_size);
size_t nbeginticks = GetTickCount ();
cout << "BeginTime =" << nbeginticks << Endl;
(void) Writefilefun (SZTENMBBUF);
size_t nendticks = GetTickCount ();
cout << "EndTime =" << nendticks << Endl;
size_t Nspan = nendticks-nbeginticks;
cout << "nspantime =" << nspan << "MS" << Endl; Ms
float ntotalbytes = write_buff_size*max_write_cnt; Total number of bytes written
float ntotaltimes = (float) (nspan)/1000.0f; Total time spent, unit s
cout << "ntotalwritebytes =" << ntotalbytes << Endl;
cout << "ntotaltimes =" << ntotaltimes << Endl;
float fspeed = ntotalbytes/ntotaltimes;
cout << "Speed =" << fspeed << "byte/s" << Endl; Write Speed byte/s
cout << "Speed =" << fspeed/1024.0f/1024.0f << "MB/S" << Endl; Write Speed mbyte/s
if (NULL! = sztenmbbuf)
{
Free (SZTENMBBUF);
Sztenmbbuf = NULL;
}
}
int _tmain (int argc, _tchar* argv[])
{
(void) writefiletestfun ();
return 0;
}
Test technical training: How to test disk write Speed 2