What is the most efficient way to execute this code?

My teacher asked us to make the program the most efficient way and use the switching case for this.

The program asks the user for data entry, and depending on what the user enters, the program will have to follow a set of instructions.

If the input is "A" or "a", the array must be sorted from A to Z.

If the input signal is "Z" or "z", the array must be sorted from Z to A.

If the input is "R" or "r", the array must be canceled.

An array is a string [].

So, I am wondering if it’s more efficient to use

switch (choice.ToLower()) { case "a": Array.Sort(array); break; case "z": Array.Sort(array); Array.Reverse(array); break; case "r": Array.Reverse(array); break; } 

or

  if (choice.ToLower() == "a" || choice.ToLower() == "z") { Array.Sort(array); } if (choice.ToLower() == "r" || choice.ToLower() == "z") { Array.Reverse(array); } 

and also if this code can be optimized even further.

So, the most efficient way to use the case of a switch or if structure, as shown above, and explain why?

I'm just curious because I always try to fully optimize my entire code.

+5
source share
3 answers

Well, you can check it out yourself:

 class Program { static void MyMethod1(int[] array, string choice) { switch (choice.ToLower()) { case "a": Array.Sort(array); break; case "z": Array.Sort(array); Array.Reverse(array); break; case "r": Array.Reverse(array); break; } } static void MyMethod2(int[] array, string choice) { if (choice.ToLower() == "a" || choice.ToLower() == "z") { Array.Sort(array); } if (choice.ToLower() == "r" || choice.ToLower() == "z") { Array.Reverse(array); } } static int[][] CreateRandomArrays(int num, int length) { Random rand = new Random(); int[][] arrays = new int[num][]; for (int i = 0; i < arrays.Length; i++) { arrays[i] = new int[length]; for (int i2 = 0; i2 < length; i2++) arrays[i][i2] = rand.Next(); } return arrays; } static void Main(string[] args) { int[][] test1 = CreateRandomArrays(50, 200000); int[][] test2 = CreateRandomArrays(50, 200000); Stopwatch s = new Stopwatch(); s.Start(); for (int i = 0; i < test1.Length; i++) MyMethod1(test1[i], "z"); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); s.Restart(); for (int i = 0; i < test2.Length; i++) MyMethod2(test2[i], "z"); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } } 

As you can see, the result is almost identical:

 1010 ms vs 1008 ms 
+6
source

ToUpper is faster + using Linq, the order of things will not be completed until joining the part ...

 using System; using System.Collections.Generic; using System.Linq; class Program { static string[] words = { "what", "is", "the", "most", "effecient", "way", "to", "execute", "this", "code" }; static void Main(string[] args) { IEnumerable<string> result; Console.Write("Choose words order (A to Z (A), Z to A (Z), Reversed (R)): "); switch (Console.ReadLine().ToUpper()) { case "A": result = words.OrderBy(w => w); break; case "Z": result = words.OrderByDescending(w => w); break; case "R": result = words.Reverse(); break; default: result = words.AsEnumerable(); break; } Console.WriteLine(string.Join(" ", result)); } } 
+1
source

In your case, if it’s slower, because every time he checks 2 conditions. Also check out the article on switch vs if speed. (in your if 4 example, conditions are always checked).
If the switch contains more than five elements, it is implemented using a lookup table or a hash list, which means that all elements receive the same access time compared to the if list, where the last element takes much more time to achieve since it must first evaluate each previous condition.

0
source

Source: https://habr.com/ru/post/1233426/


All Articles