From 407de02fe4a6ce93808d694271fee636bc0ab80e Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Thu, 18 Apr 2013 19:29:05 +0100 Subject: [PATCH] Use binary search for get_layer_range. Gives a 13% speed up on Teethy_Tiki.stl (thing:49334) with default settings on my laptop. When $self->layers->[0]->slice_z == $min_z the returned $min_layer value is 0 rather than the (incorrect?) -1 returned by the old code. --- lib/Slic3r/Print/Object.pm | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index d7dd3f70e..d4052fcc9 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -95,18 +95,33 @@ sub get_layer_range { # $min_layer is the uppermost layer having slice_z <= $min_z # $max_layer is the lowermost layer having slice_z >= $max_z - my ($min_layer, $max_layer) = (0, undef); - for my $i (0 .. $#{$self->layers}) { - if ($self->layers->[$i]->slice_z >= $min_z) { - $min_layer = $i - 1; - for my $k ($i .. $#{$self->layers}) { - if ($self->layers->[$k]->slice_z >= $max_z) { - $max_layer = $k - 1; - last; - } - } + my ($min_layer, $max_layer); + + my ($bottom, $top) = (0, $#{$self->layers}); + while (1) { + my $mid = $bottom+int(($top - $bottom)/2); + if ($mid == $top || $mid == $bottom) { + $min_layer = $mid; last; } + if ($self->layers->[$mid]->slice_z >= $min_z) { + $top = $mid; + } else { + $bottom = $mid; + } + } + $top = $#{$self->layers}; + while (1) { + my $mid = $bottom+int(($top - $bottom)/2); + if ($mid == $top || $mid == $bottom) { + $max_layer = $mid; + last; + } + if ($self->layers->[$mid]->slice_z < $max_z) { + $bottom = $mid; + } else { + $top = $mid; + } } return ($min_layer, $max_layer); }