-
-
Save haifengwang/34b8408f7bd92a2b5ffd to your computer and use it in GitHub Desktop.
ParallelCreateTxt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Explore | |
{ | |
public class CreateTxt | |
{ | |
public static void CommomCreateTxt() | |
{ | |
var ls = GetPersonLs(); | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
foreach (var item in ls) | |
{ | |
File.WriteAllText(string.Format(@"F:\project\teststatic\{0}.txt", item.Id), item.ToString(), Encoding.UTF8); | |
} | |
watch.Stop(); | |
Console.WriteLine("普通循环生成时间:{0}", watch.ElapsedMilliseconds.ToString()); | |
//423 440 | |
} | |
public static void ThreadPoolCreateTxt() | |
{ | |
var ls = GetPersonLs(); | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
ManualResetEvent eventX = new ManualResetEvent(false); | |
//可以获取和设置线程池的最大线程数,即线程池中共有多少个线程。线程池中的线程数目仅受可用内存的限制。但是,线程池将对允许在进程中同时处于活动状态的线程数目强制实施限制(这取决于CPU的数目和其他因素)。默认情况下,每个系统处理器最多可以运行25个线程池线程。 | |
ThreadPool.SetMaxThreads(3, 3); | |
// thr t = new thr(15, eventX); | |
foreach (var item in ls) | |
{ | |
ThreadPool.QueueUserWorkItem(x => { | |
File.WriteAllText(string.Format(@"F:\project\teststatic\{0}.txt", item.Id), item.ToString(), Encoding.UTF8); | |
}); | |
} | |
watch.Stop(); | |
Console.WriteLine("线程池生成时间:{0}", watch.ElapsedMilliseconds.ToString()); | |
} | |
public static void ParallelCreateTxt() | |
{ | |
var ls = GetPersonLs(); | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
Parallel.ForEach(ls, new ParallelOptions{MaxDegreeOfParallelism=Environment.ProcessorCount}, item => { File.WriteAllText(string.Format(@"F:\project\teststatic\{0}.txt", item.Id), item.ToString(), Encoding.UTF8); }); | |
watch.Stop(); | |
Console.WriteLine("Parallel循环生成时间:{0}", watch.ElapsedMilliseconds.ToString()); | |
} | |
static List<Person> GetPersonLs() | |
{ | |
var ls = new List<Person>(); | |
for (int i = 0; i < 20000; i++) | |
{ | |
ls.Add(new Person { Id = i, Name = string.Format("a{0}_{1}", i, i + i), Age = (i + 20) * i }); | |
} | |
return ls; | |
} | |
/* | |
* for (int i = 0; i < 10; i++) | |
{ | |
CommomCreateTxt(); | |
//ParallelCreateTxt(); | |
System.Threading.Thread.Sleep(1000); | |
} | |
*/ | |
/* | |
* 普通循环生成时间:455 | |
普通循环生成时间:257 | |
普通循环生成时间:241 | |
普通循环生成时间:258 | |
普通循环生成时间:249 | |
普通循环生成时间:250 | |
普通循环生成时间:284 | |
普通循环生成时间:270 | |
普通循环生成时间:251 | |
普通循环生成时间:262 | |
*/ | |
/* | |
* | |
* Parallel循环生成时间:300 | |
Parallel循环生成时间:178 | |
Parallel循环生成时间:170 | |
Parallel循环生成时间:284 | |
Parallel循环生成时间:425 | |
Parallel循环生成时间:171 | |
Parallel循环生成时间:159 | |
Parallel循环生成时间:161 | |
Parallel循环生成时间:158 | |
Parallel循环生成时间:159 | |
*/ | |
} | |
public class Person | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("Id={0} ,Name={1} Age={2}", this.Id, this.Name, this.Age); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment