你一定要明白,你不认为你正在做的基本上是产生基础-94数字的序列...你正在生成的字符串的领域得到变化大,速度非常快。例如,使用的字符集(0×21 –的0x7E),有
8836可能(94 )2字符字符串
830584可能的(94 )3-字符串
78074896可能(94 )4字符串
等
通过你已经打了6个字符的时候,你看94 或689,869,781,056个字符串来生成。
你建立你的字符串的方式很可能是......一点点硬的醇”堆和字符串实习生表。对于初学者来说,我会标记程序集不要通过程序集属性CompilationRelaxationsAttribute进行字符串实习。
,并使用该使用单个固定大小StringBuilder的非递归方法。这里有一个SQL Server表值函数,它使用这种方法完成你的代码(我认为):
using Microsoft.SqlServer.Server;
class MySqlServerDotNetFunctions
{
[SqlFunction(TableDefinition="id int not null , value varchar(2000)" , FillRowMethodName="FillRow")]
public static IEnumerable> GenerateStrings(int stringLength)
{
const char lowerBound = '!' ;
const char upperBound = '~' ;
if (stringLength < 1 || stringLength > 2000) throw new ArgumentOutOfRangeException("stringLength","string length must be in the range 1-2000") ;
// initialize the stringbuilder
bool carry = false ; // carry flag
StringBuilder sb = new StringBuilder(new string(lowerBound,stringLength)) ;
for (long i = 0 ; !carry && ++i > 0 ;)
{
// return the current iteration
yield return new Tuple(i,sb) ;
// increment our string
int p = sb.Length-1 ; // we work right-to-left
do
{
carry = ++sb[p] > upperBound ;
if (carry)
{
sb[p] = lowerBound ;
}
} while (carry && --p >= 0) ;
}
}
public static void FillRow(object o , out long id , out string value)
{
Tuple item = (Tuple) o ;
id = item.Item1 ;
value = item.Item2.ToString() ;
return ;
}
}