Modified heap profiling to only dump at end of program, not after every generation.
[surreal-numbers] / chapter-1-experiments / ch1-breeding-numbers.go
index fc1c397..c5baa14 100644 (file)
@@ -10,7 +10,6 @@ import (
     "os"
     "runtime/pprof"
     "sort"
     "os"
     "runtime/pprof"
     "sort"
-    "strconv"
 )
 
 // =============================================================================
 )
 
 // =============================================================================
@@ -222,7 +221,7 @@ func (s *surrealSet) remove(num surrealNumber, u surrealUniverse) {
 func main() {
     // Flags to enable various performance profiling options.
     cpuProfile := flag.String("cpuprofile", "", "Filename for saving CPU profile output.")
 func main() {
     // Flags to enable various performance profiling options.
     cpuProfile := flag.String("cpuprofile", "", "Filename for saving CPU profile output.")
-    memProfilePrefix := flag.String("memprofileprefix", "", "Filename PREFIX for saving memory profile output.")
+    memProfile := flag.String("memprofile", "", "Filename for saving memory profile output.")
     suppressOutput := flag.Bool("silent", false, "Suppress printing of numberline at end of simulation. Useful when profiling.")
 
     // Obtain termination conditions from user.
     suppressOutput := flag.Bool("silent", false, "Suppress printing of numberline at end of simulation. Useful when profiling.")
 
     // Obtain termination conditions from user.
@@ -261,27 +260,24 @@ func main() {
         }
         fmt.Printf(" %d", generation)
 
         }
         fmt.Printf(" %d", generation)
 
-        // First generate all possible reduced form symbols per Axiom 1.
-        potentialNumbers := permuteExistingNumbers(generation, universe)
-        // Now prune out any symbols which are NOT valid numbers per Axiom 2.
-        validNumbers := pruneInvalidNumbers(potentialNumbers, universe)
+        // First generate all possible new numbers in this generation.
+        potentiallyNewNumbers := permuteExistingNumbers(generation, universe)
         // Attempt to add the new numbers to the universe. Any duplicates will
         // be weeded out in the attempt.
         // Attempt to add the new numbers to the universe. Any duplicates will
         // be weeded out in the attempt.
-        addNumbersToUniverse(validNumbers, &universe)
-
-        // Setup any memory profiling requested by the user. This will snapshot
-        // the heap at the end of every generation.
-        if *memProfilePrefix != "" {
-            memProFile, err := os.Create(*memProfilePrefix + "-gen" + strconv.Itoa(generation) + ".mprof")
-            if err != nil {
-                log.Fatal("ERROR: Unable to write heap profile to disk:", err)
-            }
-            pprof.WriteHeapProfile(memProFile)
-            memProFile.Close()
-        }
+        addNumbersToUniverse(potentiallyNewNumbers, &universe)
     }
     fmt.Printf(".\n")
 
     }
     fmt.Printf(".\n")
 
+    // Setup any memory profiling requested by the user. This will snapshot
+    // the heap only at this point, after all numbers were generated.
+    if *memProfile != "" {
+        memProFile, err := os.Create(*memProfile)
+        if err != nil {
+            log.Fatal("ERROR: Unable to open heap profile output file:", err)
+        }
+        pprof.WriteHeapProfile(memProFile)
+        memProFile.Close()
+    }
 
     // Print the number line with generation on the horizontal axis and
     // magnitude on the vertical axis.
 
     // Print the number line with generation on the horizontal axis and
     // magnitude on the vertical axis.
@@ -313,7 +309,9 @@ func permuteExistingNumbers(generation int, universe surrealUniverse) []surrealN
             leftSet.insert(universe.numbers[i], universe)
             rightSet.insert(universe.numbers[j], universe)
             newSurrealNumber := surrealNumber{leftSet,rightSet,0.0,0,generation}
             leftSet.insert(universe.numbers[i], universe)
             rightSet.insert(universe.numbers[j], universe)
             newSurrealNumber := surrealNumber{leftSet,rightSet,0.0,0,generation}
-            numbers = append(numbers, newSurrealNumber)
+            if isValidSurrealNumber(newSurrealNumber, universe) {
+                numbers = append(numbers, newSurrealNumber)
+            }
         }
     }
     // Now build permutations with one empty set and one 1-element set.
         }
     }
     // Now build permutations with one empty set and one 1-element set.
@@ -329,16 +327,6 @@ func permuteExistingNumbers(generation int, universe surrealUniverse) []surrealN
     return numbers
 }
 
     return numbers
 }
 
-func pruneInvalidNumbers(candidates []surrealNumber, universe surrealUniverse) []surrealNumber {
-    var numbers []surrealNumber
-    for i := 0; i < len(candidates); i++ {
-        if isValidSurrealNumber(candidates[i], universe) {
-            numbers = append(numbers, candidates[i])
-        }
-    }
-    return numbers
-}
-
 // Although a non-reduced-form number is technically valid, for the purposes of
 // this program we're declaring it invalid.
 func isValidSurrealNumber(candidate surrealNumber, universe surrealUniverse) bool {
 // Although a non-reduced-form number is technically valid, for the purposes of
 // this program we're declaring it invalid.
 func isValidSurrealNumber(candidate surrealNumber, universe surrealUniverse) bool {