c# - Switch vs dictionary with double keys -
i making unity game (c# 2.0) , first time requires best performance possible. when mission ends, player given amounts of resources depending on time factor held mission object. there 4 resources in total, using 4 16-long switches-on-int (time*1000), 1 switch per resource.
static int resource1reward(double time)  {    int time2 = time*1000;    switch (time2)    {     } }   looking @ posts decided try using 4 dictionaries.
 dictionary<double, int> resource1dic = new dictionary<double, int>(16);  static int resource1dicreward(double time)  {      return resource1[time];  }   using stopwatch, saw huge difference between calling (1) 4 methods; , (2) 1 method looked 4 dictionaries.
- 10 15 ms
 - 0 2 ms
 
the problem still have long static method initializes 4 dictionaries. have same keys (time switch) different values. there better/cleaner way this?
your business logic bit unclear, in regards dictionary object initialization/performance issue, solution below may serve valid alternative. pertinent use-case, can declare dictionary<double, int> shown in following sample c# code snippet:
public static  dictionary<double, int> sampledictionary =new dictionary<double, int>  {      { 3.1, 3},     { 3.14, 3},     { 3.1415, 3},     { 2.72, 2},     { 2.728, 2} };   it fast , rather straightforward. hope may help.
Comments
Post a Comment