changeset 226:0c5bc599e661

Optimized the variance calculations in MultisetStatistics. Improves the processing performance in "sample" mode.
author shade
date Mon, 04 Nov 2013 12:09:04 +0100
parents 56852f8f4a8d
children d43fbb55c77b
files jmh-core/src/main/java/org/openjdk/jmh/util/internal/AbstractStatistics.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/DoubleIterator.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/ListStatistics.java jmh-core/src/main/java/org/openjdk/jmh/util/internal/MultisetStatistics.java
diffstat 4 files changed, 24 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/AbstractStatistics.java	Mon Nov 04 11:51:26 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/AbstractStatistics.java	Mon Nov 04 12:09:04 2013 +0100
@@ -194,20 +194,4 @@
         return Math.sqrt(getVariance());
     }
 
-    @Override
-    public double getVariance() {
-        if (getN() > 0) {
-            double v = 0;
-            double m = getMean();
-            for (DoubleIterator it = valuesIterator(); it.hasNext(); ) {
-                double d = it.next();
-                v += Math.pow(d - m, 2);
-            }
-            return v / (getN() - 1);
-        } else {
-            return Double.NaN;
-        }
-    }
-
-    protected abstract DoubleIterator valuesIterator();
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/DoubleIterator.java	Mon Nov 04 11:51:26 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,33 +0,0 @@
-/*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package org.openjdk.jmh.util.internal;
-
-public interface DoubleIterator {
-
-    boolean hasNext();
-
-    double next();
-
-}
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/ListStatistics.java	Mon Nov 04 11:51:26 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/ListStatistics.java	Mon Nov 04 12:09:04 2013 +0100
@@ -26,7 +26,6 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
 
 /**
@@ -140,19 +139,17 @@
     }
 
     @Override
-    protected DoubleIterator valuesIterator() {
-        return new DoubleIterator() {
-            private final Iterator<Double> backIterator = values.iterator();
+    public double getVariance() {
+        if (getN() > 0) {
+            double v = 0;
+            double m = getMean();
+            for (double d : values) {
+                v += Math.pow(d - m, 2);
+            }
+            return v / (getN() - 1);
+        } else {
+            return Double.NaN;
+        }
+    }
 
-            @Override
-            public boolean hasNext() {
-                return backIterator.hasNext();
-            }
-
-            @Override
-            public double next() {
-                return backIterator.next();
-            }
-        };
-    }
 }
--- a/jmh-core/src/main/java/org/openjdk/jmh/util/internal/MultisetStatistics.java	Mon Nov 04 11:51:26 2013 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/util/internal/MultisetStatistics.java	Mon Nov 04 12:09:04 2013 +0100
@@ -24,9 +24,6 @@
  */
 package org.openjdk.jmh.util.internal;
 
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
 public class MultisetStatistics extends AbstractStatistics {
 
     private final Multiset<Double> values;
@@ -86,35 +83,17 @@
     }
 
     @Override
-    protected DoubleIterator valuesIterator() {
-        return new DoubleIterator() {
-            private Iterator<Double> current = values.keys().iterator();
-            private int count;
-            private Double val;
+    public double getVariance() {
+        if (getN() > 0) {
+            double v = 0;
+            double m = getMean();
+            for (double d : values.keys()) {
+                v += Math.pow(d - m, 2) * values.count(d);
+            }
+            return v / (getN() - 1);
+        } else {
+            return Double.NaN;
+        }
+    }
 
-            private void ensureNonEmpty() {
-                while (count == 0 && current.hasNext()) {
-                    val = current.next();
-                    count = values.count(val);
-                }
-            }
-
-            @Override
-            public boolean hasNext() {
-                ensureNonEmpty();
-                return (count > 0);
-            }
-
-            @Override
-            public double next() {
-                ensureNonEmpty();
-                if (count > 0) {
-                    count--;
-                    return val;
-                } else {
-                    throw new NoSuchElementException("No next element.");
-                }
-            }
-        };
-    }
 }