From c2bb234c20321d896db2a38218ac32ea1e548086 Mon Sep 17 00:00:00 2001 From: Eyal Soha Date: Fri, 13 Jul 2018 00:29:43 +0300 Subject: [PATCH 1/5] Remove extra import math --- src/octoprint/cli/analysis.py | 3 +++ src/octoprint/util/gcodeInterpreter.py | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/octoprint/cli/analysis.py b/src/octoprint/cli/analysis.py index 34c90d78d..bb69de05c 100644 --- a/src/octoprint/cli/analysis.py +++ b/src/octoprint/cli/analysis.py @@ -71,3 +71,6 @@ def gcode_command(path, speedx, speedy, speedz, offset, maxt, throttle, throttle click.echo("DONE:{}s".format(time.time() - start_time)) click.echo("RESULTS:") click.echo(yaml.safe_dump(interpreter.get_result(), default_flow_style=False, indent=" ", allow_unicode=True)) + +if __name__ == "__main__": + gcode_command() diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index 061564ce0..36f227493 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -498,7 +498,6 @@ def getCodeInt(line, code): def getCodeFloat(line, code): - import math n = line.find(code) + 1 if n < 1: return None @@ -511,4 +510,3 @@ def getCodeFloat(line, code): return val if not (math.isnan(val) or math.isinf(val)) else None except: return None - From 111a37e97eefdd8165998ec5e26806d4200862ad Mon Sep 17 00:00:00 2001 From: Eyal Soha Date: Fri, 13 Jul 2018 00:42:33 +0300 Subject: [PATCH 2/5] Unroll record for improved speed --- src/octoprint/util/gcodeInterpreter.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index 36f227493..b9faf9765 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -152,12 +152,12 @@ class MinMax3D(object): self.max = Vector3D(None, None, None) def record(self, coordinate): - for c in "xyz": - current_min = getattr(self.min, c) - current_max = getattr(self.max, c) - value = getattr(coordinate, c) - setattr(self.min, c, value if current_min is None or value < current_min else current_min) - setattr(self.max, c, value if current_max is None or value > current_max else current_max) + self.min.x = coordinate.x if not self.min.x else min(self.min.x, coordinate.x) + self.min.y = coordinate.y if not self.min.y else min(self.min.y, coordinate.y) + self.min.z = coordinate.z if not self.min.z else min(self.min.z, coordinate.z) + self.max.x = coordinate.x if not self.max.x else max(self.max.x, coordinate.x) + self.max.y = coordinate.y if not self.max.y else max(self.max.y, coordinate.y) + self.max.z = coordinate.z if not self.max.z else max(self.max.z, coordinate.z) @property def size(self): From 792fe328ef7a2d86ef5f86e50e58ebbdfb08648f Mon Sep 17 00:00:00 2001 From: Eyal Soha Date: Fri, 13 Jul 2018 00:44:04 +0300 Subject: [PATCH 3/5] Use infinity instead of None for faster MinMax3d --- src/octoprint/util/gcodeInterpreter.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index b9faf9765..6753d9238 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -148,16 +148,16 @@ class MinMax3D(object): """ def __init__(self): - self.min = Vector3D(None, None, None) - self.max = Vector3D(None, None, None) + self.min = Vector3D(float("inf"), float("inf"), float("inf")) + self.max = Vector3D(-float("inf"), -float("inf"), -float("inf")) def record(self, coordinate): - self.min.x = coordinate.x if not self.min.x else min(self.min.x, coordinate.x) - self.min.y = coordinate.y if not self.min.y else min(self.min.y, coordinate.y) - self.min.z = coordinate.z if not self.min.z else min(self.min.z, coordinate.z) - self.max.x = coordinate.x if not self.max.x else max(self.max.x, coordinate.x) - self.max.y = coordinate.y if not self.max.y else max(self.max.y, coordinate.y) - self.max.z = coordinate.z if not self.max.z else max(self.max.z, coordinate.z) + self.min.x = min(self.min.x, coordinate.x) + self.min.y = min(self.min.y, coordinate.y) + self.min.z = min(self.min.z, coordinate.z) + self.max.x = max(self.max.x, coordinate.x) + self.max.y = max(self.max.y, coordinate.y) + self.max.z = max(self.max.z, coordinate.z) @property def size(self): @@ -165,7 +165,7 @@ class MinMax3D(object): for c in "xyz": min = getattr(self.min, c) max = getattr(self.max, c) - value = abs(max - min) if min is not None and max is not None else 0.0 + value = abs(max - min) if not math.isinf(min) and not math.isinf(max) else 0.0 setattr(result, c, value) return result From 8c1262dd3fb4d0d7723b7097493c1cbc14d4825f Mon Sep 17 00:00:00 2001 From: Eyal Date: Fri, 13 Jul 2018 11:57:52 +0300 Subject: [PATCH 4/5] Replace infinity tests with inequality --- src/octoprint/util/gcodeInterpreter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index 6753d9238..e4d0678fc 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -165,7 +165,7 @@ class MinMax3D(object): for c in "xyz": min = getattr(self.min, c) max = getattr(self.max, c) - value = abs(max - min) if not math.isinf(min) and not math.isinf(max) else 0.0 + value = abs(max - min) if max >= min else 0.0 setattr(result, c, value) return result From 542edbc3e632db9c78483c6077a797f4b88342cb Mon Sep 17 00:00:00 2001 From: Eyal Soha Date: Fri, 13 Jul 2018 20:07:35 +0300 Subject: [PATCH 5/5] Revert "Use infinity instead of None for faster MinMax3d" This reverts commit 792fe328ef7a2d86ef5f86e50e58ebbdfb08648f. Most of the speed-up comes from removing the unneeded import and the unrolling so no big deal. --- src/octoprint/util/gcodeInterpreter.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index e4d0678fc..b9faf9765 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -148,16 +148,16 @@ class MinMax3D(object): """ def __init__(self): - self.min = Vector3D(float("inf"), float("inf"), float("inf")) - self.max = Vector3D(-float("inf"), -float("inf"), -float("inf")) + self.min = Vector3D(None, None, None) + self.max = Vector3D(None, None, None) def record(self, coordinate): - self.min.x = min(self.min.x, coordinate.x) - self.min.y = min(self.min.y, coordinate.y) - self.min.z = min(self.min.z, coordinate.z) - self.max.x = max(self.max.x, coordinate.x) - self.max.y = max(self.max.y, coordinate.y) - self.max.z = max(self.max.z, coordinate.z) + self.min.x = coordinate.x if not self.min.x else min(self.min.x, coordinate.x) + self.min.y = coordinate.y if not self.min.y else min(self.min.y, coordinate.y) + self.min.z = coordinate.z if not self.min.z else min(self.min.z, coordinate.z) + self.max.x = coordinate.x if not self.max.x else max(self.max.x, coordinate.x) + self.max.y = coordinate.y if not self.max.y else max(self.max.y, coordinate.y) + self.max.z = coordinate.z if not self.max.z else max(self.max.z, coordinate.z) @property def size(self): @@ -165,7 +165,7 @@ class MinMax3D(object): for c in "xyz": min = getattr(self.min, c) max = getattr(self.max, c) - value = abs(max - min) if max >= min else 0.0 + value = abs(max - min) if min is not None and max is not None else 0.0 setattr(result, c, value) return result