Skip to content

Class MedianExperimentor

Ori Roth edited this page Apr 2, 2017 · 3 revisions

Synopsis of Class MedianExperimentor

public class MedianExperimentor { 
    /*
     * Forge (1)
     */
        MedianExperimentor(); 
    /*
     * Utilities (4)
     */
        static void main(String[] args) throws IOException; 
        static final int MAX_N; 
        static void addPowers(Set<Integer> ns, int base); 
        static int pow(int b, int e); 
} 

Input types: Set.

Exception types: IOException.

Code

// SSDLPedia
package il.ac.technion.cs.cs236700.median;

import static il.ac.technion.cs.ssdl.utils.DBC.nonnull;
import static il.ac.technion.cs.ssdl.utils.DBC.positive;
import il.ac.technion.cs.ssdl.csv.CSVLine;
import il.ac.technion.cs.ssdl.csv.CSVWriter;
import il.ac.technion.cs.ssdl.stereotypes.Application;
import il.ac.technion.cs.ssdl.utils.Permutation;
import il.ac.technion.cs.ssdl.utils.Statistics;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Application public class MedianExperimentor {
    public static void main(final String[] args) throws IOException {
        final CSVWriter w = new CSVWriter("median.csv");
        for (final int n : selectRange()) {
            System.out.println("Computing median of arrays sized " + n);
            for (final ApproximateMedian a : ApproximateMedian.$$$)
                w.writeflush(exerciseManyMedian(n, a));
        }
        w.close();
    }
    
    static final int REPETITIONS = 100;
    
    private static CSVLine exerciseManyMedian(final int n, final ApproximateMedian a) {
        positive(n);
        nonnull(a);
        final Statistics s = new Statistics();
        for (int i = 0; i < REPETITIONS; i++)
            s.record(exerciseOneMedian(n, a));
        final CSVLine $ = new CSVLine.Ordered();
        $.put("N", n);
        $.put("EXPERIMENTS", s.n());
        $.put("ALGORITHM", a.getClass().getSimpleName());
        $.put("AVERAGE_MEDIAN", s.mean());
        $.put("AVERAGE RELATIVE MEDIAN", s.mean() / n);
        $.put("SD", s.sd());
        $.put("RELATIVE SD", s.sd() / n);
        $.put("RELATIVE LOG SD", log(s.sd(), n));
        return $;
    }
    
    private static double log(final double b, final double d) {
        positive(b);
        positive(d);
        return Math.log(d) / Math.log(b);
    }
    
    private static int exerciseOneMedian(final int n, final ApproximateMedian a) {
        return a.value(Permutation.random(n));
    }
    
    public static final int MAX_N = pow(10, 6);
    
    private static int[] selectRange() {
        final Set<Integer> $ = new HashSet<Integer>();
        final int[] bases = new int[] { 2, 3, 5, 7 };
        for (final int b : bases)
            addPowers($, b);
        return toArray($);
    }
    
    private static int[] toArray(final Set<Integer> is) {
        final int[] $ = new int[is.size()];
        int i = 0;
        for (final Integer v : is)
            $[i++] = v.intValue();
        Arrays.sort($);
        return $;
    }
    
    public static void addPowers(final Set<Integer> ns, final int base) {
        for (int i = 0;; i++) {
            final int n = pow(base, i);
            if (n > MAX_N)
                return;
            ns.add(new Integer(n));
        }
    }
    
    public static int pow(final int b, final int e) {
        if (e == 0)
            return 1;
        final int $ = pow(b * b, e / 2);
        return e % 2 == 0 ? $ : $ * b;
    }
}

Metrics

Metric Value Acronym Explanation
LOC 93 Lines Of Code Total number of lines in the code
SCC 55 SemiColons Count Total number of semicolon tokens found in the code.
NOT 755 Number Of Tokens Comments, whitespace and text which cannot be made into a token not included.
VCC 2180 Visible Characters Count The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC 2107 Code Characters Count Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC 74 Unique Identifiers Count The number of different identifiers found in the code
WHC 4 Weighted Horizontal Complexity A heuritistic on horizontal complexity

Statistics

Statistic Value
Average token length 2.8
Tokens/line 8.1
Visible characters/line 23
Code characters/line 23
Semicolons/tokens 7%
Comment text percentage 3%

Tokens by Kind

Token Kind Occurrences
KEYWORD 112
OPERATOR 38
LITERAL 25
ID 245
PUNCTUATION 335
COMMENT 1
OTHER 306
Clone this wiki locally