tmf: Move the symbol provider messages to their own package
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / internal / analysis / timing / core / segmentstore / statistics / SegmentStoreStatistics.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.internal.analysis.timing.core.segmentstore.statistics;
13
14 import org.eclipse.tracecompass.segmentstore.core.ISegment;
15
16 /**
17 * Class to calculate simple segment store statistics (min, max, average)
18 *
19 * @author Bernd Hufmann
20 */
21 public class SegmentStoreStatistics {
22 private long fMin;
23 private long fMax;
24 private long fNbSegments;
25 private double fAverage;
26 private double fVariance;
27
28 /**
29 * Constructor
30 */
31 public SegmentStoreStatistics() {
32 fMin = Long.MAX_VALUE;
33 fMax = Long.MIN_VALUE;
34 fNbSegments = 0;
35 fAverage = 0.0;
36 fVariance = 0.0;
37 }
38
39 /**
40 * Get minimum value
41 *
42 * @return minimum value
43 */
44 public long getMin() {
45 return fMin;
46 }
47
48 /**
49 * Get maximum value
50 *
51 * @return maximum value
52 */
53 public long getMax() {
54 return fMax;
55 }
56
57 /**
58 * Get number of segments analyzed
59 *
60 * @return number of segments analyzed
61 */
62 public long getNbSegments() {
63 return fNbSegments;
64 }
65
66 /**
67 * Gets the arithmetic average
68 *
69 * @return arithmetic average
70 */
71 public double getAverage() {
72 return fAverage;
73 }
74
75 /**
76 * Gets the standard deviation of the segments, uses the online algorithm
77 * shown here <a href=
78 * "https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm">
79 * Wikipedia article of dec 3 2015 </a>
80 *
81 * @return the standard deviation of the segment store, will return NaN if
82 * there are less than 3 elements
83 */
84 public double getStdDev() {
85 return fNbSegments > 2 ? Math.sqrt(fVariance / (fNbSegments - 1)) : Double.NaN;
86 }
87
88 /**
89 * Update the statistics based on a given segment
90 * <p>
91 * This is an online algorithm and must retain a complexity of O(1)
92 *
93 * @param segment
94 * the segment used for the update
95 */
96 public void update(ISegment segment) {
97 long value = segment.getLength();
98 /*
99 * Min and max are trivial, as well as number of segments
100 */
101 fMin = Math.min(fMin, value);
102 fMax = Math.max(fMax, value);
103
104 fNbSegments++;
105 /*
106 * The running mean is not trivial, see proof in javadoc.
107 */
108 double delta = value - fAverage;
109 fAverage += delta / fNbSegments;
110 fVariance += delta * (value - fAverage);
111 }
112 }
This page took 0.034471 seconds and 5 git commands to generate.