64,652
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
const int BufSize = 1024 * 1024 * 2;
char* buffer = new char[BufSize];
for (int i = 0; i < 10; ++i)
{
cout<<"test "<<i<<endl;
DWORD s = GetTickCount();
ifstream ifile1("test.dat", ios::binary);
ofstream ofile1("cpp.dat", ios::binary);
while (true)
{
ifile1.read(buffer, BufSize);
ofile1.write(buffer, ifile1.gcount());
if (ifile1.eof())
break;
}
ifile1.close();
ofile1.close();
cout<<"cpp time : "<<GetTickCount() - s<<endl;
s = GetTickCount();
FILE* ifile2 = fopen("test.dat", "rb");
FILE* ofile2 = fopen("c.dat", "wb");
fseek (ifile2, 0L, SEEK_END);
size_t lSize =ftell(ifile2);
rewind(ifile2);
char * buffer1;
buffer1 = (char*) malloc (sizeof(char)*lSize);
if (buffer1 == NULL)
{
fputs ("Memory error",stderr);
exit (2);
}
long result = fread (buffer1,sizeof(char),lSize,ifile2);
if (result != lSize)
{
fputs ("Reading error",stderr);
exit (3);
}
fwrite(buffer1, sizeof(char),lSize, ofile2);
fclose(ifile2);
fclose(ofile2);
cout<<"c time : "<<GetTickCount() - s<<endl;
cout<<endl;
}
delete[] buffer;
return 0;
}
#include "iostream"
#include "stdlib.h"
#include "fstream"
#include "windows.h"
using namespace std;
int main() {
const int BufSize = 1024 * 1024 * 2;
char* buffer = new char[BufSize];
for (int i = 0; i < 10; ++i)
{
cout<<"test "<<i<<endl;
DWORD s = GetTickCount();
ifstream ifile1("test.dat", ios::binary);
ofstream ofile1("cpp.dat", ios::binary);
while (true)
{
ifile1.read(buffer, BufSize);
ofile1.write(buffer, ifile1.gcount());
if (ifile1.eof())
break;
}
ifile1.close();
ofile1.close();
cout<<"cpp time : "<<GetTickCount() - s<<endl;
s = GetTickCount();
FILE* ifile2 = fopen("test.dat", "rb");
FILE* ofile2 = fopen("c.dat", "wb");
while (true)
{
size_t size = fread(buffer, 1, BufSize, ifile2);
fwrite(buffer, size, 1, ofile2);
if (feof(ifile2))
break;
}
fclose(ifile2);
fclose(ofile2);
cout<<"c time : "<<GetTickCount() - s<<endl;
cout<<endl;
}
delete[] buffer;
return 0;
}
测试文件 35.0 MB (36,758,319 字节)
结果
test 0
cpp time : 2109
c time : 719
test 1
cpp time : 734
c time : 1031
test 2
cpp time : 2219
c time : 953
test 3
cpp time : 813
c time : 1203
test 4
cpp time : 953
c time : 515
test 5
cpp time : 579
c time : 890
test 6
cpp time : 1235
c time : 1468
test 7
cpp time : 782
c time : 671
test 8
cpp time : 563
c time : 875
test 9
cpp time : 1344
c time : 1359
请按任意键继续. . .