Skip to content

Commit 29b643d

Browse files
committed
refactor profiling scripts
1 parent 73aa81d commit 29b643d

13 files changed

+66
-75
lines changed

profiling/banded_matrix_solver.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
33
import time
44
import random
55
import csv
6-
from pathlib import Path
6+
import pathlib
77
from ezdxf.math.linalg import (
88
Matrix,
99
BandedMatrixLU,
1010
banded_matrix,
1111
LUDecomposition,
1212
)
1313

14-
DIR = Path("~/Desktop/Outbox").expanduser()
14+
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
15+
if not CWD.exists():
16+
CWD = pathlib.Path(".")
1517

1618

1719
def random_values(n, spread=1.0):
@@ -48,7 +50,7 @@ def profile(func, *args):
4850

4951
REPEAT = 5
5052

51-
with open(DIR / "profiling_banded_matrix.csv", mode="wt", newline="") as f:
53+
with open(CWD / "profiling_banded_matrix.csv", mode="wt", newline="") as f:
5254
writer = csv.writer(f, dialect="excel")
5355
writer.writerow(["Parameters", "Standard LU", "Banded LU", "Factor"])
5456
for size in range(10, 101, 5):

profiling/bezier4p_interpolation.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
3-
from typing import Iterable
43
import time
54
import ezdxf
6-
from pathlib import Path
5+
import pathlib
76
import math
87
from ezdxf.math import cubic_bezier_interpolation, BoundingBox
98
from ezdxf.render import random_3d_path
109

11-
DIR = Path("~/Desktop/Outbox").expanduser()
10+
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
11+
if not CWD.exists():
12+
CWD = pathlib.Path(".")
1213

1314

1415
def profile_bezier_interpolation(count, path):
@@ -33,7 +34,7 @@ def export_path(path):
3334
curve.approximate(20), dxfattribs={"layer": "Bézier", "color": 1}
3435
)
3536
doc.set_modelspace_vport(center=bbox.center, height=bbox.size[1])
36-
doc.saveas(DIR / "path1.dxf")
37+
doc.saveas(CWD / "path1.dxf")
3738

3839

3940
path = list(random_3d_path(100, max_step_size=10, max_heading=math.pi * 0.8))

profiling/bspline_interpolation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
3-
from typing import Iterable
43
import time
5-
import ezdxf
6-
from pathlib import Path
4+
import pathlib
75
import math
6+
import ezdxf
87
from ezdxf.math import (
98
global_bspline_interpolation,
109
BoundingBox,
@@ -13,8 +12,9 @@
1312
)
1413
from ezdxf.render import random_3d_path
1514

16-
DIR = Path("~/Desktop/Outbox").expanduser()
17-
15+
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
16+
if not CWD.exists():
17+
CWD = pathlib.Path(".")
1818

1919
def profile_bspline_interpolation(count, path):
2020
for _ in range(count):
@@ -43,7 +43,7 @@ def export_path(path):
4343
curve = global_bspline_interpolation(path)
4444
spline.apply_construction_tool(curve)
4545
doc.set_modelspace_vport(center=bbox.center, height=bbox.size[1])
46-
doc.saveas(DIR / "path1.dxf")
46+
doc.saveas(CWD / "path1.dxf")
4747

4848

4949
path = list(random_3d_path(100, max_step_size=10, max_heading=math.pi * 0.8))

profiling/bspline_point_calculation.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
3-
from typing import Iterable
43
import time
5-
import ezdxf
6-
from pathlib import Path
74
import math
85
from ezdxf.math import global_bspline_interpolation, linspace
96
from ezdxf.render import random_3d_path
107

11-
DIR = Path("~/Desktop/Outbox").expanduser()
12-
138
path = list(random_3d_path(100, max_step_size=10, max_heading=math.pi * 0.8))
149
spline = global_bspline_interpolation(path)
1510

profiling/convexhull.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
# License: MIT License
33
import random
44
import time
5+
import pathlib
56
import ezdxf
67

78
from ezdxf.math import Vec2, convex_hull_2d, is_point_left_of_line
89

10+
CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
11+
if not CWD.exists():
12+
CWD = pathlib.Path(".")
13+
914
SIZE = 100
1015
ROUNDS = 2000
1116

@@ -76,7 +81,7 @@ def export_dxf(points):
7681
msp.add_lwpolyline(hull, dxfattribs={"color": 2, "layer": "old_hull"})
7782
hull = convex_hull_2d(points)
7883
msp.add_lwpolyline(hull, dxfattribs={"color": 6, "layer": "new_hull"})
79-
doc.saveas(r"C:\Users\manfred\Desktop\Outbox\convexhull.dxf")
84+
doc.saveas(CWD / "convexhull.dxf")
8085

8186

8287
def main():
@@ -88,5 +93,6 @@ def main():
8893
print(f"ratio old/new: {old/new:.3f}")
8994
export_dxf(points)
9095

96+
9197
if __name__ == "__main__":
9298
main()

profiling/raw_data_reading.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
3-
import os
43
import time
5-
from ezdxf import EZDXF_TEST_FILES
4+
import ezdxf
65

7-
BIG_FILE = os.path.join(EZDXF_TEST_FILES, "CADKitSamples", "torso_uniform.dxf")
6+
BIG_FILE = ezdxf.options.test_files_path / "CADKitSamples" / "torso_uniform.dxf"
87

98

109
def load_ascii():

profiling/read_big_R12_files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright (c) 2019 Manfred Moitzi
22
# License: MIT License
33
import time
4-
from pathlib import Path
4+
import ezdxf
55

6-
DIR = Path(r"D:\Source\dxftest\CADKitSamples")
7-
_3D_MODEL = DIR / "fanuc-430-arm.dxf"
8-
_2D_PLAN = DIR / "AEC Plan Elev Sample.dxf"
6+
CWD = ezdxf.options.test_files_path / "CADKitSamples"
7+
_3D_MODEL = CWD / "fanuc-430-arm.dxf"
8+
_2D_PLAN = CWD / "AEC Plan Elev Sample.dxf"
99

1010

1111
def load_3D_model():

profiling/reading_samples.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
3-
import ezdxf
43
import os
54
from collections import Counter
65
import time
7-
from ezdxf import EZDXF_TEST_FILES
6+
import ezdxf
87

98
CADKIT = "CADKitSamples"
109
CADKIT_FILES = [
@@ -50,7 +49,7 @@ def count_entities(msp):
5049

5150

5251
for _name in CADKIT_FILES:
53-
filename = os.path.join(EZDXF_TEST_FILES, CADKIT, _name)
52+
filename = ezdxf.options.test_files_path / CADKIT / _name
5453
print(f"reading file: {filename}")
5554
start_reading = time.perf_counter()
5655
doc = ezdxf.readfile(filename)

profiling/rtree.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# Copyright (c) 2020, Manfred Moitzi
1+
# Copyright (c) 2020-2022, Manfred Moitzi
22
# License: MIT License
33
import time
4-
from pathlib import Path
54
from ezdxf.math import Vec3, closest_point
65
from ezdxf.math.rtree import RTree
76

@@ -10,8 +9,6 @@
109
except ImportError:
1110
plt = None
1211

13-
DIR = Path("~/Desktop/Outbox").expanduser()
14-
1512

1613
def random_points(n, size=1.0):
1714
return [Vec3.random() * size for _ in range(n)]
@@ -66,9 +63,7 @@ def profile_rtree_building(repeat: int, max_size: int):
6663
for size in range(100, 2000, 100):
6764
points = random_points(size, 50.0)
6865
name = f"RTree({size}, {max_size})"
69-
t0 = profile(
70-
profile_build_time_random_rtree, repeat, points, max_size
71-
)
66+
t0 = profile(profile_build_time_random_rtree, repeat, points, max_size)
7267
time_str = f"{t0:6.2f}s"
7368
print(f"Build random {name}, {repeat}x , {time_str}")
7469
log.append((size, t0))
@@ -81,9 +76,7 @@ def profile_rtree_contains_all_points(repeat: int, max_size: int):
8176
points = random_points(size, 50.0)
8277
tree = RTree(points, max_size)
8378
name = f"RTree({size}, {max_size})"
84-
t0 = profile(
85-
profile_tree_contains_points, repeat, tree, points
86-
)
79+
t0 = profile(profile_tree_contains_points, repeat, tree, points)
8780
time_str = f"{t0:6.2f}s"
8881
print(f"{name} contains all points, {repeat}x , {time_str}")
8982
log.append((size, t0))
@@ -98,9 +91,7 @@ def profile_rtree_nearest_neighbor(repeat: int, max_size: int):
9891
name = f"RTree({size}, {max_size})"
9992

10093
search_points = random_points(100, 50.0)
101-
t0 = profile(
102-
profile_tree_nearest_neighbor, repeat, tree, search_points
103-
)
94+
t0 = profile(profile_tree_nearest_neighbor, repeat, tree, search_points)
10495
time_str = f"{t0:6.2f}s"
10596
print(f"{name} nearest neighbor, {repeat}x , {time_str}")
10697
log.append((size, t0))
@@ -113,9 +104,7 @@ def profile_brute_force_contains_all_points(repeat: int):
113104
points = random_points(size, 50.0)
114105

115106
name = f"Brute Force({size})"
116-
t0 = profile(
117-
profile_brute_force_contains_points, repeat, points
118-
)
107+
t0 = profile(profile_brute_force_contains_points, repeat, points)
119108
time_str = f"{t0:6.2f}s"
120109
print(f"{name} contains all points, {repeat}x , {time_str}")
121110
log.append((size, t0))
@@ -171,15 +160,20 @@ def show_log(log, name: str):
171160
if PROFILE_RTREE_CONTAINS:
172161
log = profile_rtree_contains_all_points(10, max_size)
173162
if plt:
174-
show_log(log, f"Random RTree contains all points, max node size={max_size}")
163+
show_log(
164+
log,
165+
f"Random RTree contains all points, max node size={max_size}",
166+
)
175167
if PROFILE_BRUTE_FORCE_CONTAINS:
176168
log = profile_brute_force_contains_all_points(10)
177169
if plt:
178170
show_log(log, f"Brute force contains all points")
179171
if PROFILE_RTREE_NEIGHBOR:
180172
log = profile_rtree_nearest_neighbor(10, max_size)
181173
if plt:
182-
show_log(log, f"Random RTree nearest neighbor, max node size={max_size}")
174+
show_log(
175+
log, f"Random RTree nearest neighbor, max node size={max_size}"
176+
)
183177
if PROFILE_BRUTE_FORCE_NEIGHBOR:
184178
log = profile_brute_force_nearest_neighbor(10)
185179
if plt:

profiling/setup_new_drawing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019 Manfred Moitzi
1+
# Copyright (c) 2019-2022 Manfred Moitzi
22
# License: MIT License
33
from timeit import Timer
44
import ezdxf
@@ -16,12 +16,12 @@ def setup_drawing():
1616
def main(count):
1717
t = Timer("setup_drawing()", SETUP)
1818
time2 = t.timeit(count)
19-
print_result(time2, f"setup {count} new style DXF")
19+
print_result(time2, f"setting up {count} new DXF documents")
2020

2121

2222
def print_result(time, text):
23-
print(f"Profiling: {text}; takes {time:.2f} seconds")
23+
print(f"Profiling: {text} takes {time:.2f} seconds")
2424

2525

2626
if __name__ == "__main__":
27-
main(300)
27+
main(1000)

0 commit comments

Comments
 (0)