|
(how code looks after trying with 7 threads (because captured image is divided in 7 rows)) I have to warn you CODE is not optimized. as example I show you only 2 thread methods: ThreadOneP and ThreadTwoP
public int TransferToMatrixThread()
{
temptest = 0;
CabX = CabX - 18;
CabY = CabY + 53;
CabXX = CabX - 484; //POSSITION
CabYY = CabY + 20;
for (int i = 0; i < 11; i++) //FILLS MATRIX WITH 0
{
for (int j = 0; j < 11; j++)
{
transfered[i, j] = 0;
}
}
ThreadStart ts = new ThreadStart(ThreadOneP);
ThreadStart ts2 = new ThreadStart(ThreadTwoP);
ThreadStart ts3 = new ThreadStart(ThreadThreeP);
ThreadStart ts4 = new ThreadStart(ThreadFourP);
ThreadStart ts5 = new ThreadStart(ThreadFiveP);
ThreadStart ts6 = new ThreadStart(ThreadSixP);
ThreadStart ts7 = new ThreadStart(ThreadSevenP);
Thread thread1 = new Thread(ts);
Thread thread2 = new Thread(ts2);
Thread thread3 = new Thread(ts3);
Thread thread4 = new Thread(ts4);
Thread thread5 = new Thread(ts5);
Thread thread6 = new Thread(ts6);
Thread thread7 = new Thread(ts7);
DateTime startTime = DateTime.Now; //TIME
thread1.Start();
thread2.Start();
thread3.Start();
thread4.Start();
thread5.Start();
thread6.Start();
thread7.Start();
thread1.Join();
thread2.Join();
thread3.Join();
thread4.Join();
thread5.Join();
thread6.Join();
thread7.Join();
DateTime stopTime = DateTime.Now;
TimeSpan duration = stopTime - startTime;
listBox1.Items.Add("7 THREAD = " + duration);
TransfX = currX + CabXX + 9; // norima nustatyti i pav viduri todel pridedamos konstantos
TransfY = currY + CabYY + 11; // norima nustatyti i pav viduri todel pridedamos konstantos
return temptest;
}
public void ThreadOneP()
{
int o = CabYY;
int zy = 8;
int zx = 2;
for (int ok = CabXX; ok < CabXX + 490; ok = ok + 72)
{
var color22 = RGBreturnLVL(ok, o, 30, 30, bmpScreenshot51);
if (maze[0, 1] < color22.R && color22.R < maze[0, 2] && maze[0, 3] < color22.G && color22.G < maze[0, 4] && maze[0, 5] < color22.B && color22.B < maze[0, 6])
{ transfered[zy, zx] = maze[0, 0]; }
else if (maze[3, 1] < color22.R && color22.R < maze[3, 2] && maze[3, 3] < color22.G && color22.G < maze[3, 4] && maze[3, 5] < color22.B && color22.B < maze[3, 6])
{ transfered[zy, zx] = maze[3, 0]; }
else { transfered[zy, zx] = 0; temptest++; }
zx++;
}
}
public void ThreadTwoP()
{
int o = CabYY - 72;
int zy = 7;
int zx = 2;
for (int ok = CabXX; ok < CabXX + 490; ok = ok + 72)
{
var color22 = RGBreturnLVL(ok, o, 30, 30, bmpScreenshot61);
if (maze[0, 1] < color22.R && color22.R < maze[0, 2] && maze[0, 3] < color22.G && color22.G < maze[0, 4] && maze[0, 5] < color22.B && color22.B < maze[0, 6])
{ transfered[zy, zx] = maze[0, 0]; }
else if (maze[3, 1] < color22.R && color22.R < maze[3, 2] && maze[3, 3] < color22.G && color22.G < maze[3, 4] && maze[3, 5] < color22.B && color22.B < maze[3, 6])
{ transfered[zy, zx] = maze[3, 0]; }
else { transfered[zy, zx] = 0; temptest++; }
zx++;
}
}
waiting your response
|
|
|
|
|
Some comments:
1.
please show code in PRE tags (e.g. use "code block" widget) for better readability
2.
your threads share the transfered[] array, i.e. they each fill one row. For performance, I would avoid 2-D arrays, give them each a 1-D array.
3.
you are abusing the var keyword; if color22 is a Color, then declare it a Color (for readability).
4.
as there are only a few iterations in the for loop, most time is spent inside RGBreturnLVL(). So one may want to see that code too.
5.
if threads do almost identical things, they don't need separate code; make a simple class that describes one threaded job, holding its code and data.
6.
if is not running on the main thread (the name seems to indicate so), then listBox1.Items.Add("7 THREAD = " + duration); is NOT allowed. See this article.[^]
7.
assuming there is no blocking system call (e.g. a file/network read) inside your threaded code, it does not make sense to have more calculation threads than there are CPUs. Threading overhead can make it slower.
8.
seems like maze[] is pretty constant, and only a few elements are needed here; I would copy them in local variables once, and use those all the time.
|
|
|
|
|
Thanks I will look into your comments, change code and then paste
|
|
|
|
|
AND, YES most of time is spend in RGBreturnLVL(...) method.
//Calculate average 30x30 image color
private Color RGBreturnLVL(int PositionX, int PositionY, int width, int height, Bitmap Image)
{
int ra = 0;
int ga = 0;
int ba = 0;
int total = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Color clr = CropBitmap(Image, PositionX, PositionY, width, height).GetPixel(x, y);
ra += clr.R;
ga += clr.G;
ba += clr.B;
total++;
}
}
ra /= total;
ga /= total;
ba /= total;
return Color.FromArgb(ra, ga, ba);
}
|
|
|
|
|
I asked you to use PRE tags.
and that code refers to yet another method.
and doesn't look very efficient.
|
|
|
|
|
GetPixel is a performance disaster, you should really use LockBits and unsafe code (that's what GetPixel does, too, but it does it every time you call it)
Using LockBits once and then using unsafe code to read the pixels is several orders of magnitude faster than using GetPixel
|
|
|
|
|
CropBitmap creates a copy of some sub-range of the bitmap?
If so, you are creating one copy for each pixel you read! That looks like an incredible waste of time. Move that CropBitmap call out of your loop.
Or try to get rid of it entirely; use Image.GetPixel(x + PositionX, y + PositionY) instead.
|
|
|
|
|
Daniel Grunwald thanks a lot!
results after changing :
without threads : 50-70ms
with threads: ~115ms
significant change, i do not expected to save that much time.
|
|
|
|
|
as long as the threaded version is slower, you're doing things the wrong way.
|
|
|
|
|
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.
|
|
|
|
|
Daniel Grunwald wrote: keep your program single-threaded when possible
My general advice is slightly different: first optimize using a single thread, then consider adding one or a few threads.
|
|
|
|
|
Why don't you use a thread pool instead of all that start/join crap?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
I was thinking the same thing! Also, it looks like the methods that do the work are almost identical (at least the two that were posted), so I'd abstract that logic out into its own method and pass in just the data that changes between rows.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
|
|
|
|
|
|
Hi,
I want to use SQL Server's "Query Editor" in my C# project, can i integrate it in C# Project.
Thanks,
Sonia
|
|
|
|
|
Sonia Shahid wrote: I want to use SQL Server's "Query Editor" in my C# project, can i integrate it in C# Project.
You're referring to the QBE-editor where you can modify the query with the mouse? That's not a separate component, I don't think that you can load it
I are Troll
|
|
|
|
|
can anyone please tell me how to build a website which has multipoint sdk functionality
please don't tell me about www.mymousegames.com , i want to create simple website
please reply
thanks in advance
|
|
|
|
|
Have you read the docs[^] (bottom of the page)? There's also a link to the multipoint developer forum where you might be able to find better help with this.
|
|
|
|
|
I've written the following code to remove a node from a treeview. The node data isremoved, butthe node is still there. Apparently I need to force a refresh, as the selected node does go away when I navigate away and back to the page.
Can anyone help?
Thanks
Eyal
int remove = SchemaDB.DeleteGenericLink(originNode.Tag),TargetNode.Tag);
if (remove)
{
originNode.Remove();
}
|
|
|
|
|
is this code executing on the main thread? if not, you're in trouble. see this[^].
|
|
|
|
|
i have only one thread.
so i guess it does runs from the main thread..
|
|
|
|
|
eyalle wrote: i guess
not very scientific; you could check using Control.InvokeRequired or by looking at Thread.GetCurrentThread().ThreadID.
eyalle wrote: i have only one thread.
so ...
unless you have asynchronous handlers, moat of them run from a threadpool thread (most timers, SerialPort.DataReceived, etc).
|
|
|
|
|
The easiest thing would be to call the TreeView.Invalidate() to have it redraw itself.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
|
|
|
|
|
what to you mean by
putting the horse ahead of the cart?
|
|
|
|
|
That's from my signature, it's not related to your inquiry. However, there is a saying in the United States (and perhaps other English-speaking countries) of "putting the cart ahead of the horse" in other words, doing things in the wrong order. I just reversed them to where they are back in the correct order because I'm a goofball.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
|
|
|
|
|