|
|
I mean, adding one textbox and while to running to the textbox in the timers range 0-2000 millisecond
modified 11-Sep-14 7:50am.
|
|
|
|
|
I have an object[]. I need to convert it to IEnumerable T **FAST**.
Right now I have (this part is called ONCE and EnumFactory is cache):
MethodInfo mi = typeof(Enumerable).GetMethod("CastIterator", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(from);
ParameterExpression[] param = new ParameterExpression[] { Expression.Parameter(typeof(object[])) };
EnumFactory = Expression.Lambda<Func<object[], object>>(Expression.Call(mi, param), param).Compile();
Then I call it as (this part is called millions of times):
return EnumFactory(oArray);
Most of the examples you'll see call typeof(Enumerable).GetMethod("Cast"), but if you look at that in reflector, you'll see it does a few useless checks and then calls CastIterator. To save time on the useless checks and the extra method call, I call CastIterator directly.
Any other options? EnumFactory is, of course, cached. If I comment out the part that fills in oArray, its negligible compared to calling EnumFactory. I do not know T at compile time.
|
|
|
|
|
How slow is a simple for -loop ?
private static T[]
CastArray<T>
(
object[] Value
)
{
T[] result = new T [ Value.Length ] ;
for ( int i = 0 ; i < result.Length ; i++ ) result [ i ] = (T) Value [ i ] ;
return ( result ) ;
}
It works with int s, does it work with what you have? This should be your baseline. How much faster is your technique?
I suppose the bigger question is why you have an object array in the first place.
|
|
|
|
|
I don't know T until runtime, so I would have to do the same thing of making a generic method and calling it through a delegate / invoke / expression tree.
However, the above code you suggested will actually be "much" slower then Cast / CastIterator since you are new'ing up the T array every time. CastIterator is simply:
private static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source)
{
foreach (object current in source)
{
yield return (TResult)((object)current);
}
yield break;
}
Unless of course, the yield keyword is expensive... but from my understanding of how it works internally, it should be MUCH, MUCH faster then the method above since its lazy (and it doesn't new up anything).
I know I'm nitpicking lol, but really trying to squeeze everything out of this block of code.
|
|
|
|
|
If you don't know the type until runtime, why not make it IEnumerable<dynamic>. When you run this, it will appear that you have an IEnumerable of objects (as you have now), but with the proviso that because it's dynamic, it will honour any method calls you care to make on that object. I haven't tested the performance of this, but that might be worth a try for you.
|
|
|
|
|
Hmm... I need to return a strongly typed IEnumerable. If I return an IEnumerable<dynamic> off of the object[], it compiles, but I get a runtime cast error. I don't have control over the calling code. I'm just the library that serves it up.
|
|
|
|
|
SledgeHammer01 wrote: will actually be "much" slower
That may be, but unless you try it (and/or the Enumerator you show) you'll never know for sure -- you're only acting on hearsay or prejudice. Get a baseline reading, then try something else and get another reading, etc. You're assuming that what you have is "much" faster, but it may not be, you need metrics.
What are you trying to do?
Why are you accepting an object[] of them; can you correct this upstream?
And what actual types are involved? Value types? Structs? Small simple classes? Large complex classes?
SledgeHammer01 wrote: its lazy
Laziness may not help much unless the consumer doesn't need all the members.
And that cast to object looks redundant.
|
|
|
|
|
PIEBALDconsult wrote: That may be, but unless you try it (and/or the Enumerator you show) you'll never
know for sure -- you're only acting on hearsay or prejudice. Get a baseline
reading, then try something else and get another reading, etc. You're assuming
that what you have is "much" faster, but it may not be, you need metrics.
What are you trying to do? Why are you accepting an object[] of
them; can you correct this upstream? And what actual types are involved?
Value types? Structs? Small simple classes? Large complex classes?
Ok ... well, for the sake of the thread, I did... its not "much, much slower" as I thought it would be... but it is quite a bit slower (about 15%). 3900ms vs 4500ms
What I'm trying to do is given an array of some unknown (at compile time) object (which is why I store them as object[]), I need to convert said object[] into an IEnumerable T where T is known at compile time by the caller of my class.
I could potentially store a T[], but unfortunately, my class has non-T overloads. So its kind of like I need to support:
T GetTheObjects<T>();
IEnumerable<SomeClass> enum = GetTheObjects<SomeClass>();
object GetTheObjects(Type type);
IEnumerable<SomeClass> enum = (IEnumerable<SomeClass>)GetTheObjects(typeof(SomeClass));
If I only had to support the T overloads, I could avoid boxing / unboxing...
PIEBALDconsult wrote: And that cast to object looks redundant.
Agreed. That's how Reflector shows it. I tried c&p'ing that static method into my dll and removing the extra cast, but it didn't affect performance.
|
|
|
|
|
I'm new to C# and am working on first project.
I have a WINFORM that displays a treenode and a datagridview. I want selected items from the treenode to go to the datagridview. If I select the parent node of treenode all children go to datagridview but if I just expand the parent and select one of the children, nothing goes to datagrid. (selection of a treenode fires a method to get attributes of the item from sql table and it is the attributes that go to the grid + node value)
When parent is selected 'if (e.Node.Nodes.Count > 0)' is true. When parent isn't selected but child is 'if (e.Node.Nodes.Count > 0)' is false.
So my question is what code do I need to find child nodes that are checked/unchecked?
Once I get correct code to find child node check, what code moves it to datagrid or delete from datagrid if child is unchecked?
private void getChildNodesToGrid()
{
DataTable dt = getFieldsTable();
dgvColumns.DataSource = dt;
getAttributeSIDs();
}
private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
{
getFileAndColumns();
if (e.Node.Nodes.Count > 0)
{
this.CheckAllChildNodes(e.Node, e.Node.Checked);
e.Node.Expand();
foreach (TreeNode tn in e.Node.Nodes)
{
if (tn.Nodes.Count.Equals(0))
tn.Checked = e.Node.Checked;
}
getChildNodesToGrid();
}
|
|
|
|
|
RESOLVED:
Added an 'else' to the AfterCheck so it simply calls getChildNodesToGrid()
I then updated private DataTable getFieldsTable() to add a counter and condition 'if (fileNode.Nodes[cnt].Checked)'.
Don't know if this is proper programming but seems to work.
private void tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
{
getFileAndColumns();
if (e.Node.Nodes.Count > 0)
{
e.Node.Expand();
foreach (TreeNode tn in e.Node.Nodes)
{
if (tn.Nodes.Count.Equals(0))
tn.Checked = e.Node.Checked;
}
getChildNodesToGrid();
}
else
{
e.Node.Expand();
if (e.Node.Checked)
{
getChildNodesToGrid();
}
}
private DataTable getFieldsTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ColumnName");
dt.Columns.Add("FMFieldName");
dt.Columns.Add("FMFieldNumber");
dt.Columns.Add("FMFileNumber");
dt.Columns.Add("FMFieldType");
dt.Columns.Add("ResolvedValue");
dt.Columns.Add("PointsToFileNumber");
TreeNode fileNode = tvFileMan.SelectedNode;
int cnt = 0;
foreach (TreeNode tn in fileNode.Nodes)
{
if (tn.Nodes.Count == 0)
{
if (fileNode.Nodes[cnt].Checked)
{
DataRow dr = dt.NewRow();
dr["FMFieldName"] = tn.Text.Substring(tn.Text.IndexOf(" - ") + 4);
dr["FMFieldNumber"] = tn.Tag.ToString();
dr["FMFileNumber"] = tn.Parent.Tag.ToString();
dr["ColumnName"] = suggestName(tn.Text.Substring(tn.Text.IndexOf(" - ") + 4));
if (dr["PointsToFileNumber"].ToString().Length > 0)
{
dr["ColumnName"] = suggestName(tn.Text.Substring(tn.Text.IndexOf(" - ") + 4) + "txt");
}
dt.Rows.Add(dr);
}
cnt++;
}
}
return dt;
}
private void getFileAndColumns()
{
label4.Visible = false;
label5.Visible = false;
btAllFields.Visible = false;
cbComputed.Checked = false;
TreeNode node = tvFileMan.SelectedNode;
if (node == null) return;
if (node.Index == 0) return;
if (node.Nodes.Count > 0)
{
tbFileNumber.Text = node.Tag.ToString();
tbFileName.Text = node.Text.Substring(node.Text.IndexOf(" - ") + 4);
tbTableName.Text = "xxxx." + (suggestName(tbFileName.Text) + "F" + tbFileNumber.Text.Replace('.', 'x'));
label4.Text = "To select all fields push button to the right. \nOtherwise double-click fields to add them one at a time.";
label4.Visible = true;
btAllFields.Visible = true;
dgvColumns.DataSource = null;
dgvPKIENS.DataSource = getPKIENSTable();
getPKIENSIDs();
}
else
{
}
}
|
|
|
|
|
I want to create a simple chat app. I would like to host it on my server and allow one-to-many people to chat.
I'd like to do it myself as a learning experience. Can anyone point me in the right direction?
Thank you
If it's not broken, fix it until it is
|
|
|
|
|
Lots of examples here[^].
/ravi
|
|
|
|
|
LOL - Never thought to look here
DOH!
If it's not broken, fix it until it is
|
|
|
|
|
What kind of app are you looking to build - web or desktop? I favor desktop, which relieves one of having to deal with the intricacies of building an async web UI, but your needs may vary.
/ravi
|
|
|
|
|
I'd build the UI with WPF and host the server on my server.
If it's not broken, fix it until it is
|
|
|
|
|
You know, I'm fairly good with MVC 4 Web Services. Any reason I shouldn't use that?
If it's not broken, fix it until it is
|
|
|
|
|
No, I think that's a good idea. If your server talks JSON, you could also use it with native mobile clients.
/ravi
|
|
|
|
|
Agreed. And it should be fairly simple.
If it's not broken, fix it until it is
|
|
|
|
|
Have you considered using a Jabber server instead of writing one from scratch?
/ravi
|
|
|
|
|
Kevin Marois wrote: I'm fairly good with MVC 4 Web Services
Then it might be a good opportunity to teach rather than learn.
|
|
|
|
|
The part I'm not sure about is getting notifications in the client from the Server.
Ever done this?
If it's not broken, fix it until it is
|
|
|
|
|
Details. Details. Don't bother me with details.
(I don't even know what MVC 4 is.)
|
|
|
|
|
Several years ago I wrote one that was just WinForms-to-SQL-Server, then I started working on a WCF service for it (again, it was quite a while ago), but since then it has stagnated. It's something I'd like to finish for an article.
Calling it a "chat app" isn't terribly accurate, as it is something between that and a simple e-mail system. It supports message threads (like forum posts here on CP) and delayed delivery for recipient(s) who are offline.
Kevin Marois wrote: as a learning experience
Should be a good way to learn the communication framework du jour and allow clients to be created for a number of devices.
|
|
|
|
|
I'm thinking I could create a Web API that maintains a list of Conversations. Each Conversation would have a unique Id and a list of Participants.
The only problem I see is the API notifying clients that they have a message. I'm guessing a callback of some sort would do the trick.
If it's not broken, fix it until it is
|
|
|
|