mmap测试

本文通过使用mmap机制进行大规模文件写入操作来测试其性能表现,并记录了整个过程及结果。实验中设置了4千万次循环写入特定字符串到文件的操作,并利用timeb结构记录开始与结束时间,进而计算出所需总时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "mmap_fstream.h"
#include "log.h"
#include <iostream>
#include <ostream>
#include <string.h>
#include <fstream>
#include <time.h>
#include <sys/stat.h>
#include <sys/timeb.h>
#include <fcntl.h>

using namespace std;
using namespace log4cplus;
using namespace log4cplus::helpers;

static Logger logger;

void InitLog();

int main(){

//	InitLog();
	string file_name("mmap_file.txt");
	MMapStream out_stream(file_name.c_str());
//	ofstream out_stream(file_name.c_str());


	struct timeb start_time;
	struct timeb end_time;
	ftime(&start_time);
	uint32_t loop_times = 40000000;
	for(int i = 0; i < loop_times; i++){

//		for(int j = 0; j < loop_times; j++){
//
//			char* buf = "test";
//			char* buff = new char[4];
//			memcpy(buff,buf, strlen(buff));
//			delete[] buff;
//			buff = NULL;
//		}
		char* buffer = "liyongggggggggggggggggggggggg";
		out_stream.write(buffer, strlen(buffer));
	}
	ftime(&end_time);
	cout << "start_time is: " << start_time.millitm << endl;
	cout << "end_time is: " <<  end_time.millitm << endl;
	cout << "time is: " << end_time.millitm - start_time.millitm  << endl;
//	mmap_stream.seekp(4095);
//	std::streamoff current_off = mmap_stream.tellp();
//	char buffer1[] = "liyong";
//	mmap_stream.write(buffer1, strlen(buffer1));
//	char buffer2[] = "LIYONG";
//	mmap_stream.write(buffer2, strlen(buffer2));

//	int fd = open("mmap_file.txt",O_CREAT | O_RDWR, 0644);
//	lseek(fd, 4095, SEEK_SET);
//	char* buf = new char[6];
//	read(fd, buf, 6);
//	cout << string(buf) << endl;
//
//	lseek(fd, 5002, SEEK_SET);
//	read(fd, buf, 5);
//	cout << string(buf) << endl;



}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值