正文
void
BM_Regex_Match
(
int
batchSize)
{
std
::
string
buffer =
"cnt:"
;
std
::
string
regStr =
"cnt.*"
;
boost::
regex
reg
(regStr)
;
std
::
ofstream
outFile
(
"BM_Regex_Match.txt"
,
std
::ios::trunc)
;
outFile.close();
for
(
int
i =
0
; i <
1000
; i++) {
std
::
ofstream
outFile
(
"BM_Regex_Match.txt"
,
std
::ios::app)
;
buffer +=
"a"
;
int
count =
0
;
uint64_t
durationTime =
0
;
for
(
int
i =
0
; i < batchSize; i++) {
count++;
uint64_t
startTime = GetCurrentTimeInMicroSeconds();
if
(!boost::regex_match(buffer, reg)) {
std
::
cout
<<
"error"
<<
std
::
endl
;
}
durationTime += GetCurrentTimeInMicroSeconds() - startTime;
}
outFile << i <<
'\t'
<<
"durationTime: "
<< durationTime <<
std
::
endl
;
outFile << i <<
'\t'
<<
"process: "
<< formatSize(buffer.size() * (
uint64_t
)count *
1000000
/ durationTime)
<<
std
::
endl
;
outFile.close();
}
}
int
main
(
int
argc,
char
** argv)
{
logtail::Logger::Instance().InitGlobalLoggers();
std
::
cout
<<
"BM_Regex_Match"
<<
std
::
endl
;
BM_Regex_Match(
10000
);
return
0
;
}
这时候我们就需要注意了,我们使用行首正则时,其实往往只需要匹配单行日志开头的一部分,例如这个日志就是cnt,我们并不需要整个
.*
部分,因为匹配这部分会消耗不必要的性能。特别是当日志非常长时,这种影响尤为明显。