Code:
// Thread URL: http://cboard.cprogramming.com/showt...5&pagenumber=2
// Program original by: FillYourBrain
// Modified slightly by: Jason Doucette (http://www.jasondoucette.com/)
// Purpose: testing C functions vs Win32 API functions, for speed of file accessing
#include <windows.h>
#include <iostream>
#include <stdio.h>
/*
// FillYourBrain
#define NUM_OPENCLOSE 1000000
#define NUM_LARGEWRITE 1000000
#define NUM_SMALLWRITE 1000000
#define NUM_LARGEREAD 1000000
#define NUM_SMALLREAD 1000000
#define BUFFERSIZE 10000
#define SMALLSIZE 1
*/
// Jason
#define NUM_OPENCLOSE 5000000
#define NUM_LARGEWRITE 10000
#define NUM_SMALLWRITE 10000000
#define NUM_LARGEREAD 2000000
#define NUM_SMALLREAD 10000000
#define BUFFERSIZE 1000000
#define SMALLSIZE 10000
int main(void)
{
DWORD test1, test2, written;
int et1, et2;
struct _iobuf *file_runtime;
HANDLE file_api;
int i;
std::cout << "Large read/writes ues a buffer of " << BUFFERSIZE << " bytes." << std::endl;
std::cout << "Small read/writes ues a buffer of " << SMALLSIZE << " bytes." << std::endl;
std::cout << std::endl;
char *buf = new char[BUFFERSIZE];
//Create the file
file_api = ::CreateFile("C:\\TestFile.dat",
GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
::CloseHandle(file_api);
//--------------------------------------------------------
//OPEN and CLOSE tests.
//runtime
test1 = ::GetTickCount();
for(i=0; i < NUM_OPENCLOSE; i++)
{
file_runtime = fopen("C:\\TestFile.dat", "rb");
fclose(file_runtime);
}
test2 = ::GetTickCount();
et1 = test2-test1;
std::cout << "OPEN/CLOSE runtime " << et1 << std::endl;
//API
test1 = ::GetTickCount();
for(i=0; i < NUM_OPENCLOSE; i++)
{
file_api = ::CreateFile("C:\\TestFile.dat",
GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
::CloseHandle(file_api);
}
test2 = ::GetTickCount();
et2 = test2-test1;
std::cout << "OPEN/CLOSE W32 API " << et2 << std::endl;
std::cout << "Win32 API takes " << et2 * 100 / et1
<< "% the time of the C runtime library." << std::endl;
std::cout << std::endl;
//--------------------------------------------------------
//LARGE WRITE tests
//runtime
test1 = ::GetTickCount();
file_runtime = fopen("C:\\TestFile.dat", "wb");
for(i=0; i < NUM_LARGEWRITE; i++)
{
fwrite(buf, BUFFERSIZE, 1, file_runtime);
fseek(file_runtime, 0, SEEK_SET);
}
fclose(file_runtime);
test2 = ::GetTickCount();
et1 = test2-test1;
std::cout << "LARGE WRITE runtime " << et1 << std::endl;
//API
test1 = ::GetTickCount();
file_api = ::CreateFile("C:\\TestFile.dat",
GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
for(i=0; i < NUM_LARGEWRITE; i++)
{
::WriteFile(file_api, buf, BUFFERSIZE, &written, NULL);
::SetFilePointer(file_api, 0, 0, FILE_BEGIN);
}
::CloseHandle(file_api);
test2 = ::GetTickCount();
et2 = test2-test1;
std::cout << "LARGE WRITE W32 API " << et2 << std::endl;
std::cout << "Win32 API takes " << et2 * 100 / et1
<< "% the time of the C runtime library." << std::endl;
std::cout << std::endl;
//--------------------------------------------------------
//SMALL WRITE tests
//runtime
test1 = ::GetTickCount();
file_runtime = fopen("C:\\TestFile.dat", "wb");
for(i=0; i < NUM_SMALLWRITE; i++)
{
fwrite(buf, SMALLSIZE, 1, file_runtime);
fseek(file_runtime, 0, SEEK_SET);
}
fclose(file_runtime);
test2 = ::GetTickCount();
et1 = test2-test1;
std::cout << "SMALL WRITE runtime " << et1 << std::endl;
//API
test1 = ::GetTickCount();
file_api = ::CreateFile("C:\\TestFile.dat",
GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
for(i=0; i < NUM_SMALLWRITE; i++)
{
::WriteFile(file_api, buf, SMALLSIZE, &written, NULL);
::SetFilePointer(file_api, 0, 0, FILE_BEGIN);
}
::CloseHandle(file_api);
test2 = ::GetTickCount();
et2 = test2-test1;
std::cout << "SMALL WRITE W32 API " << et2 << std::endl;
std::cout << "Win32 API takes " << et2 * 100 / et1
<< "% the time of the C runtime library." << std::endl;
std::cout << std::endl;
//--------------------------------------------------------
//LARGE READ
//runtime
test1 = ::GetTickCount();
file_runtime = fopen("C:\\TestFile.dat", "rb");
for(i=0; i < NUM_LARGEREAD; i++)
{
fread(buf, BUFFERSIZE, 1, file_runtime);
fseek(file_runtime, 0, SEEK_SET);
}
fclose(file_runtime);
test2 = ::GetTickCount();
et1 = test2-test1;
std::cout << "LARGE READ runtime " << et1 << std::endl;
// API
test1 = ::GetTickCount();
file_api = ::CreateFile("C:\\TestFile.dat",
GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
for(i=0; i < NUM_LARGEREAD; i++)
{
::ReadFile(file_api, buf, BUFFERSIZE, &written, NULL);
::SetFilePointer(file_api, 0, 0, FILE_BEGIN);
}
::CloseHandle(file_api);
test2 = ::GetTickCount();
et2 = test2-test1;
std::cout << "LARGE READ W32 API " << et2 << std::endl;
std::cout << "Win32 API takes " << et2 * 100 / et1
<< "% the time of the C runtime library." << std::endl;
std::cout << std::endl;
//--------------------------------------------------------
//SMALL READ
//runtime
test1 = ::GetTickCount();
file_runtime = fopen("C:\\TestFile.dat", "rb");
for(i=0; i < NUM_SMALLREAD; i++)
{
fread(buf, SMALLSIZE, 1, file_runtime);
fseek(file_runtime, 0, SEEK_SET);
}
fclose(file_runtime);
test2 = ::GetTickCount();
et1 = test2-test1;
std::cout << "SMALL READ runtime " << et1 << std::endl;
// API
test1 = ::GetTickCount();
file_api = ::CreateFile("C:\\TestFile.dat",
GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
for(i=0; i < NUM_SMALLREAD; i++)
{
::ReadFile(file_api, buf, SMALLSIZE, &written, NULL);
::SetFilePointer(file_api, 0, 0, FILE_BEGIN);
}
::CloseHandle(file_api);
test2 = ::GetTickCount();
et2 = test2-test1;
std::cout << "SMALL READ W32 API " << et2 << std::endl;
std::cout << "Win32 API takes " << et2 * 100 / et1
<< "% the time of the C runtime library." << std::endl;
std::cout << std::endl;
//--------------------------------------------------------
// clean up
::DeleteFile("C:\\TestFile.dat");
delete [] buf;
return(0);
}