Home >Backend Development >C++ >Is \d Really Less Efficient Than [0-9] or [0123456789] in Regex?

Is \d Really Less Efficient Than [0-9] or [0123456789] in Regex?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-31 18:36:10967browse

Is d Really Less Efficient Than [0-9] or [0123456789] in Regex?

D's efficiency in regular expression is not as efficient as [0-9]

In the recent discussion, some people questioned the efficiency of using instead of

or

in regular expressions. Unexpectedly, the test in the C#regular expression engine shows that the efficiency of [0123456789] is lower than the other two options. [0-9] d The possible cause of low efficiency d

Unicode number:

contains all unicode numbers, not just the common 0-9. Therefore, analytical non -standard numbers may slow down the speed of regular expression engines.
  1. Unnecessary extra function: Including additional functions other than d, it may need more complicated processing. These additional functions affect efficiency.
  2. Test results d [0-9] In order to prove this problem, the following tests were performed:

10,000 random string, each string contains 1,000 characters, half of which contain numbers. Each regular expression (

,

,

) Time to process the string spent:
  • regular expression Time The percentage of time relative to D
    > 00: 00: 00.2141226 100%
    > 00: 00: 00.1357972 63.42%
    > 00: 00: 00.1388997 64.87%
  • The results show that and
  • are significantly better than d in terms of efficiency. [0-9] [0123456789] Conclusion
正则表达式 时间 相对d的时间百分比
d 00:00:00.2141226 100%
[0-9] 00:00:00.1357972 63.42%
[0123456789] 00:00:00.1388997 64.87%
Although can provide a wider number of digital matching functions, it is sacrificed in terms of performance. For key -type applications, it is recommended to use more concise

or [0-9]. [0123456789]

The above is the detailed content of Is \d Really Less Efficient Than [0-9] or [0123456789] in Regex?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn