

















```
package main
import (
"fmt"
"regexp"
"strings"
"time"
)
func testRegexPerformance(repeatCount int) {
testString := strings.Repeat("a", repeatCount) + "#"
regex := regexp.MustCompile(`(\w*)*$`)
startTime := time.Now()
result := regex.MatchString(testString)
endTime := time.Now()
executionTime := endTime.Sub(startTime).Milliseconds()
fmt.Println("Repeat Count:", repeatCount)
fmt.Println("Execution Time:", executionTime, "milliseconds")
fmt.Println("-----------------------------------")
fmt.Println(result)
}
func main() {
// 测试从 1 到 50 的重复次数
for i := 1; i <= 50; i++ {
testRegexPerformance(i)
}
}
```
```
function testRegexPerformance(repeatCount) {
var testString = 'a'.repeat(repeatCount) + '#';
var regex = /(\w*)*$/;
var startTime = process.hrtime();
var result = regex.test(testString);
var endTime = process.hrtime(startTime);
var executionTime = endTime[0] * 1000 + endTime[1] / 1000000;
console.log("Repeat Count:", repeatCount);
console.log("Execution Time:", executionTime + " milliseconds");
console.log("-----------------------------------" + result);
}
// 测试从 1 到 50 的重复次数
for (var i = 1; i <= 50; i++) {
testRegexPerformance(i);
}
```
<?php
function testRegexPerformance($repeatCount) {
$testString = str_repeat('a', $repeatCount) . '#';
$regex = '/(\w*)*$/';
$startTime = microtime(true);
$result = preg_match($regex, $testString);
$endTime = microtime(true);
$executionTime = ($endTime - $startTime) * 1000;
echo "Repeat Count: $repeatCount\n";
echo "Execution Time: $executionTime milliseconds\n";
echo "-----------------------------------\n";
var_dump($result);
}
// 测试从 1 到 50 的重复次数
for ($i = 1; $i <= 50; $i++) {
testRegexPerformance($i);
}
```
```
import re
import time
def test_regex_performance(repeat_count):
test_string = 'a' * repeat_count + '#'
regex = r'(\w*)*$'
start_time = time.time()
result = re.match(regex, test_string)
end_time = time.time()
execution_time = (end_time - start_time) * 1000
print("Repeat Count:", repeat_count)
print("Execution Time:", execution_time, "milliseconds")
print("-----------------------------------")
print(result)
# 测试从 1 到 50 的重复次数
for i in range(1, 51):
test_regex_performance(i)
```
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。