|
data=r""" |
|
`pdf2ps` + `ps2pdf` with pipe (*) |
|
gs -sDEVICE=ps2write -dNOCACHE -sOutputFile=- -q -dBATCH -dNOPAUSE a.pdf -c quit | ps2pdf - c.pdf |
|
|
|
`pdf2ps` + `ps2pdf` with temporary file ([source](https://unix.stackexchange.com/a/634170/296692)) (*) |
|
gs -sDEVICE=ps2write -dNOCACHE -sOutputFile=c.ps -q -dBATCH -dNOPAUSE a.pdf |
|
ps2pdf c.ps c.pdf |
|
|
|
`pdfimage24` (1200/2) |
|
#Print time: 9.691 |
|
gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r1200 -dDownScaleFactor=2 -o c.pdf a.pdf |
|
|
|
`pdfimage24` (1200) |
|
gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r1200 -o c.pdf a.pdf |
|
|
|
`pdfimage24` (2400/2) |
|
#Print time: 15.822 |
|
gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage24 -r2400 -dDownScaleFactor=2 -o c.pdf a.pdf |
|
|
|
`pdfimage8` (2400/2) |
|
gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage8 -r2400 -dDownScaleFactor=2 -o c.pdf a.pdf |
|
|
|
`pdfimage8` (1200) |
|
#Print time: 9.539 |
|
gs -dNOPAUSE -dBATCH -sDEVICE=pdfimage8 -r1200 -o c.pdf a.pdf |
|
|
|
`convert` (600) |
|
#Print time: 9.884 |
|
convert -density 600 a.pdf c.pdf |
|
|
|
`convert` (600) + `gs` to optimize ([source](https://superuser.com/a/1588781/577463)) |
|
convert -density 600 a.pdf b.pdf |
|
gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=c.pdf b.pdf -q |
|
|
|
`pdftoppm` (1200) (`.png`) + `img2pdf` ([source](https://superuser.com/a/1490924/577463)) (†) |
|
pdftoppm -r 1200 -png a.pdf a |
|
img2pdf a-1.png -o c.pdf |
|
|
|
`pdftoppm` (1200) (`.jpg`) + `img2pdf` (†) |
|
pdftoppm -r 1200 -jpeg a.pdf a |
|
img2pdf a-1.jpg -o c.pdf |
|
|
|
`pdftoppm` (1200) (`.tiff`) + `img2pdf` (†) |
|
pdftoppm -r 1200 -tiff a.pdf a |
|
img2pdf a-1.tif -o c.pdf |
|
""" |
|
data_items=data.strip().split("\n\n") |
|
|
|
import re |
|
|
|
import functools |
|
|
|
from pathlib import Path |
|
import subprocess |
|
|
|
@functools.lru_cache |
|
def benchmark(commands: str): |
|
Path("/tmp/a.sh").write_text(commands) |
|
Path("/tmp/c.pdf").unlink(missing_ok=True) |
|
process=subprocess.run(["/bin/time", "bash", "a.sh"], cwd="/tmp/", stderr=subprocess.PIPE, stdout=subprocess.DEVNULL) |
|
assert Path("/tmp/c.pdf").is_file() |
|
match=re.search(rb" (\d+):(\d+\.\d+)+elapsed .* (\d+)maxresident\)k", process.stderr) |
|
time_taken=int(match[1])*60+float(match[2]) |
|
maxresident=int(match[3]) |
|
file_size_bytes=Path("/tmp/c.pdf").stat().st_size |
|
return time_taken, maxresident, file_size_bytes |
|
|
|
|
|
result=[] |
|
for item in data_items: |
|
header,commands=item.strip().split('\n', maxsplit=1) |
|
match=re.fullmatch(r"\s*#\s*Print time:\s*([0-9.]+)\n(.*)", commands, flags=re.DOTALL) |
|
print_time='' |
|
if match: |
|
print_time=f"{float(match[1]):.01f}" |
|
commands=match[2] |
|
time_taken, maxresident, file_size_bytes=benchmark(commands) |
|
result.append(( header, commands, time_taken, maxresident, file_size_bytes, print_time )) |
|
result.sort(key=lambda x: x[2]) |
|
|
|
for header, commands, time_taken, maxresident, file_size_bytes, print_time in result: |
|
print(f"| {header} | {time_taken:.3f} | {maxresident} | {file_size_bytes/1024:.1f} | {print_time} |") |
|
|
|
for header, commands, time_taken, maxresident, file_size_bytes, print_time in result: |
|
print(f""" |
|
## {header}\n |
|
```bash |
|
{commands} |
|
``` |
|
""".strip() + "\n") |
|
|
|
|
|
# sort -t="|" -n -k2 |