|
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?
|
|
|
|
|
Got'ya..
anyway, your solution isnt working.
i'll tell you what i found out maybe you'll know
in my code where i do
originNode.Remove();
the
targetNode.Nodes.Count doesnt change..
for Example, if i had 2 nodes under the targetNode, after the Remove function, still staying with 2..
|
|
|
|
|
I am using the following code to store public variables:
using System;
using System.Collections.Generic;
using System.Text;
namespace Takhlees
{
class public_class
{
private static string user_login;
public string login_user_login
{
get { return user_login; }
set { user_login = value; }
}
}
}
and I want to create a table called user_rights with user_right_code and user_right_value fields..... The user_right_value field will be a bit with True or False and the user_right_code will have something like:
ADD_OCCUPATION
EDIT_OCCUPATION
DELETE_OCCUPATION
BROWSE _OCCUPATIONS
BROWSE _GENDER
BROWSE_NATIONALITY
BROWSE_USERS
ADD_USER
EDIT_USER
RESET_PASSWORD
I want to know how can automatiocally retrive the user_right_code and user_right_value into public variables? do i have to create each and every code in the public_class?!!
|
|
|
|
|
Yes, in some form or fashion you need to have properties that tell you whether a particular user has a particular right. In this case you are relating a set of rights directly to a user. It is common, and may be beneficial, to use 'roles' where you construct a set of roles and each role has a set of rights associated with it, then a user is assigned a role rather than a set of individual priviledges. For example, a role named 'Admin' which has all the rights and one named 'ReadOnly' that only has the BROWSE rights. Choose whichever method works best for your needs.
As far as retrieving the rights, the best OOP way would be to have a User class and some kind of security class that would contain information about the user's rights. The User class would be a composite in that it would contain a copy of the security class. It would seem pretty straight-forward to have the User class create and fill the security object when the User class is filled with the User data (perhaps invoking a query or stored procedure to retrieve the rights data where UserID=X).
There are a myriad of ways of accomplishing this, I just gave one example to illustrate one way it could be done.
Hold on a second here... Don't you think you might be putting the horse ahead of the cart?
|
|
|
|
|
but can you please explain a bit how can i use my properties to store the user rights? now if I have sql reader to read all the rights wheer user_id = 7 for example... I should read two columns, user_right_code and user_right_value?!! sorry but even after 10 cups of coffee I am still confused....!!
|
|
|
|
|
I have a question.. is thete any way to have a variable called user_rights with a property for every right and its value? if that's possible then I can read the rights from the user_rights table and retrieve the following:
user_rights[ALLOW_CREATE_CLIENT, True)
user_rights[ALLOW_DELETE_CLIENT, False)
where ALLOW_CREATE_CLIENT and ALLOW_DELETE_CLIENT are properties of user_rights
where True and False are values for every property.
|
|
|
|
|
how can i know when a function is been added/removed from my Event?
I want to do some connection when a COM is add a function to my event.
public delegate void DoItDelegate(int i);
....
....
{
DoItDelegate MyFunction;
MyFunction += new DoItDelegate(DoItFunction);
}
public void DoItFunction(int i)
{
}
When the command : "MyFunction += new DoItDelegate(DoItFunction); " is been execute i want to do something in another function.
What can i do if i want to know when this "MyFunction += new DoItDelegate(DoItFunction);" is execute and register it to Datatable?
(The command "MyFunction += new DoItDelegate(DoItFunction);" is in the COM code and not in my code so i can't control it.)
|
|
|
|