commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1363875 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java
Date Fri, 20 Jul 2012 17:12:30 GMT
Author: erans
Date: Fri Jul 20 17:12:30 2012
New Revision: 1363875

URL: http://svn.apache.org/viewvc?rev=1363875&view=rev
Log:
MATH-797
Performance improvement: Caching the converted rules that were already
retrieved, so that the conversion from "Number" occurs only once per rule.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java?rev=1363875&r1=1363874&r2=1363875&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java
Fri Jul 20 17:12:30 2012
@@ -36,6 +36,9 @@ public abstract class BaseRuleFactory<T 
     /** List of points and weights, indexed by the order of the rule. */
     private final Map<Integer, Pair<T[], T[]>> pointsAndWeights
         = new TreeMap<Integer, Pair<T[], T[]>>();
+    /** Cache for double-precision rules. */
+    private final Map<Integer, Pair<double[], double[]>> pointsAndWeightsDouble
+        = new TreeMap<Integer, Pair<double[], double[]>>();
 
     /**
      * Gets a copy of the quadrature rule with given number of integration points.
@@ -44,7 +47,23 @@ public abstract class BaseRuleFactory<T 
      * @return a copy of the integration rule.
      */
     public Pair<double[], double[]> getRule(int numberOfPoints) {
-        return convertToDouble(getRuleInternal(numberOfPoints));
+        // Try to obtain the rule from the cache.
+        Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);
+
+        if (cached == null) {
+            // Rule not computed yet.
+
+            // Compute the rule.
+            final Pair<T[], T[]> rule = getRuleInternal(numberOfPoints);
+            cached = convertToDouble(rule);
+
+            // Cache it.
+            pointsAndWeightsDouble.put(numberOfPoints, cached);
+        }
+
+        // Return a copy.
+        return new Pair<double[], double[]>(cached.getFirst().clone(),
+                                            cached.getSecond().clone());
     }
 
     /**



Mime
View raw message