Perché la lunghezza dell'ingresso su regex non influisce sulle prestazioni e come è possibile?Perchè regex non si preoccupa della lunghezza della stringa
La stringa generata è la seguente: 128 caratteri casuali. quindi due numeri tra parentesi. e questo è ripetuto molte volte.
128 radnom characters....(-2435346|45436) 128 radnom characters....(-32525562|-325346)
Il regex ottiene tutti i numeri tra parentesi. ecco il modello.
\(([-+]?\d+\|[-+]?\d+)\)
Così le partite sarebbe come
-2435346|45436
-32525562|-325346
etc...
Ecco il codice che fa il punto di riferimento. Avvio del cronometro dopo che l'input è stato generato perché voglio valutare solo il tempo di abbinamento.
Random rand = new Random();
Func<string> getRandString = // generates 128 random characters.
() => Enumerable.Range(0, 128).Select(x => (char) rand.Next()).Aggregate("", (a, b) => a + b);
Func<int> getRandInteger =() => rand.Next(); // generates random number.
string format = "{0}({1}|{2})";
// Generate the big string.
StringBuilder bigstr = new StringBuilder();
for (int i = 0; i < 100; i++) // repeat 100 times.
{
bigstr.Append(string.Format(format, getRandString(), getRandInteger(), getRandInteger()));
}
string input = bigstr.ToString();
Stopwatch stopwatch = Stopwatch.StartNew();
var matches = Regex.Matches(input, @"\(([-+]?\d+\|[-+]?\d+)\)");
stopwatch.Stop();
Console.WriteLine("Time Elapsed :\t{0}\nInputLength :\t{1}\nMatches Count :\t{2}", stopwatch.Elapsed, input.Length, matches.Count);
Questa è l'uscita della mia console se ripeto ciclo 10
volte.
Time Elapsed : 00:00:00.0004132
InputLength : 1500
Matches Count : 10
Se ripeto il ciclo 1000
volte.
Time Elapsed : 00:00:00.0004373 // seriously?
InputLength : 149937
Matches Count : 1000
Se ripeto il ciclo 1000000
volte.
Time Elapsed : 00:00:00.0004900 // wtf?
InputLength : 149964452
Matches Count : 1000000
Schermata se non credono
E 'una specie di valutazione pigra? se è così allora come può mostrare il conteggio delle partite? come mai l'ho fatto con il debugger e ho potuto vedere le partite.
C'è qualcosa di particolare nel mio modello di regex che lo rende veloce? ma come la lunghezza della corda non influisce sulle prestazioni? Non capisco.
Non c'è niente di speciale qui. Il tuo motore regex attraverserà la stringa e salverà tutti gli stati che corrispondono alla tua regex, e tu sei il punto di riferimento su una stringa 1000 volte più grande che non è un grosso problema per ora adotti le macchine. stringhe molto più grandi.O forse il tuo benchmarikon non è giusto. – Kasramvd
Potresti essere interessato a [questa mia risposta] (http://stackoverflow.com/a/32618592/3764814) se desideri visualizzare alcuni dettagli sull'algoritmo di ricerca delle stringhe utilizzato dal motore regex .NET. –
Analisi comparativa corretta: https://andreyakinshin.gitbooks.io/performancebookdotnet/content/science/microbenchmarking.html –