Marusyk / grok.net

.NET implementation of the grok 📝
MIT License
290 stars 55 forks source link

Use a faster regular expression engine #71

Closed Marusyk closed 1 year ago

Marusyk commented 1 year ago

The Regex class in the .NET uses the .NET regular expression engine. This engine is a good general-purpose engine, but it is not the fastest. If we need the best possible performance, you can use a faster regular expression engine, such as the PCRE library.

The PCRE library is a faster regular expression engine that is available for free.

We can use the PCRE library in C# by using the PCRE.NET: https://github.com/ltrzesniewski/pcre-net library. This library provides a .NET wrapper for the PCRE library.

Marusyk commented 1 year ago

Benchmarks

methods without "_Old" use the PCRE library image

Methods with "_Old" in the name (Average):

Mean Execution Time (Average): 3,741,186.0 ns (approximately 3.7 milliseconds) Allocated Memory (Average): 231,999.0 B (approximately 226.17 KB)

PCRE Methods without "_Old" in the name (Average):

Mean Execution Time (Average): 290,362.5 ns (approximately 0.2903625 milliseconds) Allocated Memory (Average): 40,890.43 B (approximately 39.871 KB)

Conclusion

As we can see, methods without "_Old" in their names have significantly lower mean execution times and allocated memory compared to methods with "_Old" in their names. This suggests that the methods without "_Old" may perform better in terms of execution time and memory allocation.

teo-tsirpanis commented 1 year ago

I wonder, had you tried using the Regex source generator?

Marusyk commented 1 year ago

@teo-tsirpanis Yes I do, but PCRE was faster. or I didn't implement it correctly, if you are interested, you can try to implement the version with the source generator