求个代码:1000万行长度为20~30之间的字符串,删除其中重复的行
解决方案 »
- 客户端如何判断连接的是:内网服务器,还是外网服务器?
- 今天的一个面试题没做出来,还是个上机题,真郁闷啊,C# 如何对汉字按字典顺序排序??在线等
- WPF Listbox拖放
- 重庆的程序员(或测试)来看看吧,好消息
- dataTable.select(...)写法问题
- 请问winxp下的文件夹支持放多少个文件,百万级的可以吗
- 问一个关于WINFORM DATAGRrid 的取值的问题
- 各位高手帮忙啊!C#.NET中的ComboBox不点击下拉按钮就可以直接展开列怎么弄啊
- visual C#中如何实现datagrid的打印功能?
- 请教:在dataGrid 中如何根据行值的不同,设置不同的颜色
- 如何控制应用程序使之处于“激活状态”?
- 用C#写的EXE,如何在没有装.NET Framework的系统中提示用户需要下载?
Stopwatch watch = new Stopwatch();
watch.Start(); string[] alldit= File.ReadAllLines("d:\\alldit.txt"); foreach(string tmpdit in alldit)
{
if (!reditnew.Contains(tmpdit))
{
reditnew +=tmpdit;
}
}
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
这是我现在的代码一个小时了也不见结束
private void button1_Click(object sender, EventArgs e)
{
string reditnew = "";
Stopwatch watch = new Stopwatch();
watch.Start(); string[] alldit = File.ReadAllLines("d:\\alldit.txt"); foreach (string tmpdit in alldit)
{
if (!reditnew.Contains(tmpdit))
{
reditnew += tmpdit + "\r\n";
}
}
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
}
这是我现在的代码,更正二楼的漏了换行
文本文件发过来
private void button1_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string reditnew = "";
string[] alldit = File.ReadAllLines("d:\\alldit.txt");
Array.Sort(alldit); for (int i = 0; i < alldit.Length - 1; i++)
{
if (alldit[i].Split('$')[0] == alldit[i + 1].Split('$')[0])
{ alldit[i] = ""; }
else
{
if (alldit[i].Length > 1)
{
reditnew += alldit[i] + "\r\n";
}
}
}
reditnew += alldit[alldit.Length - 1] + "\r\n";
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();新代码,请帮忙改进
private void button1_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string reditnew = "";
string[] alldit = File.ReadAllLines("d:\\alldit.txt");
Array.Sort(alldit); for (int i = 0; i < alldit.Length - 1; i++)
{
if (alldit[i].Split('$')[0] == alldit[i + 1].Split('$')[0])
{ alldit[i] = ""; }
else
{
if (alldit[i].Length > 1)
{
reditnew += alldit[i] + "\r\n";
}
}
}
reditnew += alldit[alldit.Length - 1] + "\r\n";
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
}
这个再帮忙再改进下效率也不知道怎么再随机打乱排序
using System.Diagnostics;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] alldit = CreateDatas(100000);
var copy = new string[alldit.Length];
Array.Copy(alldit, copy, alldit.Length);
test10(alldit);
test1(copy);
Console.ReadKey();
} private static void test10(string[] alldit)
{
Array.Sort(alldit);
var reditnew = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
string last = null;
foreach (string tmpdit in alldit)
{
if (last == null || tmpdit != last)
{
reditnew.AppendLine(tmpdit);
last = tmpdit;
}
}
string result = reditnew.ToString();
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
} private static string[] CreateDatas(int p) //随机产生p和字符串
{
var Rnd = new Random();
return (from n in Enumerable.Range(1, p)
select Rnd.Next().ToString()).ToArray();
} private static void test1(string[] alldit)
{
string reditnew = "";
Stopwatch watch = new Stopwatch();
watch.Start();
foreach (string tmpdit in alldit)
{
if (!reditnew.Contains(tmpdit))
{
reditnew += tmpdit + "\r\n";
}
}
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
string reditnew = "";
StringBuilder newalldit = new StringBuilder();
string[] alldit = File.ReadAllLines("d:\\alldit.txt");
Array.Sort(alldit); for (int i = 0; i < alldit.Length - 1; i++)
{
if (alldit[i].Split('$')[0] == alldit[i + 1].Split('$')[0])
{ alldit[i] = ""; }
else
{
if (alldit[i].Length > 1)
{
// reditnew += alldit[i] + "\r\n";
newalldit.AppendLine(alldit[i]);
}
}
}
//reditnew += alldit[alldit.Length - 1] + "\r\n";
newalldit.AppendLine(alldit[alldit.Length - 1]);
watch.Stop();
textview.Text += ("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
reditnew = newalldit.ToString();
File.WriteAllText("d:\\newdit.txt", reditnew);
Console.Beep();
}
再次改进。性能OK了。。麻烦帮忙教我怎么打乱排序。谢谢
请教我下现在怎么打乱他的排序
public static T[] RandomSort<T>(T[] array)
{
int len = array.Length;
System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>();
T[] ret = new T[len];
Random rand = new Random();
int i = 0;
while (list.Count < len)
{
int iter = rand.Next(0, len);
if (!list.Contains(iter))
{
list.Add(iter);
ret[i] = array[iter];
i++;
} }
return ret;
}
找着了。。结帖
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Collections.Generic;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] alldit = CreateDatas(100000);
var copy = new string[alldit.Length];
Array.Copy(alldit, copy, alldit.Length);
test10(alldit);
test1(copy);
Console.ReadKey();
} private static void test10(string[] alldit)
{
Array.Sort(alldit);
var reditnew = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
string last = null;
foreach (string tmpdit in alldit)
{
if (last == null || tmpdit != last)
{
reditnew.AppendLine(tmpdit);
last = tmpdit;
}
}
string result = reditnew.ToString();
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
} private static string[] CreateDatas(int p) //随机产生p和字符串
{
var Rnd = new Random();
return (from n in Enumerable.Range(1, p)
select Rnd.Next().ToString()).ToArray();
} private static void test1(string[] alldit)
{
var reditnew = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (string tmpdit in alldit)
{
if (dict.ContainsKey(tmpdit))
{
reditnew.AppendLine(tmpdit);
}
}
var result = reditnew.ToString();
watch.Stop();
Console.WriteLine("用时:" + watch.ElapsedMilliseconds.ToString() + "毫秒\r\n");
}
}
}
后边那个新的测试使用的是hash字典。而且它也是按照原来文件中的次序排的,无需“打乱”次序。