From ebfb3b1d2de36c811bd13d92d58c0e2310e721e7 Mon Sep 17 00:00:00 2001 From: shawnlaffan Date: Thu, 28 Mar 2024 16:07:42 +1100 Subject: [PATCH] micro-optimise rand_curveball We store scalar variables for the array lengths anyway so we might as well use them. --- lib/Biodiverse/Randomise/CurveBall.pm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Biodiverse/Randomise/CurveBall.pm b/lib/Biodiverse/Randomise/CurveBall.pm index 1063dd160..c3978187f 100644 --- a/lib/Biodiverse/Randomise/CurveBall.pm +++ b/lib/Biodiverse/Randomise/CurveBall.pm @@ -117,13 +117,13 @@ END_PROGRESS_TEXT } } - printf "[RANDOMISE] Randomise using curveball algorithm for %s labels from %s groups\n", - scalar @sorted_labels, scalar @sorted_groups; + say "[RANDOMISE] Randomise using curveball algorithm for $n_labels labels from $n_groups groups"; # No need to consider groups that cannot be swapped out. # Could use a binary search but this is only done once per iteration. if (keys %has_max_richness) { @sorted_groups = grep {!exists $has_max_richness{$_}} @sorted_groups; + $n_groups = scalar @sorted_groups; } $progress_bar->reset; @@ -154,12 +154,12 @@ END_PROGRESS_TEXT ) { $attempts++; - my $group1 = $sorted_groups[int $rand->rand (scalar @sorted_groups)]; - my $group2 = $sorted_groups[int $rand->rand (scalar @sorted_groups)]; + my $group1 = $sorted_groups[int $rand->rand ($n_groups)]; + my $group2 = $sorted_groups[int $rand->rand ($n_groups)]; while ($group1 eq $group2) { # handle pathological case of only one group - last MAIN_ITER if scalar @sorted_groups == 1; - $group2 = $sorted_groups[int $rand->rand (scalar @sorted_groups)]; + last MAIN_ITER if $n_groups == 1; + $group2 = $sorted_groups[int $rand->rand ($n_groups)]; } my \%labels1 = $lb_hash{$group1};