From 50b4a3cd894dcb0d7bf7bc4b522e8eec96e6b225 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Fri, 5 Apr 2019 11:22:59 -0700 Subject: [PATCH] Fix and optimize how progress is displayed while training trees --- .../randomforest/tree/ForestTrainer.java | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java b/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java index 83964aa..2686666 100644 --- a/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java +++ b/src/main/java/ca/joeltherrien/randomforest/tree/ForestTrainer.java @@ -71,20 +71,19 @@ public class ForestTrainer { final Random random = new Random(); for(int j=0; jbuilder() .treeResponseCombiner(treeResponseCombiner) .trees(trees) @@ -117,7 +116,8 @@ public class ForestTrainer { } if(displayProgress){ - System.out.println("\nFinished"); + System.out.println("\rFinished tree " + ntree + "/" + ntree + " trees"); + System.out.println("Finished"); } } @@ -136,10 +136,12 @@ public class ForestTrainer { } executorService.shutdown(); + + int prevNumberTreesSet = -1; while(!executorService.isTerminated()){ try{ - Thread.sleep(100); + Thread.sleep(500); } catch (InterruptedException e) { // do nothing; who cares? } @@ -152,7 +154,13 @@ public class ForestTrainer { } } - System.out.print("\rFinished " + numberTreesSet + "/" + ntree + " trees"); + // Only output trees set on screen if there was a change + // In some environments where standard output is streamed to a file this method below causes frequent writes to output + if(numberTreesSet != prevNumberTreesSet){ + System.out.print("\rFinished " + numberTreesSet + "/" + ntree + " trees"); + prevNumberTreesSet = numberTreesSet; + } + } } @@ -188,6 +196,8 @@ public class ForestTrainer { } executorService.shutdown(); + + int prevNumberTreesSet = -1; while(!executorService.isTerminated()){ try{ @@ -197,8 +207,15 @@ public class ForestTrainer { } if(displayProgress) { + int numberTreesSet = treeCount.get(); + + // Only output trees set on screen if there was a change + // In some environments where standard output is streamed to a file this method below causes frequent writes to output + if(numberTreesSet != prevNumberTreesSet){ + System.out.print("\rFinished " + numberTreesSet + "/" + ntree + " trees"); + prevNumberTreesSet = numberTreesSet; + } - System.out.print("\rFinished " + treeCount.get() + "/" + ntree + " trees"); } }