%20%E3%81%A8%20xparse%20(l3)%20%E3%81%AE%E5%8A%B9%E7%8E%87.png)
文字列処理は、(LaTeX2e
もともとxstring
はexpl3
xparse
2013 スレッド「トークン化と解析」) 比較的単純な文字列解析マクロを、おそらく多数のコンパイルと多数のドキュメントにわたって、ドキュメント内で何百回も実行する予定ですが、パッケージをロードするための固定時間を差し引いて、どの方法が時間的および/または計算的に効率的であるかを知りたいです。
のコードを簡単に調べた結果に基づく近似値がxstring
あれば、3e チームから非常に歓迎されます。正確な結果に近いものは必要ありません。これは私自身の好奇心のためであり、どこかのレンガの壁をひっかくようなことがないようにするためです。
また、このアプリケーションが 2e アルゴリズムと 3e アルゴリズムの比較に役立つかどうかについての意見も聞きたいです。
これは私が使用しているマクロですxstring
。これは、キーワードやフレーズのすべての言及の最初の文字 (このデモでは最初の 2 文字) を非常に薄く強調表示するものです。完全に強調表示すると、ページが乱雑になってしまいます。
\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
):