赛莱特

赛莱特

TikZ 语法太过自由(如文档中所述),这让我有点困惑该采用哪种语法。最佳实践确实是我在编写任何代码时的主要考虑因素。

我不知道 TikZ 在编译过程中如何解析输入源代码的内部细节。请考虑以下两种情况。

  1. 使用单一\path命令来控制一系列几何对象。

    \path
    (0,0) circle (1) [draw]
    -- (1,1) node[above] {x}
    +(1,-1) rectangle ++(2,2)[draw];
    
  2. \path每个对象使用一个命令。

    \draw (0,0) circle (1);
    \draw (0,0) -- (1,1) node[above] {x};
    \draw (1,1) +(1,-1) rectangle ++(2,2);
    

第一种情况下的单一是否\path会使编译更有效率(与第二种情况相比)?

生成的PDF怎么样,文件大小有差别吗?

我当然同意下面的引言。

编写让代码读者更清楚的内容是最佳实践。

但是,我们暂时把它扔掉,因为可读性问题更加主观而不是性能影响。

答案1

当然,正如@Jake提到的,代码的可读性是最重要的。所以这应该始终执行!

不过,这个问题相当有趣。我可能有一个分析答案。我使用的pdflatex编译器版本:3.1415926-2.4-1.40.13

首先,我们必须确定要研究哪个最小例子。

\documentclass{article}
\usepackage{tikz}
\pdfcompresslevel=0 % allows direct comparison
\begin{document}
\thispagestyle{empty} % this is crucial to obtain the smallest file

\end{document}

此外,还有两段代码需要比较,我们将比较pdflatex可执行文件(即TikZshipout)在 PDF 中创建的对象流。我只展示了重要的对于绘图,感兴趣区域上方和下方的对象流操作是相同的。

  1. 代码

    \begin{tikzpicture}
      \draw (0,0) --(1,0) (2,0) -- (3,0);
    \end{tikzpicture}
    

    生成以下页面对象:

    0.0 0.0 m       % start point of new drawing (m for move)
    28.3468 0.0 l   % line ending (l) for line
    56.69362 0.0 m  % new move
    85.04042 0.0 l  % draw line
    S               % stop
    
  2. 代码

    \begin{tikzpicture}
      \draw (0,0) --(1,0);
      \draw (2,0) -- (3,0);
    \end{tikzpicture}
    

    生成以下页面对象:

    0.0 0.0 m       % start point of new drawing (m for move)
    28.3468 0.0 l   % line ending (l) for line
    S               % stop
    56.69362 0.0 m  % new move
    85.04042 0.0 l  % draw line
    S               % stop
    

因此,如上所示,两段代码实际上确实会产生额外的工作用于 PDF 查看器。

让我们考虑一个稍微有趣的例子:

  1. 代码

    \begin{tikzpicture}
      \draw[red] (0,0) --(1,0) (2,0) -- (3,0);
    \end{tikzpicture}
    

    生成以下对象:

    1 0 0 rg 1 0 0 RG  % choose color in RGB
    0.0 0.0 m          % move
    28.3468 0.0 l      % draw
    56.69362 0.0 m     % move
    85.04042 0.0 l     % draw
    S                  % stop
    0 g 0 G            % apply color
    
  2. 代码

    \begin{tikzpicture}
      \draw[red] (0,0) --(1,0);
      \draw[red] (2,0) -- (3,0);
    \end{tikzpicture}
    

    生成以下对象:

    1 0 0 rg 1 0 0 RG  % choose color
    0.0 0.0 m          % move
    28.3468 0.0 l      % draw
    S                  % stop
    0 g 0 G            % apply color
    Q                  % restore the previous saved graphics state
    q                  % save the current graphics state (i.e. Q q is a no-op)
    1 0 0 rg 1 0 0 RG  % choose color
    56.69362 0.0 m     % move
    85.04042 0.0 l     % draw
    S                  % stop
    0 g 0 G            % apply color
    

但是,正如您所注意到的,PDF 的压缩会将其减少到大致布局相同这很容易。 :)

可以这么说,无论你做什么,最终产品中的优化程序都是不休比这些简单的例子更重要。

结论

在优化之前,必须以某种方式创建对象流,这意味着传递给优化器的字节数更少应该(理论上)意味着更快的编译(前提是优化器线性依赖于接收的字节数!)。

但是,在非压缩文件中,很容易考虑非常复杂的绘图,其中包含大量无操作操作。此外,优化器越容易解释对象流,它就越优化,从而生成更小的文件。
我敢说:有人可能会说,做任何事都应该遵循一条路径

编辑

我想弄清楚使用多个路径段是否真的会导致速度损失。所以我做了以下事情。

  1. 上述红色的示例已用于测试。
  2. 在我的test.tex文档中我创建了 10,000 次
  3. 我平均跑了 100 次

以下是我得到的结果:

-----One draw Two draws
Mean 20.3028  26.6568  
Std.  1.2976   2.5581  

显然,它确实会影响执行时间,但这里我们分别使用了 10,000 和 20,000 个绘制命令。因此,文档必须与此数量级相当才能引人注目。标准差让我怀疑这是否是明显的比较。不过,我可能不会再做测试了。:)

答案2

在第一种情况下,我删除了多余的内容[draw]。以下是我 Aspire 4752ZG 上的日志,该机器配备 Intel Core 8960 处理器(2.2GHz,2MB L3 缓存),4 GB DDR3 内存,安装了 Ubuntu 12.10。我不知道这些长日志是否有用。

赛莱特

第一种情况

文件大小:1,679 字节

时间日志

第二种情况

文件大小:1,688 字节

PDFLaTeX

第一种情况

Output written on prob44d.pdf (1 page, 8656 bytes).
PDF statistics:
15 PDF objects out of 1000 (max. 8388607)
10 compressed objects within 1 object stream
0 named destinations out of 1000 (max. 500000)
13 words of extra memory for PDF output out of 10000 (max. 10000000)

第二种情况

Output written on prob44c.pdf (1 page, 8664 bytes).
PDF statistics:
15 PDF objects out of 1000 (max. 8388607)
10 compressed objects within 1 object stream
0 named destinations out of 1000 (max. 500000)
13 words of extra memory for PDF output out of 10000 (max. 10000000)

LuaLaTeX

第一种情况

Output written on prob44e.pdf (1 page, 8674 bytes).
PDF statistics: 15 PDF objects out of 1000 (max. 8388607)
10 compressed objects within 1 object stream
0 named destinations out of 1000 (max. 131072)
16 words of extra memory for PDF output out of 10000 (max. 10000000)

第二种情况

Output written on prob44f.pdf (1 page, 8674 bytes).
PDF statistics: 15 PDF objects out of 1000 (max. 8388607)
10 compressed objects within 1 object stream
0 named destinations out of 1000 (max. 131072)
16 words of extra memory for PDF output out of 10000 (max. 10000000)

关于编译时间

我只对xelatex编译进行了计时,因为只有它才有,而且pdflatex我看到了文件大小的明显差异(无论多小)。我每次都进行了 15 次试验,并在 R 中对结果运行了 Welch t 检验。(当我不得不做这pdflatex部分时,我偷懒了。好吧,你可以试试。)

以下是分析。

第一个设置的方差为 0.005339,而第二个设置的方差为 2.47 e -05。

T 检验

Welch Two Sample t - test
data : setupa and setupb
t = 0.5147 , df = 14.13 , p - value = 0.6147
alternative hypothesis : true difference in means is not equal to 0
95 percent confidence interval :
-0.03079 0.05026
sample estimates :
mean of x mean of y
0.6073
0.5975

在这里你会发现差异并不明显。随着计算机能力的不断提高,这种差异几乎察觉不到。(当然,除非你time在终端中运行。)我不得不同意杰克的观点,即在应用颜色和内容时,第二种设置是首选。

相关内容