Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
GeneralRe: CODE Pin
Renven30-Dec-09 3:27
Renven30-Dec-09 3:27 
GeneralRe: CODE Pin
Renven30-Dec-09 3:44
Renven30-Dec-09 3:44 
GeneralRe: CODE Pin
Luc Pattyn30-Dec-09 3:49
sitebuilderLuc Pattyn30-Dec-09 3:49 
GeneralRe: CODE Pin
harold aptroot30-Dec-09 3:49
harold aptroot30-Dec-09 3:49 
GeneralRe: CODE Pin
Daniel Grunwald30-Dec-09 3:50
Daniel Grunwald30-Dec-09 3:50 
GeneralRe: CODE Pin
Renven30-Dec-09 4:03
Renven30-Dec-09 4:03 
GeneralRe: CODE Pin
Luc Pattyn30-Dec-09 4:38
sitebuilderLuc Pattyn30-Dec-09 4:38 
GeneralRe: CODE Pin
Daniel Grunwald30-Dec-09 4:42
Daniel Grunwald30-Dec-09 4:42 
You should be able to speed it up by another factor of 5 using harold's advice (use a single LockBits call and unsafe code inside the loop).

Once you've done that, you can still make it much faster by using a more intelligent algorithm for finding the averages: cumulative array sums.

It's easiest to explain in one dimension:
If you have one int-array and need to run multiple queries "give me the average of all elements from index a to b", then it's faster to preprocess the array by building the cumulative sum:
Original array = { 10, 20, 30, 40 }
Cumulative sums = { 10, 30, 60, 100 }
Every entry in the new array is the sum of all previous entries in the original array. The new array can be calculated in a single O(N) loop.

You can find the sum of any contiguous index range in the original array just by subtracting the end points in the cumulative sum array. The sum of 20+30+40 is 100-10.

This scheme can be extended into more than one dimension: for two dimensional images, create an array where every entry is the sum of all original entries to the top left of it. Such a new array can be calculated in O(N*M) if you first calculate the one-dimensional cumulative sums for one dimension and then for the other dimension.
Then you can calculate the sum of any rectangle easily: sum of all values in some rectangle = sums[bottom, right] - sums[bottom, left] - sums[top, right] + sums[top, left]
So using cumulative sums, you'll be able to calculate the average color of an rectangle using just 4 memory accesses instead of 900 memory access (30*30 pixels).

None of this is related to threading. I don't know why the multithreaded version is slower in your case (maybe due locking inside of the GetPixel calls?). But multithreading usually won't give you as much of an speedup as using a better algorithm, so keep your program single-threaded when possible. Multiple threads can introduce lots of complexity and subtle bugs for little benefit.
GeneralRe: CODE Pin
Luc Pattyn30-Dec-09 4:58
sitebuilderLuc Pattyn30-Dec-09 4:58 
AnswerRe: Threads, speed up calculations Pin
#realJSOP30-Dec-09 2:27
mve#realJSOP30-Dec-09 2:27 
GeneralRe: Threads, speed up calculations Pin
Ben Fair30-Dec-09 3:19
Ben Fair30-Dec-09 3:19 
GeneralRe: Threads, speed up calculations Pin
Renven30-Dec-09 4:05
Renven30-Dec-09 4:05 
QuestionIntegrate sql server's Query Editor Window with C# Project Pin
Dot-Net-Dev30-Dec-09 0:50
Dot-Net-Dev30-Dec-09 0:50 
AnswerRe: Integrate sql server's Query Editor Window with C# Project Pin
Eddy Vluggen30-Dec-09 4:03
professionalEddy Vluggen30-Dec-09 4:03 
Questionhow to make web application using multipoint sdk Pin
krunal2529-Dec-09 23:45
krunal2529-Dec-09 23:45 
AnswerRe: how to make web application using multipoint sdk Pin
Jimmanuel30-Dec-09 3:02
Jimmanuel30-Dec-09 3:02 
QuestionTreeview doesn't update after treenode.remove Pin
eyalle29-Dec-09 23:14
eyalle29-Dec-09 23:14 
AnswerRe: Treeview doesn't update after treenode.remove Pin
Luc Pattyn30-Dec-09 0:44
sitebuilderLuc Pattyn30-Dec-09 0:44 
GeneralRe: Treeview doesn't update after treenode.remove Pin
eyalle30-Dec-09 0:48
eyalle30-Dec-09 0:48 
GeneralRe: Treeview doesn't update after treenode.remove Pin
Luc Pattyn30-Dec-09 1:25
sitebuilderLuc Pattyn30-Dec-09 1:25 
AnswerRe: Treeview doesn't update after treenode.remove Pin
Ben Fair30-Dec-09 3:25
Ben Fair30-Dec-09 3:25 
GeneralRe: Treeview doesn't update after treenode.remove Pin
eyalle30-Dec-09 3:28
eyalle30-Dec-09 3:28 
GeneralRe: Treeview doesn't update after treenode.remove Pin
Ben Fair30-Dec-09 3:32
Ben Fair30-Dec-09 3:32 
GeneralRe: Treeview doesn't update after treenode.remove Pin
eyalle30-Dec-09 3:38
eyalle30-Dec-09 3:38 
Questionretrive user rights from sql table Pin
Jassim Rahma29-Dec-09 22:53
Jassim Rahma29-Dec-09 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.