1

Using .Net framework 4, I have following input string

String s = "9330e655-63d4-4aee-be79-505554256bd3"

I want to write a function that will return true or false as to whether or not any input string is a valid SqlGuid:

Method 1:

' Parse to validate ...
ok = true
try
   s_uid = SqlGuid.Parse( s )
catch e as Exception
   ok = false
end try
return ok

However, the ASP.Net framework also provides the Guid.TryParse() method, ie:

Method 2:

' Parse to validate ...
ok = Guid.TryParse( s )

My question: Which is more efficient and what do most use to validate SQL GUIDs (Method 1 or 2)?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
bdcoder
  • 3,280
  • 8
  • 35
  • 55

2 Answers2

1

If you're just checking validity then you can use either method, and Guid.TryParse will allow you to validate without doing expensive exception handling.

Be aware, however, that the string representations of Guid and SqlGuid are not the same, so a string generated from a Guid should not be used to create a SqlGuid since it will result in a different GUID value.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

best practice would be to use the build-in TryParse but if you want to look at the speed/performance itself here some quick benchmark.

don't forget to build as Release and don't run it from visual studio.

using System;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var regex = new Regex(@"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b", RegexOptions.IgnoreCase);
            string s = "9330e655-63d4-4aee-be79-505554256bd3";
            SqlGuid resultSql;
            Guid result;
            var sw = System.Diagnostics.Stopwatch.StartNew();
            bool valid;

            valid = true;
            for (int i = 0; i < 500000; ++i)
            {
                try
                {
                    resultSql = SqlGuid.Parse(s);
                    valid &= true;
                }
                catch
                { 
                    valid = false;
                }
            }

            sw.Stop();

            Console.WriteLine("sql try-catch {0}ms all valid {1}", sw.ElapsedMilliseconds, valid);

            sw = System.Diagnostics.Stopwatch.StartNew();

            valid = true;
            for (int i = 0; i < 500000; ++i)
            {
                try
                {
                    result = Guid.Parse(s);
                    valid &= true;
                }
                catch
                {
                    valid = false;
                }
            }

            sw.Stop();

            Console.WriteLine("guid try-catch {0}ms all valid {1}", sw.ElapsedMilliseconds, valid);

            sw = System.Diagnostics.Stopwatch.StartNew();

            valid = true;
            for (int i = 0; i < 500000; ++i)
            {
                valid &= Guid.TryParse(s, out result);
            }

            sw.Stop();

            Console.WriteLine("tryparse {0}ms all valid {1}", sw.ElapsedMilliseconds, valid);

            sw = System.Diagnostics.Stopwatch.StartNew();

            valid = true;
            for (int i = 0; i < 500000; ++i)
            {
                valid &= regex.IsMatch(s);
            }

            sw.Stop();

            Console.WriteLine("regex {0}ms all valid {1}", sw.ElapsedMilliseconds, valid);


            Console.ReadKey();
        }
    }
}
Fredou
  • 19,848
  • 10
  • 58
  • 113