LaTeX2e
我理解字符串处理可以通过使用xstring
和expl3
使用来完成xparse
(最初基于2013 年主题“标记和解析”) 我将在一个文档中运行数百次相对简单的字符串解析宏,可能涉及许多编译和许多文档,并且想知道哪种方法在时间上和/或计算上更有效率,不考虑加载包的固定时间。
3e 团队非常欢迎基于对xstring
代码的简要了解而得出的近似值。我不需要任何接近精确的结果 - 这只是为了满足我的好奇心,只是为了确保我不会最终试图在某处划破砖墙。
另外,我对这个应用程序是否可以对 2e 和 3e 算法进行有用的比较感兴趣。
这是我正在使用的宏xstring
:它用于对每个提及的关键词和短语的首字母(此演示中为前两个字母)进行非常轻微的突出显示,如果完全突出显示,只会使页面混乱:
\documentclass{article}
\usepackage{xstring}
%%% capitalize first n letters of each word %%%
% apply operator#3 to string#1 with word separators #2
\newcommand{\Splitstrop}[3][ ]{%
\providecommand\csA{}%
\providecommand\csB{}%
\StrCut{#3}{#1}\csA\csB%
#2{\csA}%
\ifx\csB\empty\else{#1}\Splitstrop[#1]{#2}{\csB}\fi\relax}
% apply operation#3 on first #2 letters of string #1
\newcommand{\Leftstrop}[3][1]{#2{\StrLeft{#3}{#1}}\StrGobbleLeft{#3}{#1}\relax}
\newcommand{\Kw}[1]{\textsc{#1}} %first mention of keyword/phrase
\newcommand{\Kwd}[1]{\Leftstrop[2]{\Kw}{#1}} %single-word 2nd mention: apply \Kw to first two letters of the word (I plan to sc just the first letter, but this is more illustrative)
\newcommand{\Kwds}[1]{\Splitstrop[ ]{\Kwd}{#1}} % keyphrase 2nd mention: apply Kwd to each word (space is word separator)
\begin{document}
Example: We define the \Kw{embiggen} \Kwd{operator} to raise the size of the \Kwd{argument} by one.
This is a reminder not to \Kwds{beg the question} in your definitions (Sec. 2-1): don't say ``We define the \Kwd{embiggen} \Kwd{operator} as the \Kwd{operator} that \Kwd{embiggens}.''
\end{document}
答案1
我尝试了你的例子l3benchmark
:
\documentclass{article}
\usepackage{xstring,l3benchmark}
%%% capitalize first n letters of each word %%%
% apply operator#3 to string#1 with word separators #2
\newcommand{\Splitstrop}[3][ ]{%
\StrCut{#3}{#1}\csA\csB
#2{\csA}%
\ifx\csB\empty\else{#1}\Splitstrop[#1]{#2}{\csB}\fi\relax
}
% apply operation#3 on first #2 letters of string #1
\newcommand{\Leftstrop}[3][1]{#2{\StrLeft{#3}{#1}}\StrGobbleLeft{#3}{#1}\relax}
\newcommand{\Kw}[1]{\textsc{#1}} %first mention of keyword/phrase
\newcommand{\Kwd}[1]{\Leftstrop[2]{\Kw}{#1}} %single-word 2nd mention: apply \Kw to first two letters of the word (I plan to sc just the first letter, but this is more illustrative)
\newcommand{\Kwds}[1]{\Splitstrop[ ]{\Kwd}{#1}} % keyphrase 2nd mention: apply Kwd to each word (space is word separator)
\ExplSyntaxOn
\cs_set_eq:NN \benchmark \benchmark:n
\ExplSyntaxOff
\begin{document}
\benchmark{
Example: We define the \Kw{embiggen} \Kwd{operator} to raise the size of the
\Kwd{argument} by one.
This is a reminder not to \Kwds{beg the question} in your definitions (Sec. 2-1):
don't say ``We define the \Kwds{embiggen operator} as the \Kwd{operator}
that \Kwd{embiggens}.''
}
\end{document}
和expl3
版本
\documentclass{article}
\usepackage{xparse,l3benchmark}
\ExplSyntaxOn
\NewDocumentCommand{\Kw}{m}
{
\kompootor_textsc:n { #1 }
}
\NewDocumentCommand{\Kwd}{m}
{
\kompootor_split:Nnn \kompootor_textsc:n { 2 } { #1 }
}
\NewDocumentCommand{\Kwds}{m}
{
\kompootor_splitstrop:nNn { ~ } \kompootor_textsc:n { #1 }
}
\seq_new:N \l__kompootor_splitstrop_in_seq
\seq_new:N \l__kompootor_splitstrop_out_seq
\cs_new_protected:Nn \kompootor_splitstrop:nNn
{
\seq_set_split:Nnn \l__kompootor_splitstrop_in_seq { #1 } { #3 }
\seq_set_map:NNn \l__kompootor_splitstrop_out_seq \l__kompootor_splitstrop_in_seq
{ \kompootor_leftstrop:Nn #2 { ##1 } }
\seq_use:Nn \l__kompootor_splitstrop_out_seq { #1 }
}
\cs_new_protected:Nn \kompootor_leftstrop:Nn
{
\kompootor_split:Nnn #1 { 2 } { #2 }
}
\cs_new:Nn \kompootor_split:Nnn
{
#1 { \tl_range:nnn { #3 } { 1 } { #2 } }
\tl_range:nnn { #3 } { #2+1 } { -1 }
}
\cs_new_protected:Nn \kompootor_textsc:n { \textsc { #1 } }
\NewDocumentCommand{\benchmark}{+m}{\benchmark:n{#1}}
\ExplSyntaxOff
\begin{document}
\benchmark{
Example: We define the \Kw{embiggen} \Kwd{operator} to raise the size of the
\Kwd{argument} by one.
This is a reminder not to \Kwds{beg the question} in your definitions (Sec. 2-1):
don't say ``We define the \Kwds{embiggen operator} as the \Kwd{operator}
that \Kwd{embiggens}.''
}
\end{document}
结果是
0.00338 seconds (1.1e4 ops)
为xstring
实施和
0.00187 seconds (6.29e3 ops)
以供expl3
实施。
输出相同(删除\benchmark
):