惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
罗磊的独立博客
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LINUX DO - 最新话题
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
T
Tenable Blog
Help Net Security
Help Net Security
WordPress大学
WordPress大学
H
Heimdal Security Blog
SecWiki News
SecWiki News
V2EX - 技术
V2EX - 技术
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
T
Tailwind CSS Blog
L
LINUX DO - 热门话题
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
O
OpenAI News
Latest news
Latest news
月光博客
月光博客
L
LangChain Blog
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
I
InfoQ
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
Forbes - Security
Forbes - Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园_首页
雷峰网
雷峰网
T
Tor Project blog
Security Archives - TechRepublic
Security Archives - TechRepublic
G
GRAHAM CLULEY
Project Zero
Project Zero
MyScale Blog
MyScale Blog
V
V2EX
Vercel News
Vercel News
F
Fortinet All Blogs
The Register - Security
The Register - Security
N
News and Events Feed by Topic
P
Palo Alto Networks Blog
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 司徒正美

博客园 - 空空儿

硬盘分区、寻址和系统启动过程 JavaScript里String.Format方法的实现 asp.net异步页 获取SQL Server数据行的物理地址信息(%%lockress%% & %%physloc%%) 利用Spire.DataExport将数据(Database/DataTable)导成各种文件 使用XML的value()方法将多行数据合并成一列 获取所有的外键信息 ASP.NET application and page life cycle SQL Server 2005 CLR 调用Web Service需要注意的几个问题 SQL SERVRE 2005 CLR TVF错误:从用户定义的表值函数获取新行时出错:Data access is not allowed in this context. PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 在C#里使用属性 - 空空儿 - 博客园 Read text file (txt, csv, log, tab, fixed length) 批量写数据---将XML数据批量写入数据库 对对象类型和调用方法属性进行存储以提升反射性能 过程 sp_addlinkedsrvlogin,第 91 行解密过程中出错的解决办法 用SQL SERVER 2005新提供的命令实现行列转换 APM (异步编程模型) 利用宏让ERStudio生成代码文件
生成大量随机字符串不同实现方式的效率对比
空空儿 · 2008-03-08 · via 博客园 - 空空儿

在26位英文字母中随即选取10个字符组成字符串,产生一定数量的唯一字符串,对比几种方式:
1.使用 System.Security.Cryptography.RNGCryptoServiceProvider 生成 Random 的种子 和 使用普通声称随机数进行对比.
2.使用 IDictionary<TKey , TValue> 其中TKey是 Int 型 存放字符串的HashCode,TValue 是 String 型,存放生成的字符串,通过对比键判断是否项是否已经存在 和 使用 IList<T> 存储字符串进行对比.
3.使用随机截取字符串 和 随机字符串数组索引获取组成字符串.

生成构建 Random 实例种子的方法:

static int GetRandomSeed( )
{
        
byte[] bytes = new byte[4];
        System.Security.Cryptography.RNGCryptoServiceProvider rng 
= new System.Security.Cryptography.RNGCryptoServiceProvider( );
        rng.GetBytes( bytes );
        
return BitConverter.ToInt32( bytes , 0 );
}

生成随机字符串的方法:

static string GetRandomString( )
{
        StringBuilder sbPwd 
= new StringBuilder( );
        Random random 
= new Random( GetRandomSeed( ) );
        
for ( int i = 0 ; i < length ; i++ )
        {
             sbPwd.Append( strSource.Substring( random.Next( 
0 , 25 ) , 1 ) );
             
//sbPwd.Append( sourceArray[random.Next( 0 , 25 )] );
        }
        
return sbPwd.ToString( );
}

对比结果:

1.使用 GetRandomSeed( )方法生成 Random 种子 并使用字符截取 使用IDictionary<int , string> 耗时 20688MS  产生重复项 359  生成项:1000000
2.不使用 GetRandomSeed( )方法生成 Random 种子 并使用字符截取 使用IDictionary<int , string>  耗时 1562547MS  产生重复项 127749442  生成项:100000
3.使用 GetRandomSeed( )方法生成 Random 种子 并使用字符串数组 使用IDictionary<int , string>  耗时36125MS  产生重复项 381   生成项:1000000(使用Char数组效率更低,随机取得Char转换成String时要进行装箱)
4.使用GetRandomSeed( )方法生成 Random 种子 并使用字符截取 使用IList<string> 耗时 214719MS  产生重复项2 生成项:100000(生成项越多耗时越长)

可见使用 System.Security.Cryptography.RNGCryptoServiceProvider 生成 Random 种子 产生的效率要高很多,特别是要连续生成大量的随机数,因为 Random 生成值的重复率非常低.
使用字符串的HashCode对比字符串比直接对比字符串效率要高很多.
使用字符串截取比使用字符串数组效率要高点.