C# non-evil fast array lookup? -



C# non-evil fast array lookup? -

i want have big number of class instances homecoming same similar fields of data, in illustration implementation:

foreach (someclass sc in someclasses) { system.console.writeline(sc.getdata("1st field")); system.console.writeline(sc.getdata("another field")); system.console.writeline(sc.getdata("and another")); } // ---- within someclass: dictionary<string, string> mydata; public string getdata(string field) { homecoming mydata[field]; }

what don't string hashing, lookup , matching has happen on , on 1 time again in illustration (i assume thats how dictionary works). find improve approach.

coming c world, thought of assigning fields unique integer key, such can alter array lookup:

// ---- within someclass: string[] mydata; public string getdata(int field_key) { homecoming mydata[field_key]; }

now field lookup efficient, doesn't sense right in these "arrays evil" times, , tedious , error prone deal field_key integer.

i don't know if i'm chasing performance ghosts here, want find design both efficient , clean.

suggestions?

because fields not known @ compile time, rather dynamic , user configurable, i'm going modify illustration programme utilize array of properties. i'd advocate approach similar yours using own custom class (here, called myproperty) rather string. performance @ to the lowest degree (and maybe tad improve than) string approach, benefit gives more flexibility: if decide performance reasons need utilize array or list approach, can embed array index myproperty class. you'd have alter implementation of getdata not calling code.

public static void test1() { someclass[] someclasses; //created somehow //in real life, determined dynamically var properties=new[] {someclass.firstfield, someclass.anotherfield, someclass.andanother}; foreach(var sc in someclasses) { foreach(var property in properties) { console.writeline(sc.getdata(property)); } } } public class someclass { public static readonly myproperty firstfield=new myproperty(); public static readonly myproperty anotherfield=new myproperty(); public static readonly myproperty andanother=new myproperty(); private readonly dictionary<myproperty, string> mydata=new dictionary<myproperty, string>(); public string getdata(myproperty property) { homecoming mydata[property]; } } //default implementation of equals , gethashcode fine here public class myproperty {}

however, since target application collecting set of dynamic , user configurable property getters, maybe want create funcs? code below fast, , still has ability want, namely allows create little dynamic, user-configurable list of property getters.

public static void test2() { someclass[] someclasses; //created somehow //in real life, determined dynamically var getters=new[] {someclass.firstfield, someclass.anotherfield, someclass.andanother}; foreach(var sc in someclasses) { foreach(var getter in getters) { system.console.writeline(getter(sc)); } } } public class someclass { public static readonly func<someclass, string> firstfield=sc => sc.field0; public static readonly func<someclass, string> anotherfield=sc => sc.field1; public static readonly func<someclass, string> andanother=sc => sc.field2; private string field0; private string field1; private string field2; }

c# arrays performance dictionary

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -