解决方案 »
- 改写asp的ajax无刷新查询?
- List排序问题.8,3,5,1,9,4
- listview 的列宽如何固定?
- 如何对表中的某一条记录进行锁定(已知该条记录的id),目的是不让别人对该行记录进行更新。但允许自己更新。
- 关于comboBox SelectedIndexChanged事件的问题!
- C#
- 正测表达式
- 关于DataList中循环的一些疑问
- 怎么使用C#获取远程http://www.9630000.com/temp.txt,并将此文件获取并保存于本地盘当前文件夹下的temp.txt中。急用!!!谢谢!!!
- 如何使用C#控制Win2000的文件访问权限?
- 提取EXIF信息 发现信息不全 方法有问题?!!
- C#递归算法
2)我的疏忽,那些数字最多只是千位以内数字,所以4位char数组就可以了
请给出您的解决方案 谢谢!
{
ht.Clear();
...
} {
我有50000个value可能不同字符串, 我的那个程序里是假定这50000全部相同,而实际业务需求是这个50000个字符串几乎不可能相同 ht.Clear(); 我已经说过了 ,ht是保存结果的容器,如果Clear了 结果就都没了...
string strValue ="112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
string[] strList = strValue.Split(new char[] { ',' }, StringSplitOptions.None);
for (int i = 0; i < 5000; i++)
{
for(int j=0;j<strList.Length;j++)
{
if (strList[j] == "")
continue;
if(!hashTable.Contains(strList[j]))
hashTable.Add(strList[j], strList[j]);
}
}
这50000个字符串是不同的, 而且具体环境不允许使用linq
我知道啊同样的用Split()截取啦数据都添加到Hashtable里面。。
循环部分可以这样避免不断new char[] { '\0', '\0', '\0', '\0' };提高一点速度
[code=C#]
for (int i = 0; i < 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
int start = 0;
fixed (char* c = apps)
{
for (int j = 0; j < apps.Length; j++)
{
if (c[j] == ',')
{
string s = new string(c, start, j - start);
if (!ht.Contains(s))
{
ht.Add(s);
}
start = j + 1;
}
}
}
/code]
for (int i = 0; i < 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
int start = 0;
fixed (char* c = apps)
{
for (int j = 0; j < apps.Length; j++)
{
if (c[j] == ',')
{
string s = new string(c, start, j - start);
if (!ht.Contains(s))
{
ht.Add(s);
}
start = j + 1;
}
}
}
int index = hashCode % this.m_buckets.Length;
for (int i = this.m_buckets[hashCode % this.m_buckets.Length] - 1; i >= 0; i = this.m_slots[i].next)
{
if ((this.m_slots[i].hashCode == hashCode) && this.m_comparer.Equals(this.m_slots[i].value, value))
{
return false;
}
}
在add时本身会进行一次判断,如果有相同的就不会添加。
{
while (true)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
HashSet<string> ht = new HashSet<string>();
timer.Start(); for (int i = 0; i <= 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
#region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int startIndex = 0; for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
string s = new string(appCharArr, startIndex, j - startIndex);
ht.Add(s);
startIndex = j + 1;
}
}
#endregion
}
timer.Stop();
int count = ht.Count;
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
}
每次都是同一个string,比起真正不同的string来速度本身会快很多。
不知道你们的需求是什么样,如果可以改成多线程,或者数据是一次性到达的,就先放到一个大的缓冲区或许能根本解决。
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
#region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
//int startIndex = 0; foreach (char c in appCharArr)
{
if (c == ',')
{
ht.Add(c.ToString());
}
}
#endregion
}
static void Main(string[] args)
{
while (true)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
bool[] array = new bool[9999];
timer.Start();
int count = 0;
for (int i = 0; i <= 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
array.Initialize();
char[] appCharArr = apps.ToCharArray(); int length = appCharArr.Length;
int startIndex = 0;
char[] tempcharArr = new char[] { '\0', '\0', '\0', '\0' };
#region
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
int temp = ConvertCharArrayToInt(tempcharArr);
if (!array[temp])
{
count++;
array[temp] = true;
}
startIndex = 0;
for (int k = 0; k < 4; k++)
tempcharArr[k] = '\0';
}
else
{
tempcharArr[startIndex] = c;
startIndex++;
}
}
#endregion
}
timer.Stop();
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
} static int ConvertCharArrayToInt(char[] array)
{
int result = 0;
int temp = 0;
int j = 0;
for (int i = 3; i >= 0; i--)
{
if (array[i] != '\0')
{
j = array[i] - 48;
switch (temp)
{
case 0:
result += j;
break;
case 1:
result += (j << 4) - (j << 2) - (j << 1);
break;
case 2:
result += (j << 7) - (j << 4) - (j << 2) - (j << 2) - (j << 2);
break;
case 3:
result += (j << 10) - (j << 4) - (j << 2) - (j << 2);
break;
}
temp++;
}
}
return result;
}
{
while (true)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
HashSet<int> ht = new HashSet<int>();
timer.Start(); for (int i = 0; i <= 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
#region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int startIndex = 0;
int appInt = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
ht.Add(appInt);
appInt = 0;
}
else
{
appInt = appInt * 100 + (int)c;
}
}
#endregion
}
timer.Stop();
int count = ht.Count;
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
}
all the same 还是非常感谢您的代码, 我学到了很多知识!
while (true)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
HashSet<int> ht = new HashSet<int>();
timer.Start(); for (int i = 0; i <= 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,"; #region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int startIndex = 0;
int appInt = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
ht.Add(appInt);
appInt = 0;
}
else
{
appInt = appInt * 100 + (int)c;
}
}
#endregion
}
timer.Stop();
int count = ht.Count;
foreach (var i in ht)
{
Console.WriteLine(i);
}
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
appInt = appInt * 10 + c - '0';
四位数用一个short就可以表示了,固定2个字节,还不需要分割符号。
如果是文件,那么:byte[] raws = File.ReadAllBytes("...");
short[] vals = new short[raws.Length/2];
Buffer.BlockCopy(raws, 0, vals, 0, vals.Length*2);
static void Main(string[] args)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
HashSet<int> big = new HashSet<int>();
bool[] small = new bool[100]; timer.Start(); for (int i = 0; i <= 500000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,"; int number = 0;
for (int j = 0; j < apps.Length; j++)
{
char c = apps[j];
if (c == ',')
{
if (number < 100)
{
small[number] = true;
}
else
{
big.Add(number);
} number = 0;
}
else
{
number = number * 10 + (int) (c - '0');
}
}
}
timer.Stop(); for (int i = 0; i < small.Length; i++)
{
if (small[i])
{
Console.WriteLine(i);
}
} foreach (var i in big)
{
Console.WriteLine(i);
}
Console.WriteLine("{0} ms", timer.Elapsed.TotalMilliseconds / 10);
}
}
HashSet存放的数据都是不重复的
HashSet.Add(T)会返回bool操作结果,这里LZ好像不需要关心这个
如果只是想取总长度,在外面弄个变量累计数字也可以,ht.Clear()就也没必要了
Mark!
{
static void Main(string[] args)
{
while (true)
{
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50000; i++)
{
sb.Append(random.Next(10000));
sb.Append(",");
}
Act1(sb.ToString());
Act2(sb.ToString());
Console.ReadLine();
}
}
static void Act1(string apps)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
HashSet<int> ht = new HashSet<int>();
timer.Start(); char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int appInt = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
ht.Add(appInt);
appInt = 0;
}
else
{
appInt = appInt * 100 + (int)c;
}
} timer.Stop();
int count = ht.Count;
Console.WriteLine(count);
Console.WriteLine(timer.Elapsed.ToString());
}
static void Act2(string apps)
{ Stopwatch timer = new Stopwatch();
timer.Reset();
MySet set = new MySet();
timer.Start();
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
set.Set();
}
else
{
set.Local(c);
}
}
timer.Stop();
int count = set.Count;
Console.WriteLine(count);
Console.WriteLine(timer.Elapsed.ToString());
}
}
public class MySet
{
bool[] set = new bool[10000];
private int index = 0;
private int count = 0; public void Local(char c)
{
index = index * 10 + (c - 48);
} public void Set()
{
if (!set[index])
{
count++;
}
set[index] = true;
index = 0;
} public int Count { get { return count; } }
}
这些数字输出也很方便
public IEnumerable<int> Numbers
{
get
{
for (int i = 0; i < 100000; i++)
{
if (set[i])
{
yield return i;
}
}
}
}
50000次循环完了之后 用linq的distinct去除重复, 最后我那破机器跑到了32ms
我现在在网吧, 回公司后再验证袁锋的那段程序有没有我的快, 下 先Dota 哈哈
{
int[] set = new int[3125];
private int index = 0;
private int count = 0;
public void Local(char c)
{
index = index * 10 + (c - 48);
}
public void Set()
{
int a = index / 32;
int b = index % 32;
if ((set[a] & (1 << b)) == 0)
{
count++;
set[a] |= (1 << b);
}
index = 0;
}
public int Count { get { return count; } }
}
您的程序在我的机器上的结果为:
Console.WriteLine("{0}", timer.Elapsed.ToString());
00:00:00.4147369
这好像是414毫秒吧... 纠结中...
private static void Act()
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
List<int> ht = new List<int>();
timer.Start();
bool[] isValue = new bool[10000]; for (int i = 50000; i >= 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,"; #region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int appInt = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
if (!isValue[appInt])
{
ht.Add(appInt);
isValue[appInt] = true;
}
appInt = 0;
}
else
{
appInt <<= 1;
appInt += appInt << 2;
appInt += c - '0';
}
}
#endregion
}
timer.Stop();
int count = ht.Count;
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
}
测试结果 :串长为5万时,时间11ms, 串的数量为5万时,时间49ms.
private void button2_Click(object sender, EventArgs e) { //这是生成测试用的数据的 StringBuilder builder = new StringBuilder(50000);
int k = 0;
for (int i = 0; i < 50000; i++) {
k = i % 10000;
builder.Append("," + k.ToString()); // if (builder.Length > 50000) break;
} Test(builder );
}
//这是函数
void Test(StringBuilder appCharArr) {
DateTime dt0 = DateTime.Now; List<int> lst = new List<int>(50000); string sub = "";
int iMax = appCharArr.Length;
for (int i = 0; i < iMax; i++) {
char c = appCharArr[i]; if (char.IsDigit(c)) {
sub += c;
}
else if (c.Equals(',')) {
if (sub.Length > 0) {
lst.Add(Convert.ToInt32(sub));
} sub = "";
}
} //-0------------------------------------
List<int> lstResult = new List<int>(50000); lst.Sort();
if (lst.Count == 0) return; //-0--------compare----------------
int iOld = lst[0];
lstResult.Add(iOld);
foreach (int each in lst) {
if (each == iOld) continue;
lstResult.Add(each);
iOld = each;
} TimeSpan span = DateTime.Now - dt0;
MessageBox.Show("总时间(ms):" + span.TotalMilliseconds.ToString()); }
private static unsafe void Act()
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
timer.Start();
int count = 0;
byte[] isValue = new byte[10000]; for (int i = 50000; i >= 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,"; #region
fixed (byte* isValueStart = isValue)
{
fixed (char* appStart = apps)
{
int value, high, appInt = count = 0;
int* next = (int*)appStart;
for (int* end = next + (apps.Length >> 1); next != end; ++next)
{
high = value = *next;
value &= 0xffff;
high >>= 16;
if (value != ',')
{
appInt <<= 1;
appInt += appInt << 2;
appInt += (value -= '0');
if (high != ',')
{
appInt <<= 1;
appInt += appInt << 2;
appInt += (high -= '0');
}
else
{
count += isValueStart[appInt] ^ 1;
appInt = 0;
isValueStart[appInt] = 1;
}
}
else
{
count += isValueStart[appInt] ^ 1;
appInt = (high -= '0');
isValueStart[appInt] = 1;
}
}
count += apps.Length & 1 & (isValueStart[appInt] ^ 1);
}
}
#endregion
}
timer.Stop();
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
}
private static unsafe void Act()
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
List<int> ht = new List<int>();
timer.Start();
byte[] isValue = new byte[10000]; for (int i = 50000; i >= 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,"; #region
fixed (byte* isValueStart = isValue)
{
fixed (char* appStart = apps)
{
int value, high, appInt = 0;
int* next = (int*)appStart;
for (int* end = next + (apps.Length >> 1); next != end; ++next)
{
high = value = *next;
value &= 0xffff;
high >>= 16;
if (value != ',')
{
appInt <<= 1;
appInt += appInt << 2;
appInt += (value -= '0');
if (high != ',')
{
appInt <<= 1;
appInt += appInt << 2;
appInt += (high -= '0');
}
else
{
if (isValueStart[appInt] == 0)
{
ht.Add(appInt);
isValueStart[appInt] = 1;
}
appInt = 0;
}
}
else
{
if (isValueStart[appInt] == 0)
{
ht.Add(appInt);
isValueStart[appInt] = 1;
}
appInt = (high -= '0');
}
}
if ((apps.Length & 1 & (isValueStart[appInt] ^ 1)) != 0)
{
ht.Add(appInt);
}
}
}
#endregion
}
timer.Stop();
int count = ht.Count;
Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
}
目前最快的一个 static void Act()
{
while (true)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
HashSet<int> ht = new HashSet<int>();
timer.Start();
//int[] list = new int[10000000];
bool[] small = new bool[300];
int[] result = new int[300];
for (int i = 0; i <= 50000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20";
#region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int appInt = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
//list[startIndex] = appInt;
small[appInt] = true;
appInt = 0;
}
else
{
appInt = appInt * 10 + (int)(c-'0');
}
}
#endregion
}
var intApps = GetIntArr(small); timer.Stop();
int count = 0;
count = intApps.Count();
List<int> lll = intApps.ToList();
Console.WriteLine(timer.Elapsed.ToString() + " Count:" + count);
Console.ReadLine();
}
}
static IEnumerable<int> GetIntArr(bool[] small)
{
for (int i = 0; i < small.Length; i++)
{
if (small[i])
{
yield return i;
}
}
}
54楼没有理解我的需求 应该将你程序中50000换成2500000才相当于我的需求
目前最快的一个
sorry,没有看你的代码,只看了前边的说明,理解有误,在此深表歉意!
恩,反正算法是都是这样的。
就是29楼litaoye提到的 bool[]
内存数组本来就是一种特殊的哈希表。只不过哈希函数比较简单index直接对应位置。
不过要求数字比较集中,而且都是整数,才能直接用数组代替哈希表
using System.Collections.Generic;
using System.Diagnostics;namespace CsdnTest
{
class Program
{
static void Main(string[] args)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
bool[] flag = new bool[10000];
List<int> rseult = new List<int>();
timer.Start(); for (int i = 0; i <= 500000; i++)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20,";
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
//rseult.Clear();
int appInt = 0; for (int j = 0; j < length; j++)
{
if (appCharArr[j] == ',')
{
if (!flag[appInt])
{
flag[appInt] = true;
rseult.Add(appInt);
} appInt = 0;
}
else
appInt = appInt * 10 + appCharArr[j] - '0';
} //foreach (int index in rseult)
// flag[index] = false;
} timer.Stop(); foreach (int index in rseult)
Console.WriteLine(index); Console.WriteLine(timer.Elapsed.ToString());
Console.ReadLine();
}
}
}
var intApps = GetIntArr(small);
应该放在循环里面
{
for (int i = 0; i < small.Length; i++)
{
if (small[i])
{
yield return i;
}
}
}这一段代码的代价是很高的,直接用个循环生成数组比这个快几倍。
private static void Act()
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
timer.Start();
int count = 0;
int[] small = new int[300];
int[] result = new int[300];
for (int i = 50000; i != 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20"; #region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int appInt = count = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
if (small[appInt] != i)
{
result[count++] = appInt;
small[appInt] = i;
}
appInt = 0;
}
else
{
appInt = appInt * 10 + (int)(c - '0');
}
}
if (appCharArr[length - 1] != ',') result[count++] = appInt;
#endregion
} timer.Stop();
Console.WriteLine(timer.Elapsed.ToString() + " Count:" + count);
Console.ReadLine();
}
}
还是不能理解 留下你的QQ 我们细细讨论
private static void Act()
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
timer.Start();
int count = 0, index = 0;
int[] small = new int[300];
int[] result = new int[300];
for (int i = 50000; i != 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20"; #region
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length;
int appInt = count = 0;
++index;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
if (small[appInt] != index)
{
result[count++] = appInt;
small[appInt] = index;
}
appInt = 0;
}
else
{
appInt *= 10;
appInt += c;
appInt -= '0';
}
}
if (appCharArr[length - 1] != ',') result[count++] = appInt;
#endregion
} timer.Stop();
Console.WriteLine(timer.Elapsed.ToString() + " Count:" + count);
Console.ReadLine();
}
}
而且连List都没用,自己用数组维护,稍微有点麻烦,但性能会有略微的提高。
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
timer.Start();
int count = 0;
int[] small = new int[300];
int[] result = new int[300];
for (int i = 50000; i != 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20"; #region
fixed (int* smallStart = small, resultStart = result)
{
fixed (char* appStart = apps)
{
int length = apps.Length, appInt = count = 0, low, high;
int* start = (int*)appStart;
for (int* end = start + (length >> 1); start != end; ++start)
{
high = low = *start;
low &= 0xffff;
high >>= 16;
if (low != ',')
{
appInt *= 10;
appInt += low;
appInt -= '0';
if (high != ',')
{
appInt *= 10;
appInt += high;
appInt -= '0';
}
else
{
if (smallStart[appInt] != i)
{
resultStart[count++] = appInt;
smallStart[appInt] = i;
}
appInt = 0;
}
}
else
{
if (smallStart[appInt] != i)
{
resultStart[count++] = appInt;
smallStart[appInt] = i;
}
appInt = high;
appInt -= '0';
}
}
if ((length & 1) == 0)
{
if (appStart[length - 1] != ',') resultStart[count++] = appInt;
}
else if ((low = *(char*)start) != ',')
{
appInt *= 10;
appInt += low;
appInt -= '0';
resultStart[count++] = appInt;
}
else if (smallStart[appInt] != i)
{
resultStart[count++] = appInt;
smallStart[appInt] = i;
}
}
}
#endregion
} timer.Stop();
Console.WriteLine(timer.Elapsed.ToString() + " Count:" + count);
Console.ReadLine();
}
}
{
while (true)
{
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Reset();
timer.Start();
int count = 0;
int[] small = new int[300];
int[] result = new int[300];
for (int i = 50000; i != 0; --i)
{
string apps = "112,2,3,4,5,6,7,8,9,10,112,12,13,14,15,16,17,18,19,20"; #region
fixed (int* smallStart = small, resultStart = result)
{
fixed (char* appStart = apps)
{
int length = apps.Length, appInt = count = 0, low;
for(char* start = appStart, end = start + length; start != end; ++start)
{
low = *start;
if (low != ',')
{
appInt *= 10;
appInt += low;
appInt -= '0';
}
else
{
if (smallStart[appInt] != i)
{
resultStart[count++] = appInt;
smallStart[appInt] = i;
}
appInt = 0;
}
}
if (appStart[length - 1] != ',') resultStart[count++] = appInt;
}
}
#endregion
} timer.Stop();
Console.WriteLine(timer.Elapsed.ToString() + " Count:" + count);
Console.ReadLine();
}
}
不知道可否留下联系方式啊?
{
static void Main(string[] args)
{
while (true)
{
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50000; i++)
{
sb.Append(random.Next(10000));
sb.Append(",");
}
Splits1(sb.ToString());
Act2(sb.ToString());
Console.ReadLine();
}
}
static void Splits1(string apps)
{
Stopwatch timer = new Stopwatch();
timer.Reset();
timer.Start();
var f = GetIntArr(apps).GetEnumerator();
timer.Stop();
Console.WriteLine(timer.Elapsed.ToString());
}
static IEnumerable<int> GetIntArr(string apps)
{
int appInt = 0;
int resint = 0;
char[] small = apps.ToCharArray();
for (int i = 0; i < small.Length; i++)
{
if (small[i] != ',')
{
appInt = appInt * 10 + (int)(small[i] - '0');
}
else
{
resint = appInt;
appInt = 0;
yield return resint;
}
}
}
static void Act2(string apps)
{ Stopwatch timer = new Stopwatch();
timer.Reset();
bool[] small = new bool[10000];
timer.Start();
char[] appCharArr = apps.ToCharArray();
int length = appCharArr.Length; int appInt = 0;
for (int j = 0; j < length; j++)
{
char c = appCharArr[j];
if (c == ',')
{
small[appInt] = true;
appInt = 0;
}
else
{
appInt = appInt * 10 + (int)(c - '0');
}
}
timer.Stop();
Console.WriteLine(timer.Elapsed.ToString());
}
}
{
int appInt = 0;
int resint = 0;
char[] small = apps.ToCharArray();
for (int i = 0; i < small.Length; i++)
{
if (small[i] != ',')
{
appInt = appInt * 10 + (int)(small[i] - '0');
}
else
{
resint = appInt;
appInt = 0;
yield return resint;
}
}
}
效率提高不少