时间:2019-05-17 标签:c#refforspeed
我完全了解 .NET 中的 ref 字
,因为使用相同的变量,使用 ref 而不是进行复制会提高速度吗?
我发现瓶颈在于一般密码。
这是我的代码
protected internal string GetSecurePasswordString(string legalChars, int length)
{
Random myRandom = new Random();
string myString = "";
for (int i = 0; i < length; i++)
{
int charPos = myRandom.Next(0, legalChars.Length - 1);
myString = myString + legalChars[charPos].ToString();
}
return myString;
}
最好在合法字符之前参考?
I understand full the ref word in the .NET
Since using the same variable, would increase speed to use ref instead of making copy?
I find bottleneck to be in password general.
Here is my codes
protected internal string GetSecurePasswordString(string legalChars, int length)
{
Random myRandom = new Random();
string myString = "";
for (int i = 0; i < length; i++)
{
int charPos = myRandom.Next(0, legalChars.Length - 1);
myString = myString + legalChars[charPos].ToString();
}
return myString;
}
is better to ref before legalchars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
按值传递字符串不会复制该字符串。它仅复制对字符串的引用。通过引用而不是通过值传递字符串没有任何性能优势。
Passing a string by value does not copy the string. It only copies the reference to the string. There's no performance benefit to passing the string by reference instead of by value.
不,您不应该通过引用传递字符串引用。
然而,您毫无意义地创建了多个字符串。如果您要创建长密码,这可能就是它成为瓶颈的原因。这是一个更快的实现:
但是,它仍然存在三个大缺陷:
Random
实例。如果您快速连续调用此方法两次,您将获得两次相同的密码。坏主意。Random.Next()
调用中指定的上限是独占的 - 因此您永远不会使用legalChars
的最后一个字符。System.Security.Cryptography.RandomNumberGenerator
。这样做需要做更多的工作,因为 API 更难,但您最终会得到一个更安全的系统(如果做得正确的话)。您可能还需要考虑使用 SecureString,如果您变得真正偏执。
No, you shouldn't pass the string reference by reference.
However, you are creating several strings pointlessly. If you're creating long passwords, that could be why it's a bottleneck. Here's a faster implementation:
However, it still has three big flaws:
Random
each time. If you call this method twice in quick succession, you'll get the same password twice. Bad idea.Random.Next()
call is exclusive - so you'll never use the last character oflegalChars
.System.Random
, which is not meant to be in any way cryptographically secure. Given that this is meant to be for a "secure password" you should consider using something likeSystem.Security.Cryptography.RandomNumberGenerator
. It's more work to do so because the API is harder, but you'll end up with a more secure system (if you do it properly).You might also want to consider using SecureString, if you get really paranoid.
.Net 中的字符串是不可变的,因此对字符串的所有修改操作总是会导致新字符串的创建(和垃圾回收)。在这种情况下使用 ref 不会获得任何性能增益。相反,请使用 StringBuilder。
strings in .Net are immutable , so all modify operations on strings always result in creation ( and garbage collection) of new strings. No performance gain would be achieved by using ref in this case. Instead , use StringBuilder.
关于传递字符串 ByReference (“ref”) 而不是 ByValue 的一般性能增益:
有性能增益,但非常小!
考虑下面的程序,其中调用了一个函数 10.000 .0000 次,使用按值和按引用的字符串参数。测量的平均时间为
ByValue:249 毫秒
ByReference:226 毫秒
一般来说,“ref”要快一些,但通常不值得担心。
这是我的代码:
A word about the general performance gain of passing a string ByReference ("ref") instead of ByValue:
There is a performance gain, but it is very small!
Consider the program below where a function is called 10.000.0000 times with a string argument by value and by reference. The average time measured was
ByValue: 249 milliseconds
ByReference: 226 milliseconds
In general "ref" is a little faster, but often it's not worth worrying about it.
Here is my code: