簡體   English   中英

每幾毫秒最有效(最快)的方法是什么?

[英]What is the most efficient (fastest) way to write to a file every few milliseconds?

我需要打開一個文件,每隔幾毫秒將一個字節寫入一個文件,然后關閉該文件。 最有效的方法是什么? 目前,這導致CPU使用率很高。

寫入文件最快的方法是使用系統API。 由於您使用的是Windows: https//msdn.microsoft.com/zh-cn/library/windows/desktop/bb540534(v = vs.85).aspx

HANDLE hfile = CreateFile("file",              // name of the write
                       GENERIC_WRITE,          // open for writing
                       0,                      // do not share
                       NULL,                   // default security
                       CREATE_NEW,             // create new file only
                       FILE_ATTRIBUTE_NORMAL,  // normal file
                       NULL);                  // no attr. template
bErrorFlag = WriteFile( 
                    hfile,           // open file handle
                    DataBuffer,      // start of data to write
                    dwBytesToWrite,  // number of bytes to write
                    &dwBytesWritten, // number of bytes that were written
                    NULL);           // no overlapped structure

CloseHandle(hFile);

同樣,在加載應用程序時打開文件,在銷毀應用程序時關閉文件,因為打開和關閉文件很慢。 也就是說,不要每次都需要打開/關閉文件,而是只能打開/關閉一次。

這些功能位於<windows.h>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM