r/excel • 226 • Feb 13 '18

User Defined Function I've made the =NATO() function, converting any given string into Nato phonetic alphabet!

And here's the result

I was wondering if such a thing existed and I realized I needed it in my life. I'm sure there are thousands of ways to improve it, so feel free to correct me on it! (EDIT: a much better version is shown in the comments)

Option Explicit

Function NATO(command As String)
    Dim i#
    Dim word As String
    NATO = ""
    For i = 1 To Len(command)
        Select Case LCase(Mid(command, i, 1))
            Case " "
                word = ""
            Case "a"
                word = "Alfa"
            Case "b"
                word = "Bravo"
            Case "c"
                word = "Charlie"
            Case "d"
                word = "Delta"
            Case "e"
                word = "Echo"
            Case "f"
                word = "Foxtrot"
            Case "g"
                word = "Golf"
            Case "h"
                word = "Hotel"
            Case "i"
                word = "India"
            Case "j"
                word = "Juliett"
            Case "k"
                word = "Kilo"
            Case "l"
                word = "Lima"
            Case "m"
                word = "Mike"
            Case "n"
                word = "November"
            Case "o"
                word = "Oscar"
            Case "p"
                word = "Papa"
            Case "q"
                word = "Quebec"
            Case "r"
                word = "Romeo"
            Case "s"
                word = "Sierra"
            Case "t"
                word = "Tango"
            Case "u"
                word = "Uniform"
            Case "v"
                word = "Victor"
            Case "w"
                word = "Whiskey"
            Case "x"
                word = "X-ray"
            Case "y"
                word = "Yankee"
            Case "z"
                word = "Zulu"
        End Select
        NATO = NATO & word & " "
    Next i
End Function
131 Upvotes

61 comments sorted by

View all comments

Show parent comment

1

u/JPDVP 48 Feb 13 '18

Do you also have a 64-bit OS and 64-bit Office version? If not it could be question of the processor having more space for instructions/cache hence improving the Select Case in some way.

The array is accessing bits of memory so probably the room for improvement depending on the processor is not that high I guess..

We're delving too much inside Computer Science and it begins to be wizardry to me xD

btw just ran a series of 10 tests (back to back) and got 25 avg time for array and 30 avg time for select case

2

u/sooka 43 Feb 14 '18

Ok so here are the tests on the other machine with O365 (i7 6820HQ):
• Select Case: 39,2958931233377;
• For: 25,3644887020114.

the ratio is the same.

2

u/sooka 43 Feb 14 '18 edited Feb 14 '18

And...the tests in C#, I wasn't expecting that:

1.000.000 iterations of NATO function for string: "NATONATONATONATONATONATONATONATONATONATO"
--------------------------------------------------------------------------------------------
Not parallel:
  - NATO switch: 00:00:01.5619716
  - NATO for: 00:00:02.9222582

Parallel:
  - NATO switch: 00:00:00.5808688
  - NATO for: 00:00:01.5056278  


10.000.000 iterations of NATO function for string: "NATONATONATONATONATONATONATONATONATONATO"
--------------------------------------------------------------------------------------------
Not parallel:
  - NATO switch: 00:00:15.5502134
  - NATO for: 00:00:28.6920608

Parallel:
  - NATO switch: 00:00:05.4363801
  - NATO for: 00:00:14.6768145  

my code for anyone that would like too see it (it should be possible to optimize it a bit more, I'm a beginner with C#):

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;

namespace Switch_vs_For
{
	class Program
	{
		static string[] NatoAlpha = "Alpha,Bravo,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,Indigo,Juliett,Kilo,Lima,Mike,November,Oscar,Papa,Quebec,Romeo,Sierra,Tando,Uniform,Victor,Whiskey,Xray,Yankee,Zulu".Split(',');
		static void Main(string[] args)
		{
			Console.WriteLine("1.000.000 iterations of NATO function for string: \"NATONATONATONATONATONATONATONATONATONATO\"");
			Console.WriteLine("--------------------------------------------------------------------------------------------");
			Console.WriteLine("Not parallel:");
			SwitchForNotParallel();
			Console.WriteLine();
			Console.WriteLine("Parallel:");
			SwitchForParallel();
			Console.ReadKey();
		}

		private static void SwitchForParallel()
		{
			Stopwatch stopwatch = new Stopwatch();

			stopwatch.Start();
			Parallel.For(0, 1000000, i =>  
				{
				NATO_Switch("NATONATONATONATONATONATONATONATONATONATO");
				});
			Console.WriteLine($"  - NATO switch: {stopwatch.Elapsed}");

			stopwatch.Restart();
			Parallel.For(0, 1000000, i =>
			{
				NATO_For("NATONATONATONATONATONATONATONATONATONATO");
			});
			Console.WriteLine($"  - NATO for: {stopwatch.Elapsed}");
		}
		private static void SwitchForNotParallel()
		{
			Stopwatch stopwatch = new Stopwatch();

			stopwatch.Start();
			for (int i = 0; i < 1000000; i++)
			{
				NATO_Switch("NATONATONATONATONATONATONATONATONATONATO");
			}
			Console.WriteLine($"  - NATO switch: {stopwatch.Elapsed}");

			stopwatch.Restart();
			for (int i = 0; i < 1000000; i++)
			{
				NATO_For("NATONATONATONATONATONATONATONATONATONATO");
			}
			Console.WriteLine($"  - NATO for: {stopwatch.Elapsed}");
		}

		private static string NATO_Switch(string command)
		{
			string word = null;
			string NATO = null;
			for (int i = 0; i < command.Length; i++)
			{
				switch (command[i])
				{
					case ' ':
						word = "";
						break;
					case 'a':
						word = "Alfa";
						break;
					case 'b':
						word = "Bravo";
						break;
					case 'c':
						word = "Charlie";
						break;
					case 'd':
						word = "Delta";
						break;
					case 'e':
						word = "Echo";
						break;
					case 'f':
						word = "Foxtrot";
						break;
					case 'g':
						word = "Golf";
						break;
					case 'h':
						word = "Hotel";
						break;
					case 'i':
						word = "India";
						break;
					case 'j':
						word = "Juliett";
						break;
					case 'k':
						word = "Kilo";
						break;
					case 'l':
						word = "Lima";
						break;
					case 'm':
						word = "Mike";
						break;
					case 'n':
						word = "November";
						break;
					case 'o':
						word = "Oscar";
						break;
					case 'p':
						word = "Papa";
						break;
					case 'q':
						word = "Quebec";
						break;
					case 'r':
						word = "Romeo";
						break;
					case 's':
						word = "Sierra";
						break;
					case 't':
						word = "Tango";
						break;
					case 'u':
						word = "Uniform";
						break;
					case 'v':
						word = "Victor";
						break;
					case 'w':
						word = "Whiskey";
						break;
					case 'x':
						word = "X-ray";
						break;
					case 'y':
						word = "Yankee";
						break;
					case 'z':
						word = "Zulu";
						break;
				}
				NATO += word + " ";
			}
			return NATO;
		}
		
		private static string NATO_For(string command)
		{
			string NATO = null;
			for (int i = 0; i < command.Length; i++)
			{
				int num = Strings.Asc(command[i]) - 65;
				//int num = Convert.ToInt16(command[i]) - 65;
				if (num >= 0 && num <= 25)
				{
					NATO += NatoAlpha[num] + " ";
				}
			}
			return NATO.Trim();
		}

	}
}

1

u/sooka 43 Feb 13 '18

Yes everything 64bit (W10 + Office).
At work I've W10+O365 also 64bit, different processor but if what we got was right we should get a different ratio there :D...I hope for it...if that's not the case I'll ask the mod to call the Excel Team for an explanation! /s

Thanks for the tests :), if you think about it 5 seconds over 1 million iterations is like nothing but at the same time is gigaenormous how many you can do in that saved 5 seconds.