c# - Why is dictionary lookup slower than recursive search? -
i trying optimize search node in treenodecollection
. original method uses recursive approach:
public ugtreenode findnode(baseid nodeid, treenodecollection nodes) { foreach (ugtreenode n in nodes) { if (n.node_info.description.base_id.id == nodeid.id && n.node_info.description.base_id.rootid == nodeid.rootid && n.node_info.description.base_id.subid == nodeid.subid) return n; ugtreenode n1 = findnode(nodeid, n.nodes); if (n1 != null) return n1; } return null; }
i tried storing nodes in dictionary
, use dictionary.trygetvalue
search node:
public ugtreenode findnode(baseid nodeid, treenodecollection nodes) { ugtreenode res; _nodescache.trygetvalue(nodeid.id, out res); return res; }
but turns out second approach way slower first (appoximately 10 times slower). possible reasons this?
the recursion may faster when search 1 of first items in tree. depends on equals
implementation of baseid
or comparer use in dictionary. in recursive method, have reference comparison. dictionary uses gethashcode
, equals
.
how did measure performance?
Comments
Post a Comment