fork download
  1. using System;
  2. using static System.Console;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Diagnostics;
  7.  
  8. public class Program {
  9. public static void Main() {
  10. const int times = 1000;
  11. string texto;
  12. var lista = "RUASANTA HELENA, 769 AP 51 BL H JD ALVORADA~ 17513322 ";
  13. var sw = new Stopwatch();
  14. WriteLine($"RegEx: {(Regex.Replace(lista, @"(\d+) ", "$1~ "))}");
  15. sw.Start();
  16. for (int i = 0; i < times; i++) texto = Regex.Replace(lista, @"(\d+) ", "$1~ ");
  17. sw.Stop();
  18. var timer1 = sw.ElapsedTicks;
  19. WriteLine($"SB: {(MudaEndereco(lista))}");
  20. sw.Restart();
  21. for (int i = 0; i < times; i++) texto = MudaEndereco(lista);
  22. sw.Stop();
  23. var timer2 = sw.ElapsedTicks;
  24. WriteLine($"SB2: {(MudaEndereco2(lista, (c) => char.IsDigit(c), (c) => char.IsWhiteSpace(c)))}");
  25. sw.Restart();
  26. for (int i = 0; i < times; i++) texto = MudaEndereco2(lista, (c) => char.IsDigit(c), (c) => char.IsWhiteSpace(c));
  27. sw.Stop();
  28. var timer3 = sw.ElapsedTicks;
  29. var factor = Min(timer1, timer2, timer3);
  30. WriteLine($"RegEx: {(double)timer1 / factor, 6:0.000} ({TimeSpan.FromTicks(timer1).ToString((@"ss\:fff"))}) {timer1 / times, 3} Ticks cada");
  31. WriteLine($"SB: {(double)timer2 / factor, 6:0.000} ({TimeSpan.FromTicks(timer2).ToString((@"ss\:fff"))}) {timer2 / times, 3} Ticks cada");
  32. WriteLine($"SB2: {(double)timer3 / factor, 6:0.000} ({TimeSpan.FromTicks(timer3).ToString((@"ss\:fff"))}) {timer3 / times, 3} Ticks cada");
  33. }
  34. public static string MudaEndereco(string texto, char adicao = '~') {
  35. var resultado = new StringBuilder(texto.Length * 2);
  36. var anterior = '\0';
  37. foreach (var caractere in texto) {
  38. if (Char.IsDigit(anterior) && Char.IsWhiteSpace(caractere)) resultado.Append(adicao);
  39. resultado.Append(caractere);
  40. anterior = caractere;
  41. }
  42. return resultado.ToString();
  43. }
  44. public static string MudaEndereco2(string texto, Func<char, bool> sequencia, Func<char, bool> terminador, char adicao = '~') {
  45. var resultado = new StringBuilder(texto.Length * 2);
  46. var anterior = '\0';
  47. foreach (var caractere in texto) {
  48. if (sequencia(anterior) && terminador(caractere)) resultado.Append(adicao);
  49. resultado.Append(caractere);
  50. anterior = caractere;
  51. }
  52. return resultado.ToString();
  53. }
  54.  
  55. public static T Min<T>(params T[] numbers) => numbers.Min();
  56. }
  57.  
  58. //https://pt.stackoverflow.com/q/135183/101
Success #stdin #stdout 0.08s 24592KB
stdin
Standard input is empty
stdout
RegEx: RUASANTA HELENA, 769~  AP 51~ BL H JD ALVORADA~ 17513322~ 
SB:    RUASANTA HELENA, 769~  AP 51~ BL H JD ALVORADA~ 17513322~ 
SB2:   RUASANTA HELENA, 769~  AP 51~ BL H JD ALVORADA~ 17513322~ 
RegEx:  8.421 (00:006)  66 Ticks cada
SB:     1.000 (00:000)   7 Ticks cada
SB2:    1.483 (00:001)  11 Ticks cada