Actually a subject. Are there any ways? There is a method, it must be performed many times, the only difference is that for each method there are different input data. I would like to increase the performance of performing these methods in parallel.
2 answers
As far as I know, at the moment only two types of parallelism have been invented: through processes and through threads.
|
Use Timer.
using System; using System.Threading; namespace Timers { class Program { int TimesCalled = 0; void Display (object state) { Console.WriteLine("{0} {1}", (string)state, ++TimesCalled); } static void Main( ) { Program p = new Program(); Timer myTimer = new Timer (p.Display, "Обработка", 2000, // Первый раз метод будет вызван через 2 секунды 1000); // Повтор каждую секунду Console.WriteLine("Таймер запущен."); Console.ReadLine(); } } }
When time expires, the timer calls the method in a specific thread, assigning to it the state argument (in our case, the string "Processing").
In this example, every second, starting from the second, the lines will be displayed:
"Обработка" 1 "Обработка" 2 "Обработка" 3 "Обработка" 4 "Обработка" 5 ...
In order for a timer to call a method, it (the method) must correspond to the TimerCallback delegate:
void TimerCallback(object state)
- oneIs it with a timer then increase productivity? Nuno - renegator
- I think the creators of Timer, tried to implement it in such a way that YOU will be able to realize it quickly, then: why reinvent the wheel? - megacoder
- Speech that Timer calls a method consistently, instead of in parallel, and does it in one flow. - AlexeyM 4:04
- oneIt's not about implementing your timer. The questioner wants to increase productivity by implementing pseudo-parallel execution of several (hundreds) functions. He does not want to use the regular thread mechanism, because he suspects that it is too expensive. By the time I think so. And your functions are called on a timer once a second. And all this second, the computer is kicking vanya - renegator
|
ThreadPool
automatically performs synchronization and switching between threads, so you can not worry about it, but their number is large enough not to increase, because you can only degrade the performance ofPLINQ
andParallel
very good job with data independent of each other and work well if you have more than one processor google in more detail ... - Specter