0

I am trying to replace a text in a string with Dictionary Value , if it is matched with the Dictionary Key. Select * from Table1 where column1 ={Value1} and Column2 = {value2}.

mydict.Add({value1},OriginalValue1);
mydict.Add({value2},OriginalValue2);

I can Iterate through the dictionary keys and replace the text in the string like ,But this is going to effect the performance if there are more than 100 items in the Dictionary object.

foreach(string key in mydict.keys)
{
if(Query.Contains(key)
{
 //Replace the string
}

Is there a way to achieve this with least effect to the Performance?

4
  • 1
    What makes you think it's going to have a large enough affect on performance to matter? The Dictionary class is FAST. Commented May 12, 2016 at 20:52
  • If your dictionary contains "change a to b" and "change b to a", what do you expect when the dictionary applies to "ab"? Output "ba" or "aa"? Commented May 12, 2016 at 20:58
  • Actually the use of a dictionary should not make much difference since the OP is looping through all of the keys anyways. Dictionaries are fast when searching for a specific key. Commented May 12, 2016 at 21:35
  • It's considered bad form to design for performance. Build, and then consider performance as needed. Commented May 12, 2016 at 22:08

2 Answers 2

1

First the warning: don't complicate things in the hopes of optimizing before you know that you actually have a problem with performance. 100 replacements does not sound like a big deal to me. Usually code readability and spending time on solving real problems is of greater value than those saved 10ns.

Assuming every nanosecond really-really matters, then you should measure your baseline and consider options for improvement:

  • start with built-in simple tools like String.Replace(). They are internally usually more optimized than you can do on your own (unless you know some important extra constraints on input or desired behaviour)
  • don't do the extra Contains(key) before replacing as you need to search the exact position for replace anyway. Or reuse the first pass like using String.IndexOf(..)) if you choose to work on indexes.
  • If your keys are of similar recognizable pattern (ex: "Key1", "Key2", etc) then maybe you could do all replacements in one pass using a compiled Regex replace tools?
  • Is the input query perhaps fixed? StringBuilder.Append() is probably faster than 100x search&replace.
  • Why use dictionary if you need to iterate all pairs? Skip the hash-magic adn extra objects in heap and loop over simple key-value pairs. Not that you'll feel a notable difference in performance..

And again - measure! Use a .net profiler and measure where the real bottlenecks are and determine what works best for your specific scenario.

Whatever non-trivial solution you decide to use, remember that the next guy to maintain your code may know where you live.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for responding to my question. I was just hoping I could get solution like you have mentioned. But I am still in the process of learning about Regex tools //If your keys are of similar recognizable pattern (ex: "Key1", "Key2", etc) then maybe you could do all replacements in one pass using a compiled Regex replace tools?//
Yes. something like that
1

Just an example of speed and dictionary performance. This is one thread on a 4 year old i7-3820

  • Found 1 in string "some 11998948 input" in time 0ms
  • Found 4 in string "some 11998948 input" in time 0ms
  • Found 8 in string "some 11998948 input" in time 1ms
  • Found 9 in string "some 11998948 input" in time 1ms
  • Found 11 in string "some 11998948 input" in time 1ms
  • Found 19 in string "some 11998948 input" in time 1ms
  • Found 48 in string "some 11998948 input" in time 1ms
  • Found 89 in string "some 11998948 input" in time 1ms
  • Found 94 in string "some 11998948 input" in time 1ms
  • Found 98 in string "some 11998948 input" in time 1ms
  • Found 99 in string "some 11998948 input" in time 1ms
  • Found 119 in string "some 11998948 input" in time 1ms
  • Found 199 in string "some 11998948 input" in time 1ms
  • Found 894 in string "some 11998948 input" in time 1ms
  • Found 948 in string "some 11998948 input" in time 2ms
  • Found 989 in string "some 11998948 input" in time 2ms
  • Found 998 in string "some 11998948 input" in time 2ms
  • Found 1199 in string "some 11998948 input" in time 2ms
  • Found 1998 in string "some 11998948 input" in time 2ms
  • Found 8948 in string "some 11998948 input" in time 3ms
  • Found 9894 in string "some 11998948 input" in time 3ms
  • Found 9989 in string "some 11998948 input" in time 3ms
  • Found 11998 in string "some 11998948 input" in time 4ms
  • Found 19989 in string "some 11998948 input" in time 5ms
  • Found 98948 in string "some 11998948 input" in time 21ms
  • Found 99894 in string "some 11998948 input" in time 21ms
  • Found 119989 in string "some 11998948 input" in time 25ms
  • Found 199894 in string "some 11998948 input" in time 42ms
  • Found 998948 in string "some 11998948 input" in time 214ms
  • Found 1199894 in string "some 11998948 input" in time 255ms
  • Found 1998948 in string "some 11998948 input" in time 400ms
  • Found 11998948 in string "some 11998948 input" in time 2127ms

That means going through ~12 million elements takes roughly 2 seconds. the dictionary is not your problem. (but I think partial matches might be)

I ran it with this code.

Dictionary<string, string> dic = new Dictionary<string, string>();
for (int i = 0; i < 11998949; i++) //11998949 is max supported range
{
    dic.Add(i.ToString(), i.ToString());
}

Stopwatch sw = new Stopwatch();
sw.Start();
string Query = "some 11998948 input"; 
foreach(var a in dic.Where(a=> Query.Contains(a.Key)))
{
    Console.WriteLine($"Found {a.Key} in string {Query} in time {sw.ElapsedMilliseconds}ms");
}
Console.ReadKey();

2 Comments

This is interesting. I did not know that dictionary is faster. Thank you for this detailed explanation.
Well dic is probably slower than multiple other collection types, but it is still blazing fast, and you get the hashmap feature on keys being a uniqe object instead of just a index number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.