CreateFile()的成本是多少?

时间:2022-09-25 16:56:29

I want to write data in a file frequently ,

我想经常在文件中写入数据,

I wonder if I call CreateFile() each time I write it,

我想知道每次写这个时我都调用CreateFile(),

Does this way cost so much than the way that I just call it once ,then

这种方式的成本是否比我刚才调用它的方式要多得多

use the handle many times?

多次使用手柄?

5 个解决方案

#1


If you are going to write to the file several times, it would be better to keep the handle returned by CreateFile(). Creating a kernel handle to a file will always be more inefficient that reusing the old handle (all other things being equal), but there may be other considerations to take into account. As always, you should measure the performance before and after you do any such optimizations.

如果要多次写入文件,最好保留CreateFile()返回的句柄。创建文件的内核句柄对于重用旧句柄(所有其他条件相同)总是效率较低,但可能还需要考虑其他因素。与往常一样,您应该在执行任何此类优化之前和之后测量性能。

#2


CreateFile will certainly take some time for each call (I'd guess microseconds if you're opening the same file again many times, thanks to driver and OS buffering, but you should measure with a little benchmark if that's crucial to you). But keeping the file open indefinitely might have unpleasant results in the case of a program or system crash in the middle of operations -- whether that's the case depends on what file format you're dealing with, and what write operations you are performing on it.

CreateFile肯定会花费一些时间来进行每次调用(如果你再次打开同一个文件,我会猜几秒,这要归功于驱动程序和操作系统缓冲,但如果这对你来说至关重要,你应该用一点基准测量)。但是,如果程序或系统在操作过程中发生崩溃,那么无限期地保持文件打开可能会产生令人不快的结果 - 这种情况是否取决于您正在处理的文件格式以及您正在对其执行的写入操作。

#3


Keeping the handle open is not necessarily the best approach. Keeping an open handle can be problematic, depending on your design. While CreateFile certainly has overhead, I'd be very surprised if it weren't negligible compared to the cost of the I/O operations themselves. That being said, the way to be sure is to try both ways and measure the difference. If it's negligible, as I suspect, then use whatever approach works best for you.

保持手柄打开不一定是最好的方法。根据您的设计,保持打开手柄可能会有问题。虽然CreateFile肯定有开销,但如果与I / O操作本身的成本相比,它不可忽略,我会感到非常惊讶。话虽如此,确定的方法是尝试两种方式并衡量差异。如果它可以忽略不计,我怀疑,那么使用最适合你的方法。

#4


Reusing the file handle will give you better performance and is a better design. Most likely the disk caching of the operating system will hide many of the performance hits. I am not sure why you would want to repeatedly call CreateFile() unless you are accessing it from different application areas and that makes it difficult to pass the handle along.

重用文件句柄将为您提供更好的性能,并且是更好的设计。最有可能的是,操作系统的磁盘缓存将隐藏许多性能命中。我不知道你为什么要重复调用CreateFile(),除非你从不同的应用程序区域访问它并且这使得难以传递句柄。

#5


There is an overhead in creating handles to files and you are better off creating one handle and keeping that handle as long as you want to read/write to the file. It depends on how much you are doing this, are you doing it 100 times each second or once every minute?

创建文件句柄会产生开销,只要您想要读取/写入文件,最好创建一个句柄并保留该句柄。这取决于你做了多少,你是每秒做100次还是每分钟做一次?

#1


If you are going to write to the file several times, it would be better to keep the handle returned by CreateFile(). Creating a kernel handle to a file will always be more inefficient that reusing the old handle (all other things being equal), but there may be other considerations to take into account. As always, you should measure the performance before and after you do any such optimizations.

如果要多次写入文件,最好保留CreateFile()返回的句柄。创建文件的内核句柄对于重用旧句柄(所有其他条件相同)总是效率较低,但可能还需要考虑其他因素。与往常一样,您应该在执行任何此类优化之前和之后测量性能。

#2


CreateFile will certainly take some time for each call (I'd guess microseconds if you're opening the same file again many times, thanks to driver and OS buffering, but you should measure with a little benchmark if that's crucial to you). But keeping the file open indefinitely might have unpleasant results in the case of a program or system crash in the middle of operations -- whether that's the case depends on what file format you're dealing with, and what write operations you are performing on it.

CreateFile肯定会花费一些时间来进行每次调用(如果你再次打开同一个文件,我会猜几秒,这要归功于驱动程序和操作系统缓冲,但如果这对你来说至关重要,你应该用一点基准测量)。但是,如果程序或系统在操作过程中发生崩溃,那么无限期地保持文件打开可能会产生令人不快的结果 - 这种情况是否取决于您正在处理的文件格式以及您正在对其执行的写入操作。

#3


Keeping the handle open is not necessarily the best approach. Keeping an open handle can be problematic, depending on your design. While CreateFile certainly has overhead, I'd be very surprised if it weren't negligible compared to the cost of the I/O operations themselves. That being said, the way to be sure is to try both ways and measure the difference. If it's negligible, as I suspect, then use whatever approach works best for you.

保持手柄打开不一定是最好的方法。根据您的设计,保持打开手柄可能会有问题。虽然CreateFile肯定有开销,但如果与I / O操作本身的成本相比,它不可忽略,我会感到非常惊讶。话虽如此,确定的方法是尝试两种方式并衡量差异。如果它可以忽略不计,我怀疑,那么使用最适合你的方法。

#4


Reusing the file handle will give you better performance and is a better design. Most likely the disk caching of the operating system will hide many of the performance hits. I am not sure why you would want to repeatedly call CreateFile() unless you are accessing it from different application areas and that makes it difficult to pass the handle along.

重用文件句柄将为您提供更好的性能,并且是更好的设计。最有可能的是,操作系统的磁盘缓存将隐藏许多性能命中。我不知道你为什么要重复调用CreateFile(),除非你从不同的应用程序区域访问它并且这使得难以传递句柄。

#5


There is an overhead in creating handles to files and you are better off creating one handle and keeping that handle as long as you want to read/write to the file. It depends on how much you are doing this, are you doing it 100 times each second or once every minute?

创建文件句柄会产生开销,只要您想要读取/写入文件,最好创建一个句柄并保留该句柄。这取决于你做了多少,你是每秒做100次还是每分钟做一次?