G
Guest
I'm hoping someone can help me understand why running a set of processes
through the threadpool seems to be slower than running the same process
through a single thread (maybe I'm minunderstanding what the threadpool class
is good for). Let me example what I need to do:
Basically, I have a file that has n number of records in it. There could be
40,000 records to 4 million records. Each record requires its own
calculations, and while the records themselves are not dependant on data from
any other record, the results must be printed back to the results file in the
same order that they were processed.
I've written the following (see below) that works -- however the code is
almost twice as slow as when the process is run within a single thread. I'm
just wondering why I'm seeing what I'm seeing or is there a better way to do
this.
Thank you,
--TR
Sample code below:
using System;
using System.Collections.Generic;
namespace thread_tes
{
class MainClass
{
public static void Main(string[] args)
{
System.Threading.ThreadPool.SetMaxThreads(5, 5);
System.Threading.ManualResetEvent[] doEvents = new
System.Threading.ManualResetEvent[5];
System.IO.StreamReader reader = new
System.IO.StreamReader(@"c:\thread.txt",
System.Text.Encoding.GetEncoding(1252));
System.IO.StreamWriter writer = new
System.IO.StreamWriter(@"c:\thread1.txt", false);
RunClass[] r = new RunClass[5];
int max = r.Length;
int i = 0;
while (reader.Peek() > -1)
{
string s = reader.ReadLine();
doEvents = new
System.Threading.ManualResetEvent(false);
r = new RunClass(s, doEvents);
//r = f;
System.Threading.ThreadPool.QueueUserWorkItem(r.Run,
i);
i++;
if (i == max) {
System.Threading.WaitHandle.WaitAll(doEvents);
PrintThreads(writer, r, i);
i =0;
}
}
if (i > 0) {
System.Threading.WaitHandle.WaitAll(doEvents);
Console.WriteLine("New String: " + r[0].NewString);
PrintThreads(writer, r, i);
}
writer.Close();
Console.WriteLine("Finished");
Console.ReadLine();
}
static void PrintThreads(System.IO.StreamWriter writer, RunClass[]
r, int i)
{
Console.WriteLine("PrintThreads Executed");
for (int x=0; x < i; x++) {
if (r[x] != null)
{
writer.WriteLine(r[x].NewString);
}
}
}
}
class RunClass
{
private string _p = "";
private string _Internal = "";
private System.Threading.ManualResetEvent _doEvent = new
System.Threading.ManualResetEvent(false);
public RunClass(string p, System.Threading.ManualResetEvent _event) {
_p = p;
_doEvent = _event;
}
public string NewString
{
set {_Internal = value;}
get {return _Internal;}
}
public void Run(object status)
{
int i = 0;
i = System.Convert.ToInt32(_p);
i++;
NewString = i.ToString();
_doEvent.Set();
}
}
}
through the threadpool seems to be slower than running the same process
through a single thread (maybe I'm minunderstanding what the threadpool class
is good for). Let me example what I need to do:
Basically, I have a file that has n number of records in it. There could be
40,000 records to 4 million records. Each record requires its own
calculations, and while the records themselves are not dependant on data from
any other record, the results must be printed back to the results file in the
same order that they were processed.
I've written the following (see below) that works -- however the code is
almost twice as slow as when the process is run within a single thread. I'm
just wondering why I'm seeing what I'm seeing or is there a better way to do
this.
Thank you,
--TR
Sample code below:
using System;
using System.Collections.Generic;
namespace thread_tes
{
class MainClass
{
public static void Main(string[] args)
{
System.Threading.ThreadPool.SetMaxThreads(5, 5);
System.Threading.ManualResetEvent[] doEvents = new
System.Threading.ManualResetEvent[5];
System.IO.StreamReader reader = new
System.IO.StreamReader(@"c:\thread.txt",
System.Text.Encoding.GetEncoding(1252));
System.IO.StreamWriter writer = new
System.IO.StreamWriter(@"c:\thread1.txt", false);
RunClass[] r = new RunClass[5];
int max = r.Length;
int i = 0;
while (reader.Peek() > -1)
{
string s = reader.ReadLine();
doEvents = new
System.Threading.ManualResetEvent(false);
r = new RunClass(s, doEvents);
//r = f;
System.Threading.ThreadPool.QueueUserWorkItem(r.Run,
i);
i++;
if (i == max) {
System.Threading.WaitHandle.WaitAll(doEvents);
PrintThreads(writer, r, i);
i =0;
}
}
if (i > 0) {
System.Threading.WaitHandle.WaitAll(doEvents);
Console.WriteLine("New String: " + r[0].NewString);
PrintThreads(writer, r, i);
}
writer.Close();
Console.WriteLine("Finished");
Console.ReadLine();
}
static void PrintThreads(System.IO.StreamWriter writer, RunClass[]
r, int i)
{
Console.WriteLine("PrintThreads Executed");
for (int x=0; x < i; x++) {
if (r[x] != null)
{
writer.WriteLine(r[x].NewString);
}
}
}
}
class RunClass
{
private string _p = "";
private string _Internal = "";
private System.Threading.ManualResetEvent _doEvent = new
System.Threading.ManualResetEvent(false);
public RunClass(string p, System.Threading.ManualResetEvent _event) {
_p = p;
_doEvent = _event;
}
public string NewString
{
set {_Internal = value;}
get {return _Internal;}
}
public void Run(object status)
{
int i = 0;
i = System.Convert.ToInt32(_p);
i++;
NewString = i.ToString();
_doEvent.Set();
}
}
}