ProblemI would like to generate Braille characters from a dictionary as below which store the n-th active bits:-private static readonly int BrailleStringSize = 6;Dictionary<char, int[]> BrailleDictionary = new Dictionary<char, int[]>{    { 'a', new int[] { 1 } },               // 100000    { 'b', new int[] { 1, 2 } },            // 110000    { 'c', new int[] { 1, 4 } },            // 100100    { 'd', new int[] { 1, 4, 5 } },         // 100110    { 'e', new int[] { 1, 5 } },            // 100010    { 'f', new int[] { 1, 2, 4 } },         // 110100    { 'g', new int[] { 1, 2, 4, 5 } },      // 110110    { 'h', new int[] { 1, 2, 5 } },         // 110010    { 'i', new int[] { 2, 4 } },            // 010100    { 'j', new int[] { 2, 4, 5 } },         // 010110    { 'k', new int[] { 1, 3 } },            // 101000    { 'l', new int[] { 1, 2, 3 } },         // 111000    { 'm', new int[] { 1, 3, 4 } },         // 101100    { 'n', new int[] { 1, 3, 4, 5 } },      // 101110    { 'o', new int[] { 1, 3, 5 } },         // 101010    { 'p', new int[] { 1, 2, 3, 4 } },      // 111100    { 'q', new int[] { 1, 2, 3, 4, 5 } },   // 111110    { 'r', new int[] { 1, 2, 3, 5 } },      // 111010    { 's', new int[] { 2, 3, 4 } },         // 011100    { 't', new int[] { 2, 3, 4, 5 } },      // 011110    { 'u', new int[] { 1, 3, 6 } },         // 101001    { 'v', new int[] { 1, 2, 3, 6 } },      // 111001    { 'w', new int[] { 2, 4, 5, 6 } },      // 010111    { 'x', new int[] { 1, 3, 4, 6 } },      // 101101    { 'y', new int[] { 1, 3, 4, 5, 6 } },   // 101111    { 'z', new int[] { 1, 3, 5, 6 } }       // 101011};in which the a Braille character is a string with length of 6 while 0 and 1 represents bumps and dots.Algorithm 1private static void PrintBrailleString1(){    foreach (var entry in BrailleDictionary)    {        var output = new StringBuilder();        for (int i = 1; i <= BrailleStringSize; i++)        {            output.Append(Array.IndexOf(entry.Value, i) > -1 ? '1' : '0');        }        Console.WriteLine(${ entry.Key } => { output });    }}This solution is quite naive but run FASTEST among the 3.Algorithm 2private static void PrintBrailleString2(){    foreach (var entry in BrailleDictionary)    {        var output         = new StringBuilder();        var activeBitQueue = new Queue<int>(entry.Value);        for (int i = 1; i <= BrailleStringSize && output.Length != 6; i++)        {            if (activeBitQueue.Count != 0)            {                var top = activeBitQueue.Peek();                if (i < top)                {                    output.Append('0');                }                else if (i == top)                {                    output.Append('1');                    activeBitQueue.Dequeue();                }            }            else            {                output.Append(new string('0', BrailleStringSize - i + 1));            }        }        Console.WriteLine(${ entry.Key } => { output });    }}Here I attempt to skip unnecessary call of Array.IndexOf but the solution turns out really ugly and performs moderately.Algorithm 3private static void PrintBrailleString3(){    foreach (var entry in BrailleDictionary)    {        var output = new string(Enumerable.Range(1, BrailleStringSize)                                    .Select(b => Array.IndexOf(entry.Value, b) > -1 ?                                                    '1' : '0').ToArray());        Console.WriteLine(${ entry.Key } => { output });    }}This is a one-liner approach using LINQ and it performs the SLOWEST.Testprivate static void Main(string[] args){    var stopWatch = new Stopwatch();    stopWatch.Start();    for (int i = 0; i < 100000; i++)    {        // Test with commented Console.WriteLine        PrintBrailleString1();        //PrintBrailleString2();        //PrintBrailleString3();    }    stopWatch.Stop();    Console.WriteLine($Time taken: { stopWatch.ElapsedMilliseconds } ms);}QuestionsWhy algorithm 2 is so slower compared to algorithm 1?Should I sacrifice performance for readability by using algorithm 3?Add-onsAre there any cleaner ways to perform similar operations?
Generate Braille characters from a dictionary
c#;performance;iteration
improvablevar output = new StringBuilder();for (int i = 1; i <= BrailleStringSize; i++){  output.Append(Array.IndexOf(entry.Value, i) > -1 ? '1' : '0');}This is the only part that you actually can optimze. Eeach of the three algorithms does this and only the second one is a slightly different way. Not suprisingly LINQ kills it's performance of the last solution.Currently you loop over all six items for each letter (i <= BrailleStringSize), even if there are only two or three bumps. This is not necessary.What you could do, is to create a string with all zeros new string('0', 6) an then insert 1 at the appropriate indexes letter[bump - 1] = '1'; looping over the bumps foreach (var bump in entry.Value) without searching anything. As it tunrns out, this need only 1/3 of the time of the first algorithm which was the fastetst.var zeros = new string('0', 6);foreach (var entry in BrailleDictionary){    var letter = new StringBuilder(zeros);    foreach (var bump in entry.Value)    {        letter[bump - 1] = '1';    }           //Console.WriteLine(${ entry.Key } => { letter });}During your tests you should disable the console output. It might signifficantly affect the measurementes.measurements (for fun)If you are curious how I measured them, this is the helper I used:private static void Measure(Action action, int iterations, string name){    var sw = Stopwatch.StartNew();    for (int i = 0; i < iterations; i++) action();    sw.Elapsed.Dump(name);}and all four calls:var count = 1_000_000;Measure(() => PrintBrailleString1(), count, OP1);Measure(() => PrintBrailleString2(), count, OP2);Measure(() => PrintBrailleString3(), count, OP3);Measure(() => PrintBrailleString4(), count, t3chb0t);Measure(() => PrintBrailleString5(), count, Paparazzi);According to my tests the results are as follows:OP100:00:04.4152571 OP200:00:05.0145808 OP300:00:11.6835103 t3chb0t00:00:01.8342203 Paparazzi00:00:03.6927934 performance vs convenienceShould I sacrifice performance by using algorithm 3?It depends. Usually it doesn't matter and you can pick the most convinient solution (most of the time it's LINQ - although here it doesn't have any advantages).Your measurements show that all three algorithms run quite good but the longer they run the larnger the differences are. If you are going to translate a few books into Braille it might be important to optimize the speed and chose the fastest solution but if you only play with single words or sentences the user will never notice any difference so you don't have to go crazy about the peroformance. Optimize only if it's noticable to the user not because an artifical test shows signifficant differences for an insane number of items.