.NET 中 MD5 生成的成本有多高?
为了与外部数据源交互,我需要传递一个经过 MD5 哈希处理的滚动安全密钥(每天我们需要生成一个新的 MD5 哈希密钥)。
每次我们调用外部提要时,我都会交换是否执行此操作。 我需要一个大约 10 个字符的字符串作为提要。
它适用于 ASP.NET (C#/ .NET 3.5) 站点,几乎每个页面都使用该提要。 我最好每天生成一次哈希值,然后将其存储在应用程序缓存中,并获取内存命中,还是在每个请求时生成它?
To interact with an external data feed I need to pass a rolling security key which has been MD5 hashed (every day we need to generate a new MD5 hashed key).
I'm trading up whether or not to do it every time we call the external feed or not. I need to has a string of about 10 characters for the feed.
It's for an ASP.NET (C#/ .NET 3.5) site and the feed is used on pretty much every page. Would I best off generating the hash once a day and then storing it in the application cache, and taking the memory hit, or generating it on each request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
唯一可接受的优化基础是数据。 测量生成此内联并测量缓存它。
我的高端工作站可以在一秒钟内计算 10 字节数据段的超过 100k MD5 哈希值。 对我来说缓存这个好处是零,我敢打赌对你来说也是一样的。
The only acceptable basis for optimizations is data. Measure generating this inline and measure caching it.
My high-end workstation can calculate well over 100k MD5 hashes of a 10-byte data segment in a second. There would be zero benefit from caching this for me and I bet it's the same for you.
生成一些样本数据。 嗯,很多。 计算样本数据的MD5。 测量所需的时间。 自己决定。
Generate some sample data. Well, a lot of it. Compute the MD5 of the sample data. Measure the time it takes. Decide for yourself.
计算算法的时间复杂度!
看下面的代码:
如果我们要计算时间复杂度,我们会得到 T= 11 + n * 2 但这只是“我们看到的”,即 ToLower可能会做一些我们不知道的繁重工作。 但从这一点我们可以看出,这个算法在所有情况下都是 O(n) 的。 这意味着时间随着数据的增长而增长。
另外为了解决缓存问题,我宁愿在内存中进行“繁重”工作,因为与 CPU 使用相比,内存更便宜。
Calculate the time complexity of the algorithm!
Look at the following code:
If we were to calculate the time complexity we would get T= 11 + n * 2 however this is just "what we see" i.e. ToLower might do some heavy work which we don't know. But from this point we can see that this algorithm is O(n) in all cases. Meaning time grows as data growns.
Also to adress the cache issue, I'd rather have my "heavy" work in Memory since memory is less expensive when compared to CPU-usage.
如果某一天的缓存是相同的,那么这可能是一个想法。 您甚至可以将缓存设置为 24 小时,并编写代码在缓存过期时重新生成哈希
If it'll be the same for a given day caching it might be an idea. You could even set the cache to be 24 hours and write code to regenerate the hash when the cache expires
使用 Asp.Net 缓存非常简单,所以我不明白为什么你不应该缓存密钥。
将密钥存储在缓存中甚至可以节省一些内存,因为您可以重复使用它,而不是为每个请求创建一个新密钥。
Using the Asp.Net cache is very easy so I don't see why you shouldn't cache the key.
Storing the key in cache may even save some memory since you can reuse it instead of creating a new one for each request.