Note that there are some explanatory texts on larger screens.

plurals
  1. POThe cost of casting?
    primarykey
    data
    text
    <p>I have a key/value pairs mapped by hash (ID) in <code>Dictionary&lt;string, Dictionary&lt;string, string&gt;&gt;</code> called id2key_value. You can think of this as a way to represent database-like table with rows.</p> <p>I added some helper functions to ease the use of some basic datatypes by doing a cast, like</p> <pre><code>public int GetInt(string id, string key) { int value = 0; bool success = int.TryParse(map[id][key], out value); if (success) return value; else throw new InvalidOperationException( "Trying to obtain a non-integer value with GetInt()."); } </code></pre> <p>Well, I thought I was being clever when I came up with an idea of a "cast-cache", which basically holds already parsed objects, so I could skip the parsing of the string for int, bool, DateTime, etc., and simply cast them to appropriate datatype from the cache. Like,</p> <pre><code>public int GetInt(string id, string key) { if (cast_cache.ContainsKey(id) &amp;&amp; cast_cache[id].ContainsKey(key)) return (int) cast_cache[id][key]; int value = 0; bool success = int.TryParse(map[id][key], out value); if (success) { this.AddToCache(id, key, value); return value; } else throw new InvalidOperationException( "Trying to obtain a non-integer value with GetInt()."); } </code></pre> <p>The "cast-cache" is simply <code>Dictionary&lt;string, Dictionary&lt;string, object&gt;&gt;</code>.</p> <p>So, I did some performance testing with 10000 ints added to the map. Then I did one million random retrieves, with and without "cast-caching".</p> <p>It took 495(ms) without caching and 490(ms) with caching. I also ran a test with DateTime, there the difference was more significant, but less than I expect (~750(ms) non-cached vs ~500(ms) cached).</p> <p>Not (obviously) understanding the principle of a cast, how costly this operation is and why the performance is so close to the one that "deserializes" from string?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload