19

I have done a benchmark on the same machine for the same input file. I got surprised, the time needed by xelatex is almost 2.9 times as the time needed by latex-dvips-ps2pdf. For the sake of simplicity, I duplicated the input file: xlx.tex for xelatex and ltx.tex for latex-dvips-ps2pdf. See the following result.

Results:

enter image description here

Legends:

  • xlx.tex : input file for xelatex.
  • xlx.pdf : pdf output without gs compression.
  • xlx-tiny.pdf : pdf output with gs compression.
  • ltx.tex : input file for latex.
  • ltx.pdf : pdf output without gs compression.
  • ltx-tiny.pdf : pdf output with gs compression.

enter image description here

Even if ps2pdf already compressed its output, it is still worth compressing the output again using gs. However, the compression ratio is too small.

C# code for benchmarking:

It is not a clean code that conforms to best practice. I will tidy it up.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace LaTeXAutomator { class Program { class Record { public string Activity { get; set; } public double Elapsed { get; set; } public override string ToString() { return string.Format("{0}: {1} seconds", Activity, Elapsed); } } class Step { public string FileName { get; set; } public string Arguments { get; set; } public bool UseShellExecute { get; set; } public void Start() { Process p = new Process(); p.StartInfo.FileName = FileName; p.StartInfo.Arguments = Arguments; p.StartInfo.UseShellExecute = UseShellExecute;

            p.EnableRaisingEvents = true;
            p.Exited += (sender, e) => { Console.WriteLine("=== {0} FINISHED ===", FileName.ToUpper()); };

            p.Start();
            p.WaitForExit();
            //p.Close();
        }
    }

    static void Main(string[] args)
    {
        var records = new List<Record>();


        var filename = "xlx"; // for xelatex
        var xelatexSteps = new List<Step>()
        {
            new Step { FileName = "xelatex", Arguments = String.Format("-interaction=nonstopmode {0}.tex", filename), UseShellExecute = false },
            new Step { FileName = "xelatex", Arguments = String.Format("-interaction=nonstopmode {0}.tex", filename), UseShellExecute = false },
            new Step { FileName = "gswin64c", Arguments =  String.Format("-sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile={0}-tiny.pdf {0}.pdf", filename), UseShellExecute = false }//,
            //new Step { FileName = "AcroRd32", Arguments =  String.Format("{0}-tiny.pdf", filename), UseShellExecute = true }
        };
        DoBenchMark(xelatexSteps, records);
        CleanUp(filename);


        filename = "ltx"; // for latex
        var latexSteps = new List<Step>()
        {
            new Step { FileName = "latex", Arguments = String.Format("-interaction=nonstopmode {0}.tex", filename), UseShellExecute = false },
            new Step { FileName = "latex", Arguments = String.Format("-interaction=nonstopmode {0}.tex", filename), UseShellExecute = false },
            new Step { FileName = "dvips", Arguments =  String.Format("{0}.dvi", filename), UseShellExecute = false },
            new Step { FileName = "ps2pdf", Arguments =  String.Format("{0}.ps", filename), UseShellExecute = false },
            new Step { FileName = "gswin64c", Arguments =  String.Format("-sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile={0}-tiny.pdf {0}.pdf", filename), UseShellExecute = false }//,
            //new Step { FileName = "AcroRd32", Arguments =  String.Format("{0}-tiny.pdf", filename), UseShellExecute = true }
        };
        DoBenchMark(latexSteps, records);
        CleanUp(filename);
        Console.Clear();

        foreach (var x in records)
        {
            Console.WriteLine(x);
        }

        var totalXeTeX = records.Take(xelatexSteps.Count).Sum(x => x.Elapsed);
        var totalLaTeX = records.Skip(xelatexSteps.Count).Sum(x => x.Elapsed);

        Console.WriteLine("==========================");
        Console.WriteLine("Total time taken by XeLaTeX: {0} seconds.", totalXeTeX);
        Console.WriteLine("Total time taken by LaTeX: {0} seconds.", totalLaTeX);

        var ratio = totalXeTeX / totalLaTeX;

        Console.WriteLine("XeLaTeX runs {0} times slower than LaTeX.", ratio);


    }

    private static void DoBenchMark(List<Step> steps, List<Record> records)
    {
        Stopwatch sw = new Stopwatch();
        foreach (var step in steps)
        {
            sw.Reset();
            sw.Start();
            step.Start();
            sw.Stop();
            records.Add(new Record { Activity = step.FileName, Elapsed = sw.Elapsed.TotalSeconds });
        }
    }

    private static void CleanUp(string filename)
    {
        foreach (var filepath in Directory.GetFiles(".", String.Format("{0}.*", filename), SearchOption.TopDirectoryOnly))
        {
            if (!(Path.GetExtension(filepath) == ".tex" || Path.GetExtension(filepath) == ".pdf"))
                File.Delete(filepath);
        }

    }
}

}

Question:

Why does xelatex execute much much slower than latex-dvips-ps2pdf?

Note: The speed is very crucial because my online TeX to PDF generator should execute as fast as possible.

Comment from TH. and my response:

I don't think this is a very good question. What sort of answer would you be looking for? "It's slower because it is." "It's slower because it implements foo differently than pdfTeX." "It's slower because it uses feature bar." Are any of those actually useful to you?

All your assumptions above are very useful for me.

Display Name
  • 46,933
  • do you have more details? I never notice anything that obvious. – pluton Jun 10 '11 at 09:41
  • @pluton: see my update please. – Display Name Jun 10 '11 at 11:00
  • 1
    @xport: It's better to use PowerShell - Timing a command's execution in PowerShell. And defragment the hard drive. And even better - load the entire LaTeX distribution in RAM Drive. – Karl Karlsson Jun 10 '11 at 11:05
  • 2
    @Karl: I don't believe it can boost the xelatex performance significantly. – Display Name Jun 10 '11 at 11:08
  • 3
    @xport, Could you please provide the contents of the TeX file please? Perhaps they are important too. – pmav99 Jun 10 '11 at 11:12
  • @xport: Actually I use pdflatex, and the boost is significant. Can't speak for xelatex however. Also, you can remove all unnecessary packages and ALL documentation. Than recreating the file name database, or whatever it's called. A boost of 20 s per run is quite possible. – Karl Karlsson Jun 10 '11 at 11:21
  • @pmav99: Please wait. I am preparing a dummy input file. The current input file that I used is very confidential. :-) – Display Name Jun 10 '11 at 12:37
  • I'm pretty sure that the slower speed of XeTeX is mainly due capability of unicode input. – morbusg Jun 15 '11 at 06:31
  • @xport: Some things that I see: 1) For example, pdflatex produces PDF files that usually can't be further compressed using Ghostscript. 2) xelatex produces file, that is 16 times bigger than the one made with LaTeX. 3) xelatex is usually used in cases, where LaTeX simply doesn't work. This makes the comparison somewhat out of the real world. – Karl Karlsson Jun 15 '11 at 09:00
  • 2
    I don't think this is a very good question. What sort of answer would you be looking for? "It's slower because it is." "It's slower because it implements foo differently than pdfTeX." "It's slower because it uses feature bar." Are any of those actually useful to you? – TH. Jun 15 '11 at 09:13
  • If there is a difference of how fragmented the compared files are the time it takes to read can be due to fragmentation. Have you done the comparison on run that is not the first run? Because if the executables and other read files are read from RAM rather than the HDD their compilation time can change. – N.N. Jun 15 '11 at 12:38
  • @N.N.: I used the same machine and the same input file. My machine has 4 cores intel cpu and 4 GB RAM DDR3. I think it is more related to how the algorithm has been implemented internally. – Display Name Jun 15 '11 at 12:47
  • 1
    Irrespective of cpu there can be a difference of read speed because something is read from the HDD rather than the RAM or the other way around. – N.N. Jun 15 '11 at 12:51
  • 1
    How about comparing latex with or without \usepackage[utf8]{inputenc}; it seems to me that it does delay the computation. – Mateus Araújo Jun 21 '11 at 01:47

1 Answers1

13

To know why it's slower one first must determine where it's slower. I make two assumptions:

  1. XeTeX is that much slower not always, but only in particular cases.
  2. XeTeX is that much slower, because it makes that big file.

In 2 I mean, there is some effort in making that file, and this effort is what goes wrong. Not the I/O associated with the file creation. Normally latex-dvips-gs and pdflatex produce files that aren't compressible by ghostscript.

It's clear, that a PDF made by 1 kB TeX file can't have a 16 MB size because of textual data. Than, re-reading I saw that there is a folder called Images. So, my guess is:

It's something to do with the images. Try turning them off 1) and lets see what happens.

1) This means commenting out, replacing with dummy images of lowest possible complexity, etc.

  • 6
    My gut feeling is also an image issue. Possibly there is an image that can be copied verbatim by dvips, but not by dvipdfmx. But regardless, i think this is an ill-thought out question, as it requires an oracle or other paranormal means to answer correctly. There is neither input nor output files so there is no means of testing anything. If this happened with me in a commercial setting, i would have closed the issue immediately with 'Unable to reproduce' as reason. – Taco Hoekwater Jun 21 '11 at 16:30
  • @Taco: You can make any dummy input files and let me know if xelatex can do faster. I will prepare the dummy input files later. I am still struggling against other issues now. – Display Name Jun 21 '11 at 18:29
  • 2
    @xport: For example, testmath.tex compiles as follows. LaTeX ~ 7.8 s, PDFLaTeX ~ 6.5 s, XeLaTeX ~ 8.7 s. That's an average after OS file caching starts to work i.e. after the 2nd or 3rd run. First run is quite slow, because of the hard drive. And yes, XeLaTeX is slower, but it's not times slower. – Karl Karlsson Jun 21 '11 at 20:18
  • that matches my experience too – Taco Hoekwater Jun 23 '11 at 19:47