forked from Elucidation/tensorflow_chessbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchsvg.py
More file actions
127 lines (100 loc) · 11.7 KB
/
benchsvg.py
File metadata and controls
127 lines (100 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import time
from io import BytesIO
import os
os.environ['path'] += r";C:\Program Files\GTK3-Runtime Win64\bin"
import cairosvg
import wand.image
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
ex_svg = """<?xml version="1.0" encoding="utf-8"?>
<svg id="svg2" viewBox="0 0 500 670" xmlns="http://www.w3.org/2000/svg">
<desc>Source: openclipart.org/detail/209545</desc>
<path id="path56" style="fill: rgb(255, 255, 255);" d="M 185.636 372.915 C 196.331 371.992 211.227 369.878 230.352 366.674 C 245.718 365.283 259.005 367.383 270.154 372.915 C 288.339 381.345 301.605 397.43 309.956 421.148 C 321.132 451.917 319.087 487.104 303.728 526.685 C 289.674 562.106 273.916 585.853 256.193 597.939 C 247.295 604.032 230.976 608.953 207.259 612.625 C 177.426 617.758 153.247 618.482 134.592 614.719 C 100.081 608.712 74.479 590.064 57.7 558.833 C 34.919 515.515 26.483 476.892 32.56 442.848 C 39.052 411.156 57.238 388.297 87.049 374.337 C 107.571 365.04 125.694 362.222 141.602 365.978 C 163.94 371.049 178.606 373.392 185.636 372.915 Z"/>
<path id="path222" style="fill: rgb(255, 255, 255);" d="M 96.878 426.786 C 107.111 427.17 114.523 430.224 119.203 435.826 C 123.89 440.512 124.836 445.341 122.023 450.497 C 119.203 454.714 113.665 456.83 105.235 456.83 C 97.283 456.354 90.329 454.254 84.236 450.497 C 78.706 446.769 77.519 441.847 80.814 435.826 C 84.472 429.734 89.869 426.786 96.878 426.786 Z"/>
<path id="path224" style="fill: rgb(255, 145, 145);" d="M 163.94 393.914 L 172.363 392.517 C 178.363 392.032 183.519 392.032 187.736 392.517 C 194.214 392.97 207.714 389.697 228.236 382.759 C 219.827 390.64 209.836 396.485 198.204 400.163 C 187.972 403.906 180.003 405.78 174.395 405.78 C 166.98 406.234 159.24 404.388 151.372 400.163 C 146.217 397.43 140.684 392.75 134.592 386.181 L 126.93 376.432 L 137.404 382.759 C 146.217 387.352 155.107 391.117 163.94 393.914 Z"/>
<path id="path226" style="fill: rgb(128, 0, 5);" d="M 140.193 384.789 C 148.098 386.181 156.747 384.789 166.036 380.648 C 172.584 380.181 175.089 379.009 173.692 377.126 C 174.152 376.189 174.706 375.274 175.089 374.337 C 176.503 372.455 177.669 371.992 178.606 372.915 C 180.486 373.846 186.105 373.626 195.385 372.291 L 191.869 377.126 C 189.995 380.401 190.714 382.978 193.994 384.859 C 200.065 387.602 208.196 387.843 218.407 385.485 L 206.54 390.392 C 193.994 395.548 183.519 398.367 175.089 398.828 C 168.614 399.304 160.887 397.195 152.067 392.517 L 140.193 384.789 Z"/>
<path id="path14" style="fill: rgb(140, 161, 1);" d="M 456.781 263.87 L 464.429 285.571 L 467.233 296.749 L 469.349 319.145 L 469.349 328.888 L 467.233 347.793 L 462.313 365.283 L 451.859 385.485 L 442.109 398.133 L 440.696 398.751 L 440.001 400.163 L 434.382 405.085 L 435.775 400.163 L 437.182 391.117 L 436.471 386.181 L 435.775 374.337 L 432.977 362.463 L 423.206 336.551 L 419.691 330.315 L 402.932 306.492 L 380.592 283.484 L 349.843 259.041 L 324.003 231.113 L 310.041 209.411 L 299.502 186.318 L 290.464 154.22 L 287.645 122.769 L 288.339 106.684 L 292.551 82.888 L 300.213 59.157 L 305.815 94.754 L 312.073 113.636 L 323.306 131.127 L 336.578 146.487 L 429.547 223.372 L 444.908 242.978 L 456.781 263.87 Z"/>
<path id="path16" style="fill: rgb(97, 110, 1);" d="M 444.908 242.978 L 456.781 263.87 L 464.429 285.571 L 467.233 296.749 L 469.349 317.747 L 467.943 344.973 L 463.732 361.766 L 456.781 378.548 L 444.211 396.03 L 442.818 396.03 L 442.818 397.43 L 435.775 405.085 L 451.164 379.945 L 458.172 363.159 L 461.616 345.684 L 462.313 337.246 L 459.6 313.529 L 454.657 300.263 L 453.961 298.844 L 452.561 296.749 L 442.818 275.822 L 430.243 259.041 L 412.057 240.854 L 352.662 192.63 L 326.04 165.39 L 309.345 137.441 L 300.213 109.425 L 296.691 89.905 L 298.11 72.421 L 300.91 65.406 L 302.301 80.077 L 308.635 106.684 L 321.906 130.418 L 330.258 140.878 L 340.789 150.705 L 429.547 223.372 L 444.908 242.978 Z"/>
<path id="path18" style="fill: rgb(108, 125, 2);" d="M 358.188 195.45 L 421.119 247.807 L 437.182 266.69 L 447.647 283.484 L 455.375 300.96 L 460.294 319.842 L 462.313 335.932 L 455.375 305.88 L 437.182 270.218 L 418.995 249.213 L 409.245 240.159 L 350.561 193.327 L 330.961 173.748 L 310.659 144.393 L 298.11 111.52 L 293.978 92.021 L 298.807 87.802 L 308.635 129.721 L 314.173 143.69 L 328.859 166.093 L 356.1 194.718 L 358.188 195.45 Z"/>
<path id="path20" style="fill: rgb(104, 120, 2);" d="M 309.345 105.99 L 313.477 115.042 L 309.345 117.152 L 303.031 124.793 L 297.394 134.642 L 295.369 145.791 L 296.691 154.917 L 296.691 159.057 L 298.807 173.748 L 305.815 200.357 L 293.261 165.39 L 288.339 138.854 L 287.645 121.378 L 290.464 94.754 L 300.213 59.157 L 303.031 86.405 L 309.345 105.99 Z"/>
<path id="path22" style="fill: rgb(108, 125, 2);" d="M 330.961 170.305 L 333.773 171.625 L 334.476 172.321 L 334.476 175.14 L 333.773 176.548 L 333.773 178.656 L 333.078 182.178 L 333.773 203.076 L 334.476 204.49 L 336.578 212.926 L 331.658 201.67 L 330.258 199.66 L 328.859 194.022 L 327.446 184.272 L 327.446 180.062 L 328.142 176.548 L 329.562 172.321 L 330.258 171.007 L 330.961 170.305 Z"/>
<path id="path24" style="fill: rgb(108, 125, 2);" d="M 367.32 205.185 L 368.043 208.004 L 367.32 222.675 L 368.747 229.692 L 371.459 236.644 L 372.879 238.76 L 373.576 241.557 L 374.271 242.262 L 374.982 243.674 L 375.679 245.777 L 376.373 247.11 L 377.09 249.213 L 377.09 250.605 L 377.787 250.605 L 377.787 252.033 L 377.09 250.605 L 365.233 228.989 L 362.413 215.029 L 362.413 210.106 L 361.703 207.287 L 361.703 204.49 L 362.413 204.49 L 362.413 202.373 L 363.108 201.67 L 363.825 201.67 L 367.32 205.185 Z"/>
<path id="path26" style="fill: rgb(108, 125, 2);" d="M 415.501 278.561 L 409.245 268.096 L 400.81 247.807 L 397.378 233.846 L 397.378 228.293 L 398.075 228.989 L 400.197 230.402 L 401.518 232.433 L 402.932 235.245 L 403.628 245.777 L 407.846 261.086 L 415.501 278.561 Z"/>
<path id="path28" style="fill: rgb(108, 125, 2);" d="M 434.382 276.447 L 434.382 275.822 L 435.079 271.61 L 435.775 269.509 L 436.471 268.812 L 437.182 270.218 L 440.696 294.633 L 439.291 314.232 L 439.291 305.171 L 435.079 281.361 L 435.079 279.258 L 434.382 276.447 Z"/>
<path id="path30" style="fill: rgb(108, 125, 2);" d="M 455.375 323.982 L 456.781 317.045 L 458.172 315.623 L 460.294 317.045 L 457.476 325.394 L 456.781 328.888 L 453.961 338.022 L 449.047 348.503 L 455.375 328.193 L 455.375 323.982 Z"/>
<path id="path32" style="fill: rgb(108, 125, 2);" d="M 344.225 174.445 L 334.476 176.548 L 332.352 171.007 L 334.476 168.905 L 336.578 167.493 L 340.093 167.493 L 347.045 170.305 L 352.662 171.625 L 365.233 176.548 L 353.358 174.445 L 344.225 174.445 Z"/>
<path id="path34" style="fill: rgb(108, 125, 2);" d="M 385.449 202.373 L 381.315 201.053 L 377.787 201.67 L 375.679 201.67 L 373.576 202.373 L 368.043 208.004 L 362.413 201.053 L 366.624 198.944 L 376.373 198.944 L 393.161 203.794 L 385.449 202.373 Z"/>
<path id="path36" style="fill: rgb(108, 125, 2);" d="M 423.206 228.989 L 419.691 228.293 L 416.197 228.293 L 412.057 227.59 L 409.245 227.59 L 402.215 229.692 L 400.81 229.692 L 400.197 230.402 L 397.378 228.989 L 400.197 226.191 L 410.643 224.772 L 412.76 225.494 L 414.863 226.886 L 423.206 228.989 Z"/>
<path id="path38" style="fill: rgb(108, 125, 2);" d="M 447.647 269.509 L 454.657 273.735 L 446.241 270.218 L 442.818 269.509 L 440.696 270.218 L 437.182 270.218 L 435.775 268.812 L 436.471 266.69 L 443.515 266.69 L 445.602 267.399 L 447.647 269.509 Z"/>
<path id="path228" style="fill: rgb(162, 176, 2);" d="M 217.087 208.004 C 194.214 256.002 181.878 300.263 180.003 340.761 C 179.08 365.503 181.417 383.617 187.033 395.327 C 179.543 399.467 174.01 399.688 170.254 396.03 C 168.373 394.156 167.214 390.883 166.738 386.181 L 166.036 380.648 C 164.161 375.495 163.237 366.915 163.237 354.73 C 163.7 330.536 168.373 300.263 177.284 263.87 C 187.972 221.489 206.158 181.695 231.757 144.393 C 255.091 110.449 267.42 87.327 268.833 75.24 L 270.154 56.359 L 275.77 53.539 C 280.927 52.133 286.784 53.077 293.261 56.359 C 302.542 61.031 306.306 65.008 304.424 68.203 C 295.612 78.996 288.816 91.325 284.207 105.293 C 281.864 111.302 272.731 125.112 256.888 146.487 C 238.709 170.305 225.445 190.75 217.087 208.004 Z"/>
<path id="path230" style="fill: rgb(131, 150, 2);" d="M 209.36 209.411 C 222.384 185.678 236.828 163.516 252.677 143.078 C 267.114 123.955 278.988 102.013 288.339 77.335 C 290.683 71.257 291.855 64.064 291.855 55.649 L 293.261 56.359 C 302.542 61.031 306.306 65.008 304.424 68.203 C 295.533 78.996 288.816 91.325 284.128 105.293 C 281.785 111.302 272.731 125.112 256.888 146.487 C 238.709 170.305 225.445 190.75 217.087 208.004 C 194.214 256.002 181.878 300.263 180.003 340.761 C 179.08 365.503 181.417 383.617 187.033 395.327 C 179.543 399.467 174.01 399.688 170.254 396.03 L 167.435 389.697 C 171.653 391.117 174.231 391.813 175.16 391.813 C 178.84 392.274 180.003 386.181 178.606 373.626 C 176.347 349.816 175.65 327.738 176.566 307.201 C 177.909 279.719 188.916 247.11 209.36 209.411 Z"/>
<path id="path232" style="fill: rgb(200, 219, 70);" d="M 273.669 68.928 C 274.137 63.843 274.834 60.79 275.77 59.853 C 276.707 59.397 277.653 60.329 278.512 62.671 C 279.903 65.406 278.512 73.36 274.379 86.405 C 269.692 101.778 259.708 119.495 244.32 139.549 C 234.492 152.58 228.718 159.752 226.836 161.172 C 224.5 163.053 225.679 160.697 230.352 154.22 C 241.501 136.978 249.858 125.035 255.489 118.559 C 259.708 113.402 263.84 105.293 268.059 94.116 C 271.339 83.825 273.194 75.482 273.669 68.928 Z"/>
<path style="stroke: rgb(0, 0, 0); fill: rgb(255, 0, 224);" d="M 28.824 127.324 L 187.677 107.468 L 176.788 202.907 L 105.688 286.818 L 118.499 172.162"/>
</svg>"""
def s2str(delta: float, is_nano=False) -> str:
"""
Converts seconds (or nanos) to a well organized string
Args:
delta (float): the seconds to convert
is_nano (bool): whether the delta is in nanoseconds or not
Returns:
str: the converted string, ex: 135.123456s -> "01min 15sec 123mil 456us"
"""
if is_nano:
delta *= 1.0e9
minute = int(delta / 60)
sec = int(delta % 60)
millisec = int((delta - int(delta)) * 1000)
microsec = int((delta * 1000 - int(delta * 1000) * 1000))
out = ""
if minute > 0:
out += f"{minute}min "
if sec > 0:
out += f"{sec}sec "
if millisec > 0:
out += f"{millisec}mil "
if microsec > 0:
out += f"{microsec}us"
return out
# === Cairo SVG ===
cairo_times = ()
for _ in range(15):
tStart = time.perf_counter()
cairosvg.svg2png(bytestring=ex_svg, write_to=BytesIO(b""))
tEnd = time.perf_counter()
print(f"Cairo SVG: {s2str(tEnd - tStart)} {' '*10}", end="\r")
cairo_times += (tEnd - tStart,)
time.sleep(0.5)
# === ImageWand Thing ===
wand_times = ()
for _ in range(15):
tStart = time.perf_counter()
with wand.image.Image(blob=ex_svg.encode("utf-8"), format="svg") as image:
_ = image.make_blob("png")
tEnd = time.perf_counter()
print(f"Wand: {s2str(tEnd - tStart)}{' '*10}", end="\r")
wand_times += (tEnd - tStart,)
time.sleep(0.5)
# === Libsvg + ReportLab ===
libs_times = ()
for _ in range(15):
tStart = time.perf_counter()
drawing = svg2rlg(BytesIO(ex_svg.encode("utf-8")))
_ = renderPM.drawToPIL(d=drawing)
tEnd = time.perf_counter()
print(f"Lib SVG + Report Lab: {s2str(tEnd - tStart)} {' '*10}", end="\r")
libs_times += (tEnd - tStart,)
time.sleep(0.5)
# === Summary ===
print(f"Cairo: "
f"{s2str(min(cairo_times))} <- "
f"{s2str(sum(cairo_times) / len(cairo_times))} -> "
f"{s2str(max(cairo_times))}")
print(f"Wand: "
f"{s2str(min(wand_times))} <- "
f"{s2str(sum(wand_times) / len(wand_times))} -> "
f"{s2str(max(wand_times))}")
print(f"Lib: "
f"{s2str(min(libs_times))} <- "
f"{s2str(sum(libs_times) / len(libs_times))} -> "
f"{s2str(max(libs_times))}")