牛骨文教育服务平台(让学习变的简单)
博文笔记

C/C++文件操作效率比较——FILE/fstream

创建时间:2016-01-11 投稿人: 浏览次数:2773

利用GetTickTount函数进行效率测试,对比C/C++在读写文本文件和二进制文件时耗时大小,程序执行时间获取方式参见我另一篇C/C++获取程序执行时间的五种方式比较。


电脑配置如下:(CPU四核八线程)


代码如下:

#include <iostream>
#include <fstream>
#include <windows.h>
#include <string.h>
#include <stdio.h>

using namespace std;

void WriteTXTFile_C()
{
	FILE* fp = fopen("c.txt","wt");
	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			fputc("C", fp);
			fputs("Hello, world!
", fp);
		}
	}
	fclose(fp);
}

void ReadTXTFile_C()
{
	FILE* fp = fopen("c.txt","rt");
	char str[15];
	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			fgetc(fp);
			fgets(str, 15, fp);
		}
	}
	fclose(fp);
}

void WriteBINFile_C()
{
	FILE* fp = fopen("c.bin","wb");
	char* str = "CHello, world!
";
	int len = strlen(str);
	
	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			fwrite(str, 1, len, fp);
		}
	}
	fclose(fp);
}

void ReadBINFile_C()
{
	FILE* fp = fopen("c.bin","rb");
	char str[16];
	
	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			fread(str, 1, 16, fp);
		}
	}
	fclose(fp);
}

void WriteTXTFile_CPlus()
{
	fstream file;
	file.open("cp.txt", ios::in | ios::out | ios::trunc);//注意ios::in,或者ios::in | ios::out两种方式在文件不存在时会执行写操作但不会建立文件。

	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			file.put("C");
			file<<"Hello, world!
";
		}
	}
	file.close();
}

void ReadTXTFile_CPlus()
{
	fstream file;
	file.open("cp.txt", ios::in | ios::out);
	char str[15];

	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			file.get(str, 15);
		}
	}
	file.close();
}

void WriteBINFile_CPlus()
{
	fstream file;
	file.open("cp.bin", ios::in | ios::out | ios::trunc | ios::binary);
	char* str = "CHello, world!
";
	int len = strlen(str);

	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			file.write(str, len);
		}
	}
	file.close();
}

void ReadBINFile_CPlus()
{
	fstream file;
	file.open("cp.bin", ios::in | ios::out | ios::binary);
	char str[16];

	for(int i = 0; i < 1000; i++)
	{
		for(int j = 0; j < 1000; j++)
		{
			file.read(str, 16);
		}
	}
	file.close();
}

int main()
{
	DWORD start, end;
	start = GetTickCount();
	WriteTXTFile_C();
	end = GetTickCount();
	printf("C语言写文本文件操作运行时间为:%d ms
", end - start);
	start = GetTickCount();
	WriteTXTFile_CPlus();
	end = GetTickCount();
	cout<<"C++写文本文件操作运行时间为:"<<end - start<<" ms"<<endl;

	cout<<endl;

	start = GetTickCount();
	ReadTXTFile_C();
	end = GetTickCount();
	printf("C语言读文本文件操作运行时间为:%d ms
", end - start);
	start = GetTickCount();
	ReadTXTFile_CPlus();
	end = GetTickCount();
	cout<<"C++读文本文件操作运行时间为:"<<end - start<<" ms"<<endl;

	cout<<endl;

	start = GetTickCount();
	WriteBINFile_C();
	end = GetTickCount();
	printf("C语言写二进制文件操作运行时间为:%d ms
", end - start);
	start = GetTickCount();
	WriteBINFile_CPlus();
	end = GetTickCount();
	cout<<"C++写二进制文件操作运行时间为:"<<end - start<<" ms"<<endl;

	cout<<endl;

	start = GetTickCount();
	ReadBINFile_C();
	end = GetTickCount();
	printf("C语言读二进制文件操作运行时间为:%d ms
", end - start);
	start = GetTickCount();
	ReadBINFile_CPlus();
	end = GetTickCount();
	cout<<"C++读二进制文件操作运行时间为:"<<end - start<<" ms"<<endl;
}

实验结果如下:



总结:针对读写文件中百万行字符串的操作,二进制文件的操作要快于文本文件;写文件的操作要快于读文件;C语言文件指针操作方式要快于C++流式读写文件,其中写操作快近4倍,读操作C的优势不太明显。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。