}//end method private static void FindStrMThread(SearchThread[] threads, String text) { int workLength = text.Length / NUM_THREADS; //run the threads for (int i = 0, start = 0; i < NUM_THREADS; i++, start += workLength) { //set the bounds of the string work area, if is last iteration //the work area may be larger or smaller. //if a word happens to be in the middle of the split operation it will not be counted threads[i] = new SearchThread(text, SEARCH_STR, start, (i != NUM_THREADS - 1) ? workLength : text.Length - start); threads[i].run(); }//end loop //wait for the threads to stop Parallel.For(0, NUM_THREADS, (int i) => { threads[i].thrd.Join(); }); Console.WriteLine("Number of occurrances of word \"{0}\" are {1}. ", SEARCH_STR, SearchThread.CountOccurrances(threads)); } //end method
public static int CountOccurrances(SearchThread[] threads) { //accumulate the total number of occurrances int total = 0; for (int i = 0; i < threads.Length; i++) total += threads[i].NumOccurrances; return total; }
}//end main private static void FindStrSingleThread(String text) { SearchThread thread = new SearchThread(text, SEARCH_STR, 0, text.Length); thread.run(); thread.thrd.Join(); Console.WriteLine("Number of occurrances of word \"{0}\" are {1}. ", SEARCH_STR, thread.NumOccurrances); }//end method
static void Main(string[] args) { SearchThread[] threads = new SearchThread[NUM_THREADS]; String text = File.ReadAllText(FILE_NAME); OptimizationTests test = new OptimizationTests(Console.Out); test.NumTimes = 1; test.TimeAction(FindStrMThread, threads, text); test.TimeAction(FindStrSingleThread, text); Console.Write("Press any key to continue... "); Console.ReadLine(); }//end main
static void Main(string[] args) { SearchThread[] threads = new SearchThread[NUM_THREADS]; String text = File.ReadAllText(FILE_NAME); OptimizationTests test = new OptimizationTests(Console.Out); test.NumTimes = 1; test.TimeAction(FindStrMThread, threads, text); test.TimeAction(FindStrSingleThread, text); Console.Write("Press any key to continue... "); Console.ReadLine(); }
private static void FindStrSingleThread(String text) { SearchThread thread = new SearchThread(text, SEARCH_STR, 0, text.Length); thread.run(); thread.thrd.Join(); Console.WriteLine("Number of occurrances of word \"{0}\" are {1}. ", SEARCH_STR, thread.NumOccurrances); }
private static void FindStrMThread(SearchThread[] threads, String text) { int workLength = text.Length / NUM_THREADS; //run the threads for (int i = 0, start = 0; i < NUM_THREADS; i++, start += workLength) { //set the bounds of the string work area, if is last iteration //the work area may be larger or smaller. //if a word happens to be in the middle of the split operation it will not be counted threads[i] = new SearchThread(text, SEARCH_STR, start, ( i != NUM_THREADS - 1 ) ? workLength : text.Length - start); threads[i].run(); }//end loop //wait for the threads to stop Parallel.For(0, NUM_THREADS, (int i) => { threads[i].thrd.Join(); }); Console.WriteLine("Number of occurrances of word \"{0}\" are {1}. ", SEARCH_STR, SearchThread.CountOccurrances(threads)); }