c# - Save data in 2d dictionary? -
c# - Save data in 2d dictionary? -
what fastest way store info dictionary<string, int[][]>
, dictionary<int, dictionary<string, string>
file, later can imported , converted variables?
currently, utilize code (this dictionary<string, int[][]>
):
string savestring = ""; int = 0; foreach (keyvaluepair<string, int[][]> entry in data) { if (i > 0) savestring += "|"; savestring += entry.key + ":"; int j = 0; foreach (int[] x in entry.value) { if (j > 0) savestring += ";"; int k = 0; foreach (int y in x) { if (k > 0) savestring += ","; savestring += y; k++; } j++; } i++; } string dir = path.combine(directory.getcurrentdirectory(), config.savedirectory, config.savename); if (!directory.exists(dir)) { directory.createdirectory(dir); } file.writealltext(path.combine(dir, "data.txt"), savestring);
and although works, slow (and doesn't either).
what work better?
you're not looking @ place. problem not storing data, it's getting data.
with example, low 50 entries of int[30][30]
(~91600 string concatenations if looked correctly), take 6600ms! no storing involved, concatenation part. problem is, each time you're appending string, need start @ 0 , go all way end, wasting lot of time.
you can read schlemiel painter's algorithm joel spolsky larn more phenomenon.
to prepare this, utilize stringbuilder
, it's made these utilize cases. same dataset, speeds operation 6600ms 6ms.
so initial example, stringbuilder:
stringbuilder savestring = new stringbuilder(); int = 0; foreach (keyvaluepair<string, int[][]> entry in data) { if (i > 0) savestring.append("|"); savestring.append(":"); int j = 0; foreach (int[] x in entry.value) { if (j > 0) savestring.append(";"); int k = 0; foreach (int y in x) { if (k > 0) savestring.append(","); savestring.append(y); k++; } j++; } i++; } string dir = path.combine(directory.getcurrentdirectory(), config.savedirectory, config.savename); if (!directory.exists(dir)) { directory.createdirectory(dir); } file.writealltext(path.combine(dir, "data.txt"), savestring.tostring());
c# linq dictionary
Comments
Post a Comment