C# 将数据写入文件的最有效方法是什么?
我需要在我的应用程序中写入大量数据。数据定期到达并被推入队列C# 将数据写入文件的最有效方法是什么?,c#,winapi,file-io,C#,Winapi,File Io,我需要在我的应用程序中写入大量数据。数据定期到达并被推入队列 //producer queue_.push( new_data );// new_data is a 64kb memory range PostThreadMessage(worker_thread_id_, MyMsg, 0, 0);//notify the worker thread 之后,我需要将这些数据写入文件。我有另一个线程,它从队列中获取数据并将其写入 //consumer while(GetMessage(msg,
//producer
queue_.push( new_data );// new_data is a 64kb memory range
PostThreadMessage(worker_thread_id_, MyMsg, 0, 0);//notify the worker thread
之后,我需要将这些数据写入文件。我有另一个线程,它从队列中获取数据并将其写入
//consumer
while(GetMessage(msg, 0, 0))
{
data = queue_.pop();
WriteFile(file_handle_, data.begin(), data.size(), &io_bytes, NULL);
}
这很简单,但效率不高。
另一个机会是使用IO完成端口
//producer
//file must be associated with io completion port
WriteFile( file_handle_
, new_data.begin()
, new_data.size()
, &io_bytes
, new my_overlaped(new_data) );
消费者只需删除未使用的缓冲区
my_completion_key* ck = 0;
my_overlapped* op = 0;
DWORD res = GetQueuedIOCompletionStatus(completion_port_, &io_bytes, &ck, &op);
//process errors
delete op->data;
在windows上执行大量文件i/o操作的首选方法是什么?很难说,因为目前的方法不清楚您遇到了什么样的问题。在专用“编写器”线程中异步不应导致性能的显著提高 如果您有少量但频繁的写入(因此频繁写入文件的成本成为瓶颈),我建议实现一种延迟写入—收集数据,并在达到某个卷阈值时将其刷新到文件中