读书人

死机了有没有更快的算法哦!该如何处

发布时间: 2012-12-17 09:31:41 作者: rapoo

死机了,有没有更快的算法哦!!
一个List<string>集合,比如list1:{"关闭0","2地方","5法0规","fg","地方","9"}
又是一个List<string>集合,比如list2:{"0","5"}
一个List<int>集合,比如list3:{1,2}


目标:在list1中寻找满足下面条件的元素:

对于list1中的某个元素而言,其包含list2中的元素,并且包含的个数是属于list3中规定的个数,那么,list1中的此元素就算满足条件(上例中有"关闭0"、"5法0规"满足条件)


求出list1中所有满足条件的元素,下面是我的方法:


public List<string> get_1(List<string> list1, List<string> list2, List<int> list3)
{
List<string> list_中介 = new List<string>(list1);
List<string> list_返回值 = new List<string>();
foreach (string s in list_中介)
{
bool b = true;
foreach (int i in list3)
{
int i_出 = list2.Where(x => s.Contains(x)).Count();
if (i == i_出)
{
b = false;
break;
}
}
if (b)
{
list1.Remove(s);
}
}
return list1;
}


可是,很慢哦,如果list1有1百万个字符串,list2只有2个元素,list3也只有2个数字,计算结果就是死机...........哪位朋友有更好、更快的算法吗?


[最优解释]
        static public IEnumerable<string> get_1(List<string> list1, List<string> list2, List<int> list3)
{
return list1.Where(x => list3.Any(z => list2.Where(y => x.Contains(y)).Count() == z)).ToList();


}


[其他解释]

为啥非要用 =>
直接写都比那个快1/3

public static void get_2(List<string> list1, List<string> list2, List<int> list3)
{
List<string> result = new List<string>();
foreach (string s1 in list1)
{
int count = 0;
foreach (string s2 in list2)
{
if (s1.Contains(s2))
count++;
}
if (count > 0 && list3.Contains(count))
result.Add(s1);
}

[其他解释]
public class Program
{
class NewCls
{
public string a;
public int b;
}
static void Main(string[] args)
{
var list1 = new List<string>() { "关闭0", "2地方", "5法0规", "fg", "地方", "9" };
List<NewCls> list2 = new List<NewCls>() { new NewCls() { a = "0", b = 1 }, new NewCls() { a = "5", b = 2 } };
list1 = list1.Where(t => list2.Count(tt => t.Contains(tt.a.Trim()) && t.Length - t.Replace(tt.a, "").Length == tt.b) > 0).ToList();
foreach (var t in list1)
{
Console.WriteLine(t);
}
Console.ReadLine();


}
}
[其他解释]
list remove是很慢的。
[其他解释]

public static List<string> get_1(List<string> list1, List<string> list2, List<int> list3)         {             List<string> result = new List<string>();             string reg = string.Join("
[其他解释]
", list2.ToArray());
int min = list3.Min();
int max = list3.Max();
foreach (string str in list1)
{
var mc = Regex.Matches(str, reg);
if(list3.IndexOf(mc.Count)>-1)
{
result.Add(str);
break;
}
}
return result;
}

[其他解释]
", list2.ToArray()); // int min = list3.Min(); // int max = list3.Max(); foreach (string str in list1) { var mc = Regex.Matches(str, reg); if(list3.IndexOf(mc.Count)>-1) { result.Add(str); break; } } return result; }


[其他解释]



我可能遇到过楼主说的情况,当时我用了3个冒泡循环!

我们的数据比较少,只有3条!
[其他解释]
用性能分析器看看那里最消耗,数据量大,总会有瓶颈的,100W数据list切割成100个1W的list,开100个线程做处理,然后汇总数据,删除检测出的元素;你也不必一定用100个线程,先测试下开5个线程,找到一个最大效率的线程数.

关于死机,如果是程序死了,有可能是等待太久,内存消耗过大之类的,如果是系统死了,就是运算量超过负荷了,这个时候如果不能在算法上改进,只能用时间来换空间了...
[其他解释]
一百万个字符串?怎么不放到数据库中?
[其他解释]
b = list3.Contains(list2.Count(x => s.Contains(x)));
[其他解释]
“list_返回值”这个没用到...
1. list_中介 感觉没有必要!如果要返回符合条件的元素就直接保存那些元素;若要剔除符合条件的元素则不需要 list_中介,只是用for循环而不用foreach。 这个 list_中介 量太大,耗内存也耗时间!

2. list1.Remove(s);感觉效果不好,list1需要在一百多万条记录中从头到尾地寻找第一个匹配值,并删除。个人觉得如果需要剔除某个元素,最好使用索引,这样可能快点(纯属臆测,你可以试试)
[其他解释]

        public List<string> get_1(List<string> list1, List<string> list2, List<int> list3)
{
List<string> result = new List<string>();

list1.ForEach((s) =>
{
if (list3.Contains(list2.Count(x => s.Contains(x))))
{
result.Add(s);
}
});

return result;
}

这不是最优算法,这只是按照你的算法精简之后的写法。
[其他解释]
还是很慢么?相比修改之前还是没有显著效果?那你请教大神去吧...我只是菜鸟..
[其他解释]
 public static List<string> get_1(List<string> list1, List<string> list2, List<int> list3)
{
List<string> result = new List<string>();
string reg = string.Join("
[其他解释]
“int i_出 = list2.Where(x => s.Contains(x)).Count(); ”
这句是不是应该移到foreach (int i in list3)外面?
[其他解释]
如果是多核CPU,可以考虑下task的多核并行处理,本人也没测试过多核编程,只是突然想到,可以尝试下
[其他解释]
用字典呢。。。
------其他解决方案--------------------


我靠 这样不慢才怪
[其他解释]
大数据量用Contains()方法效率比较慢,建议使用正则的方法来匹配。不要使用循环用indexOf()来判断

      public static List<string> get_1(List<string> list1, List<string> list2, List<int> list3)
{
List<string> result = new List<string>();
string reg = string.Join("
[其他解释]
引用:
C# code?1234 static public IEnumerable<string> get_1(List<string> list1, List<string> list2, List<int> list3) { return list1.Where(x => list3.Any(z => list2.Where……

学习!
[其他解释]
", list2.ToArray());
//int min = list3.Min();
//int max = list3.Max();
foreach (string str in list1)
{
var mc = Regex.Matches(str, reg);
if(list3.IndexOf(mc.Count)>-1)
{
result.Add(str);
break;
}
}
return result;
}

[其他解释]
某软件的计算速度挺快的,不知它是怎么搞的,比我快那么多
[其他解释]
引用:
一百万个字符串?怎么不放到数据库中?

放到数据库中,能提升运算速度吗?
[其他解释]
我最多只能改成下面这样了,但提升速度不明显:

public List<string> get_1(List<string> list1, List<string> list2, List<int> list3)
{
List<string> list_中介 = new List<string>(list1);
List<string> list_返回值 = new List<string>();


bool b = true;
foreach (string s in list_中介)
{
b = list3.Contains(list2.Where(x => s.Contains(x)).Count());
if (!b)
{
list1.Remove(s);
}
}
return list1;
}


[其他解释]
难道是Add比

引用:
C# code?1234 static public IEnumerable<string> get_1(List<string> list1, List<string> list2, List<int> list3) { return list1.Where(x => list3.Any(z => list2.Where……

此方法速度快了不下11倍?
难道是Add比Remove快?
[其他解释]
引用:
为啥非要用 =>
直接写都比那个快1/3
C# code?1234567891011121314 public static void get_2(List<string> list1, List<string> list2, List<int> list3) { List<string> result = new List<string>()……


谁能解释一下,为什么此方法速度快了不下10倍?

读书人网 >C#

热点推荐