Performance issue using Multi threading in Net 3.5
-
Hello All, I'm using Dot Net 3.5 for performing tasks in parallel using below code but It's not showing expected performance i.e. if serial task takes 1 min then using multitasking/parallel tasking it will finish within 1 min or much less time but it takes same time. Code Snippet:
// Initialize the reset events to keep track of completed threads
ManualResetEvent[] resetEvents = new ManualResetEvent[arrFileName.Count];// Launch each method in it's own thread for (int i = 0; i < arrFileName.Count; i++) { resetEvents\[i\] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback((object index) => { int actionIndex = (int)index; // Execute the method ThreadExcecute(arrFileName\[actionIndex\]); // Tell the calling thread that we're done resetEvents\[actionIndex\].Set(); }), i); } // Wait for all threads to execute WaitHandle.WaitAll(resetEvents);
Any suggestion?
-
Hello All, I'm using Dot Net 3.5 for performing tasks in parallel using below code but It's not showing expected performance i.e. if serial task takes 1 min then using multitasking/parallel tasking it will finish within 1 min or much less time but it takes same time. Code Snippet:
// Initialize the reset events to keep track of completed threads
ManualResetEvent[] resetEvents = new ManualResetEvent[arrFileName.Count];// Launch each method in it's own thread for (int i = 0; i < arrFileName.Count; i++) { resetEvents\[i\] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback((object index) => { int actionIndex = (int)index; // Execute the method ThreadExcecute(arrFileName\[actionIndex\]); // Tell the calling thread that we're done resetEvents\[actionIndex\].Set(); }), i); } // Wait for all threads to execute WaitHandle.WaitAll(resetEvents);
Any suggestion?
KIDYA wrote:
king it will finish within 1 min or much less time but it takes same time.
This is not at all necessary. It depends on how much work your processor / memory are involved with and what data is shared between these threads.
WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
KIDYA wrote:
king it will finish within 1 min or much less time but it takes same time.
This is not at all necessary. It depends on how much work your processor / memory are involved with and what data is shared between these threads.
WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
Hello All, I'm using Dot Net 3.5 for performing tasks in parallel using below code but It's not showing expected performance i.e. if serial task takes 1 min then using multitasking/parallel tasking it will finish within 1 min or much less time but it takes same time. Code Snippet:
// Initialize the reset events to keep track of completed threads
ManualResetEvent[] resetEvents = new ManualResetEvent[arrFileName.Count];// Launch each method in it's own thread for (int i = 0; i < arrFileName.Count; i++) { resetEvents\[i\] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback((object index) => { int actionIndex = (int)index; // Execute the method ThreadExcecute(arrFileName\[actionIndex\]); // Tell the calling thread that we're done resetEvents\[actionIndex\].Set(); }), i); } // Wait for all threads to execute WaitHandle.WaitAll(resetEvents);
Any suggestion?
-
Thanks for reply ,that means performance is also based on processor speed/configuration, I'm sharing same dll for performing each task which are not sharing any same inputs,does this matters?
Whenever a context switch happens, the operating system performs instructions to save data from one thread and switches over to run a second thread. So yes, even threading will depend on processing capabilities. Threading does not imply processing speed will improve. Threading is useful in scenarios when you are going to perform a background task while you want the user to be involved with something else - for e.g. working on a UI which should be responsive to mouse / keyboard events.
WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial
-
Threading isn't actually a silver bullet, you know? Anyway it's impossible to tell where this goes wrong without seeing what "ThreadExcecute(arrFileName[actionIndex]);" is doing.
-
ThreadExcecute function is doing encryption of a files using third party dll. So depending upon file size it will vary the encryption time.But If I've run this in parallel for multiple files then definitely it suppose to take less time.
-
Maybe.. with an SSD, sure. With a HDD, it's less clear: the throughput isn't that high anyway, and reading many files at the same time can introduce some seek latency.
-
Thanks for reply ,that means performance is also based on processor speed/configuration, I'm sharing same dll for performing each task which are not sharing any same inputs,does this matters?
Threading is useful in situations when you want to perform tasks in the background e.g. process complex calculations while allowing the user to navigate across across web page. Threading do not necessarily mean performance will be improved.
WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial