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

利用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!\n", 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!\n";
	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!\n";
		}
	}
	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!\n";
	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\n", 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\n", 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\n", 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\n", end - start);
	start = GetTickCount();
	ReadBINFile_CPlus();
	end = GetTickCount();
	cout<<"C++读二进制文件操作运行时间为:"<<end - start<<" ms"<<endl;
}

实验结果如下:



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

  • 13
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
CGI(Common Gateway Interface)是一种标准,用于在Web服务器上运行外部程序。 CGI程序可以与Web服务器进行通信,从而让Web服务器获取外部程序生成的数据,并将其返回给客户端浏览器。在这个过程中,CGI程序可以读写文件、处理表单数据、查询数据库等。 文件上传是Web应用程序中非常常见的一种功能。上传文件的过程涉及到客户端浏览器将文件数据发送到Web服务器,Web服务器将文件保存到指定的目录中,并将文件相关的信息存储到数据库中。CGI程序可以处理上传文件的请求,并实现文件的保存和数据库的更新等操作。 cgicc是一个C++库,用于处理CGI程序中的表单数据。它提供了一组简单易用的API,可以方便地读取和处理表单数据,并且支持文件上传等功能。使用cgicc可以极大地简化CGI程序的开发。 下面是一个使用cgicc处理文件上传的示例: ```cpp #include <iostream> #include <fstream> #include <cgicc/Cgicc.h> #include <cgicc/HTTPHTMLHeader.h> #include <cgicc/HTMLClasses.h> using namespace std; using namespace cgicc; int main() { Cgicc cgi; const_file_iterator file = cgi.getFile("file"); if(file != cgi.getFiles().end()) { string filename = file->getName(); string filepath = "/var/www/upload/" + filename; ofstream ofs(filepath.c_str(), ios::out | ios::binary); file->writeToStream(ofs); ofs.close(); cout << HTTPHTMLHeader() << endl; cout << HTMLDoctype(HTMLDoctype::eStrict) << endl; cout << html().set("lang", "en").set("dir", "ltr") << endl; cout << head() << title("File Upload Result") << head() << endl; cout << body() << h1("File Upload Result") << endl; cout << p("File " + filename + " uploaded successfully!") << endl; cout << body() << html(); } else { cout << HTTPHTMLHeader() << endl; cout << HTMLDoctype(HTMLDoctype::eStrict) << endl; cout << html().set("lang", "en").set("dir", "ltr") << endl; cout << head() << title("File Upload Result") << head() << endl; cout << body() << h1("File Upload Result") << endl; cout << p("No file uploaded!") << endl; cout << body() << html(); } return 0; } ``` 在这个示例中,我们使用cgicc库处理表单数据,并通过getFile函数获取上传的文件。如果getFile返回的迭代器不等于getFiles返回的迭代器末尾,说明有文件上传。我们可以通过getName获取上传文件的名称,并指定文件保存的路径。然后,我们使用writeToStream将文件写入到指定的文件路径中。 最后,我们输出一个HTML响应,显示文件上传的结果。如果有文件上传成功,输出“File uploaded successfully!”,否则输出“No file uploaded!”。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值