Created
May 2, 2014 19:21
-
-
Save Fhernd/9cb414eec1dbe9ccd3b2 to your computer and use it in GitHub Desktop.
Demostración del rendimiento entre Regex(string) y Regex(string, RegexOptions) en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ===++=== | |
// | |
// OrtizOL | |
// | |
// ===--=== | |
/*============================================================ | |
// | |
// Clase: PruebaRendimientoRegex.cs | |
// | |
// Propósito: Demostración rendimiento versiones | |
// constructores de la clase Regex: Regex(string) vs | |
// Regex(string, RegexOptions) | |
// | |
============================================================*/ | |
using System; | |
using System.Diagnostics; | |
using System.Text.RegularExpressions; | |
namespace Recetas.Ch02 | |
{ | |
internal class PruebaRendimientoRegex | |
{ | |
const int MAX_ITERACIONES = 100000000; | |
public static void Main() | |
{ | |
string patron = @"^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$"; | |
// Inicio de cronómetro | |
var cronometro1 = Stopwatch.StartNew(); | |
// Instanciación de objeto con Regex(string) | |
Regex regex1 = new Regex(patron); | |
// Itera 1000000 veces | |
for (int i = 1; i < MAX_ITERACIONES; ++i) | |
{ | |
bool valido = regex1.IsMatch("fernd.ortiz@gmail.com"); | |
} | |
// Detiene el cronómetro uno | |
cronometro1.Stop(); | |
// Inicio de cronómetro 2 | |
var cronometro2 = Stopwatch.StartNew(); | |
// Instanciación de objeto con Regex(string, RegexOptions) | |
Regex regex2 = new Regex(patron, RegexOptions.Compiled); | |
// Itera 1000000 veces | |
for (int i = 1; i < MAX_ITERACIONES; ++i) | |
{ | |
bool valido = regex2.IsMatch("fernd.ortiz@gmail.com"); | |
} | |
// Detiene el cronómetro | |
cronometro2.Stop(); | |
// Presentación de resultados: | |
Console.WriteLine("Tiempor requerido por Regex(string): {0}", ((double)(cronometro1.Elapsed.TotalMilliseconds * 1000 * 1000) / MAX_ITERACIONES).ToString("0.00 ns")); | |
Console.WriteLine("Tiempor requerido por Regex(string, RegexOptions): {0}", ((double)(cronometro2.Elapsed.TotalMilliseconds * 1000 * 1000) / MAX_ITERACIONES).ToString("0.00 ns")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment