{"id":749,"date":"2009-02-05T11:26:15","date_gmt":"2009-02-05T10:26:15","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=749"},"modified":"2009-02-08T23:50:50","modified_gmt":"2009-02-08T22:50:50","slug":"net-teljesitmenyhangolasi-tapasztalatok-4","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2009\/02\/05\/net-teljesitmenyhangolasi-tapasztalatok-4\/","title":{"rendered":".NET teljes\u00edtm\u00e9nyhangol\u00e1si tapasztalatok 4."},"content":{"rendered":"<p>\u00d3vakodj az enumt\u00f3l, mert lass\u00fa l\u00e9szen az &#8211; \u00e1ll a .NET bibli\u00e1ban.<\/p>\n<p>\u00c1rtatlan kis j\u00f3sz\u00e1gnak n\u00e9z ki ez az enum, m\u00e9gis sokszor l\u00e1ttam m\u00e1r, hogy miatta lassul be egy rendszer. Mi a lass\u00fa rajta? Minden. A Parsol\u00e1s pl. De ez m\u00e9g \u00e9rthet\u0151 is. De hogy a ToString() is tet\u0171, az m\u00e1r kev\u00e9sb\u00e9. Sajnos azonban reflectiont haszn\u00e1l ezekhez a m\u0171veletekhez, ami k\u00f6ztudottan lass\u00fa.<br \/>\nSzerencs\u00e9re el\u00e9g k\u00f6nny\u0171 seg\u00edteni a baj\u00e1n. Ha Parsolni kell, azaz egy string alapj\u00e1n kell egy enum \u00e9rt\u00e9ket megsz\u00fclni, akkor ezt egyszer\u0171en meg lehet oldani egy Dictionary-vel, ami string kulcsokkal \u00e9s az konkr\u00e9t enum \u00e9rt\u00e9kekkel van felt\u00f6ltve.<br \/>\nMost a m\u00e1sik oldalt mutatom meg, a turb\u00f3 ToString()-et.<br \/>\nEgyszer\u0171 dem\u00f3 enumunk:<\/p>\n<p>public enum TradeDirection<br \/>\n{<br \/>\n    Short,<br \/>\n    Long<br \/>\n}<\/p>\n<p>Ebb\u0151l teh\u00e1t ha van egy p\u00e9ld\u00e1nyunk amin ToString()-et h\u00edvunk, az lass\u00fa lesz. Hozzunk l\u00e9tre egy Dictionary-t, ami seg\u00edt az enum-string asszoci\u00e1ci\u00f3ban.<\/p>\n<p>private static Dictionary<TradeDirection, string> tradeDirectionNames = new Dictionary<TradeDirection, string>();<\/p>\n<p>Ezt kellene fel\u00f6lteni \u00e9rt\u00e9kekkel. A C# generikus dolgaival sz\u00e9pen meg lehet ezt \u00e1ltal\u00e1nosan is fogalmazni, \u00edgy mindenf\u00e9le enumra m\u0171k\u00f6dni fog:<\/p>\n<p>tradeDirectionNames.FillEnumCache();<\/p>\n<p>public static void FillEnumCache<T>(this Dictionary<T, string> enumNames)<br \/>\n{<br \/>\n    foreach (T enumValue in Enum.GetValues(typeof(T)))<br \/>\n    {<br \/>\n        enumNames.Add(enumValue, enumValue.ToString());<br \/>\n    }<br \/>\n}<\/p>\n<p>Mivel extension methodk\u00e9nt \u00edrtam meg olyan, mintha a Dictionary tudn\u00e1 ezt a funkci\u00f3t, a T t\u00edpusparam\u00e9tert meg kital\u00e1lja a compiler, \u00edgy nem kell ki\u00edrjam a teljes alakot:<\/p>\n<p>tradeDirectionNames<TradeDirection>.FillEnumCache();<\/p>\n<p>Hogy jogos-e erre az extension method az objektumorient\u00e1lt szempontb\u00f3l er\u0151sen vitathat\u00f3, de nekem \u00edgy k\u00e9zre esett.<\/p>\n<p>A generiz\u00e1lhat\u00f3s\u00e1g viszont szenz\u00e1ci\u00f3s. Ez\u00e9rt im\u00e1dom a C#-ot.<\/p>\n<p>Miut\u00e1n a fenti megold\u00e1ssal gyors\u00edtottam a rendszeren kider\u00fclt (profilerrel, mi m\u00e1ssal), hogy egy \u00f6sszetett t\u00edpusban ami k\u00e9t enumb\u00f3l \u00e1llt lass\u00fa volt a GetHashCode implement\u00e1ci\u00f3m, ami \u00edgy n\u00e9zett ki:<\/p>\n<p>public override int GetHashCode()<br \/>\n{<br \/>\n    return dir.GetHashCode() + zone.GetHashCode();<br \/>\n}<\/p>\n<p>A dir \u00e9s a zone egy-egy enum.<\/p>\n<p>A gyors\u00edt\u00e1shoz \u00edgy \u00edrtam \u00e1t:<\/p>\n<p>public override int GetHashCode()<br \/>\n{<br \/>\n    return (int)dir << 4 + (int)zone;\n}\n[\/source]\n\nEz nem csak gyorsabb, de jobban is sz\u00f3r, mint az el\u0151z\u0151.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00d3vakodj az enumt\u00f3l, mert lass\u00fa l\u00e9szen az &#8211; \u00e1ll a .NET bibli\u00e1ban. \u00c1rtatlan kis j\u00f3sz\u00e1gnak n\u00e9z ki ez az enum, m\u00e9gis sokszor&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10,70,4],"tags":[],"class_list":["post-749","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-optimalizalas","category-szakmai-elet"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=749"}],"version-history":[{"count":6,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/749\/revisions"}],"predecessor-version":[{"id":766,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/749\/revisions\/766"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}