C#中:什么是生成一个唯一的文件名的最快方法?文件名、最快、方法

由网友(若相守则卟离)分享简介:我已经在随机命名的文件,包括使用见过几个建议I've seen several suggestions on naming files randomly, including using System.IO.Path.GetRandomFileName()或使用System.Guid和附加文件扩展名。 我的问题...

我已经在随机命名的文件,包括使用见过几个建议

I've seen several suggestions on naming files randomly, including using

System.IO.Path.GetRandomFileName()

或使用

System.Guid

和附加文件扩展名。

我的问题是:?什么是生成一个唯一的文件名的最快方法

推荐答案

一个GUID是非常快的,,因为它的实现保证Windows可以产生至少16,384 GUIDS在100纳秒的时间跨度。 (正如其他人所指出的,该规范并不能保证,只允许。但是,GUID生成是真的,真快,真的。)发生碰撞的可能性在任何文件系统上的任何地方任何网络非常低。这是足够安全,虽然它会是最好的做法,经常检查,看看是否该文件名可无论如何,在现实中你永远不会甚至需要做到这一点。

A GUID would be extremely fast, since it's implementation guarantees Windows can generate at least 16,384 GUIDS in a 100-nanosecond timespan. (As others pointed out, the spec doesn't guarantee, only allows for. However, GUID generation is really, really fast. Really.) The likelihood of collision on any filesystem anywhere on any network is very low. It's safe enough that although it'd be best practice to always check to see if that filename is available anyway, in reality you would never even need to do that.

所以,你看没有I / O操作,除了自己救自己,和< 0.2毫秒(在测试机)来生成名称本身。 pretty的快。

So you're looking at no I/O operations except the save itself, and <0.2 milliseconds (on a test machine) to generate the name itself. Pretty fast.

阅读全文

相关推荐

最新文章