Example usage for com.google.common.base Stopwatch start

List of usage examples for com.google.common.base Stopwatch start

Introduction

In this page you can find the example usage for com.google.common.base Stopwatch start.

Prototype

public Stopwatch start() 

Source Link

Document

Starts the stopwatch.

Usage

From source file:processing.ActCalculator.java

private static List<Map<Integer, Double>> startActCreation(BookmarkReader reader, int sampleSize,
        boolean sorting, boolean userBased, boolean resBased, int dVal, int beta) {
    timeString = "";
    int size = reader.getUserLines().size();
    int trainSize = size - sampleSize;

    Stopwatch timer = new Stopwatch();
    timer.start();
    ActCalculator calculator = new ActCalculator(reader, trainSize, dVal, beta, userBased, resBased);
    timer.stop();//  w  w w .ja  v a  2  s .co  m
    long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);
    List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
    if (trainSize == size) {
        trainSize = 0;
    }

    timer = new Stopwatch();
    timer.start();
    for (int i = trainSize; i < size; i++) { // the test-set
        UserData data = reader.getUserLines().get(i);
        Map<Integer, Double> map = calculator.getRankedTagList(data.getUserID(), data.getWikiID(), sorting);
        results.add(map);
    }
    timer.stop();
    long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
    timeString += ("Full training time: " + trainingTime + "\n");
    timeString += ("Full test time: " + testTime + "\n");
    timeString += ("Average test time: " + testTime / (double) sampleSize) + "\n";
    timeString += ("Total time: " + (trainingTime + testTime) + "\n");
    return results;
}

From source file:processing.FolkRankCalculator.java

private static void startFolkRankCreation(BookmarkReader reader, int sampleSize) {
    timeString = "";
    System.out.println("\nStart FolkRank Calculation for Tags");
    frResults = new ArrayList<int[]>();
    prResults = new ArrayList<int[]>();
    int size = reader.getUserLines().size();
    int trainSize = size - sampleSize;
    Stopwatch timer = new Stopwatch();
    timer.start();
    FactReader factReader = new WikipediaFactReader(reader, trainSize, 3);
    FactPreprocessor prep = new FactReaderFactPreprocessor(factReader);
    prep.process();/*from  w  w  w .  j ava 2s.co m*/
    FolkRankData facts = prep.getFolkRankData();

    FolkRankParam param = new FolkRankParam();
    FolkRankPref pref = new FolkRankPref(new double[] { 1.0, 1.0, 1.0 });
    int usrCounts = facts.getCounts()[1].length;
    System.out.println("Users: " + usrCounts);
    int resCounts = facts.getCounts()[2].length;
    System.out.println("Resources: " + resCounts);
    double[][] prefWeights = new double[][] { new double[] {}, new double[] { usrCounts },
            new double[] { resCounts } };
    FolkRankAlgorithm folk = new FolkRankAlgorithm(param);
    timer.stop();
    long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);

    timer = new Stopwatch();
    // start FolkRank        
    for (int i = trainSize; i < size; i++) {
        timer.start();
        UserData data = reader.getUserLines().get(i);
        int u = data.getUserID();
        int[] uPrefs = (u < usrCounts ? new int[] { u } : new int[] {});
        int r = data.getWikiID();
        int[] rPrefs = (r < resCounts ? new int[] { r } : new int[] {});
        pref.setPreference(new int[][] { new int[] {}, uPrefs, rPrefs }, prefWeights);
        FolkRankResult result = folk.computeFolkRank(facts, pref);

        int[] topTags = new int[10];
        SortedSet<ItemWithWeight> topKTags = ItemWithWeight.getTopK(facts, result.getWeights(), 10, 0);
        int count = 0;
        for (ItemWithWeight item : topKTags) {
            topTags[count++] = item.getItem();
        }
        frResults.add(topTags);
        timer.stop();

        int[] topTagsPr = new int[10];
        SortedSet<ItemWithWeight> topKTagsPr = ItemWithWeight.getTopK(facts, result.getAPRWeights(), 10, 0);
        count = 0;
        for (ItemWithWeight item : topKTagsPr) {
            topTagsPr[count++] = item.getItem();
        }
        prResults.add(topTagsPr);
        //System.out.println(u + "|" + data.getTags().toString().replace("[", "").replace("]", "") + 
        //                  "|" + Arrays.toString(topTags).replace("[", "").replace("]", "") + 
        //                  "|" + Arrays.toString(topTagsPr).replace("[", "").replace("]", ""));
    }
    long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
    timeString += ("Full training time: " + trainingTime + "\n");
    timeString += ("Full test time: " + testTime + "\n");
    timeString += ("Average test time: " + testTime / (double) sampleSize) + "\n";
    timeString += ("Total time: " + (trainingTime + testTime) + "\n");
}

From source file:itemrecommendations.CFResourceCalculator.java

private static List<Map<Integer, Double>> startBM25CreationForResourcesPrediction(BookmarkReader reader,
        int sampleSize, boolean userBased, boolean resBased, boolean allResources, boolean bll,
        Features features) {//from   ww w  .  java2s.  c om
    int size = reader.getBookmarks().size();
    int trainSize = size - sampleSize;

    Stopwatch timer = new Stopwatch();
    timer.start();
    CFResourceCalculator calculator = new CFResourceCalculator(reader, trainSize, false, userBased, resBased, 5,
            Similarity.COSINE, features);
    timer.stop();
    long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);

    timer.reset();
    timer.start();
    List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
    for (Integer userID : reader.getUniqueUserListFromTestSet(trainSize)) {
        Map<Integer, Double> map = null;
        map = calculator.getRankedResourcesList(userID, -1, true, allResources, bll, true, false); // TODO
        results.add(map);
    }
    timer.stop();
    long testTime = timer.elapsed(TimeUnit.MILLISECONDS);

    timeString = PerformanceMeasurement.addTimeMeasurement(timeString, true, trainingTime, testTime,
            sampleSize);
    return results;
}

From source file:com.codereligion.cherry.benchmark.BenchmarkRunner.java

public static Func1<Input, Observable<Output>> benchMark() {
    return new Func1<Input, Observable<Output>>() {
        @Override/*from  w  ww. j  a v a 2s  .  com*/
        public Observable<Output> call(final Input input) {
            final Output output = Output.from(input);

            output.withGuavaContestant(benchMark(input.getRepetitions(), input.getGuavaResult()));
            output.withCherryContestant(benchMark(input.getRepetitions(), input.getCherryResult()));

            return Observable.just(output);
        }

        private ContestantResult benchMark(final long repetitions, final Contestant contestant) {

            int checkInt = 0;
            final Stopwatch stopwatch = Stopwatch.createUnstarted();
            final ContestantResult contestantResult = ContestantResult.from(contestant);

            for (long reps = 0; reps < repetitions; reps++) {

                System.gc();
                stopwatch.start();
                checkInt |= contestant.run();
                stopwatch.stop();

                final long timeInNanos = stopwatch.elapsed(TimeUnit.NANOSECONDS);
                contestantResult.addRunTime(timeInNanos);
                stopwatch.reset();
            }

            System.out.println("check int:" + checkInt);

            return contestantResult;
        }
    };
}

From source file:org.apache.beam.sdk.io.synthetic.delay.SyntheticDelay.java

/** Keep cpu busy for {@code delayMillis} by calculating lots of hashes. */
private static void cpuDelay(long delayMillis) {
    // Note that the delay is enforced in terms of walltime. That implies this thread may not
    // keep CPU busy if it gets preempted by other threads. There is more of chance of this
    // occurring in a streaming pipeline as there could be lots of threads running this. The loop
    // measures cpu time spent for each iteration, so that these effects are some what minimized.

    long cpuMicros = delayMillis * 1000;
    Stopwatch timer = Stopwatch.createUnstarted();

    while (timer.elapsed(TimeUnit.MICROSECONDS) < cpuMicros) {
        // Find a long which hashes to HASH in lowest MASK bits.
        // Values chosen to roughly take 1ms on typical workstation.
        timer.start();
        long p = INIT_PLAINTEXT;
        while (true) {
            long t = Hashing.murmur3_128().hashLong(p).asLong();
            if ((t & MASK) == (HASH & MASK)) {
                break;
            }/*from   w ww  .  jav a 2 s  .co m*/
            p++;
        }
        timer.stop();
    }
}

From source file:processing.BM25Calculator.java

private static List<Map<Integer, Double>> startBM25CreationForTagPrediction(BookmarkReader reader,
        int sampleSize, boolean userBased, boolean resBased, int beta) {
    timeString = "";
    int size = reader.getUserLines().size();
    int trainSize = size - sampleSize;
    Stopwatch timer = new Stopwatch();
    timer.start();
    BM25Calculator calculator = new BM25Calculator(reader, trainSize, true, userBased, resBased, beta);
    timer.stop();/*from   www .  j  a  v  a 2 s . c om*/
    long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);

    List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
    timer = new Stopwatch();
    timer.start();
    for (int i = trainSize; i < size; i++) {
        UserData data = reader.getUserLines().get(i);
        Map<Integer, Double> map = null;
        map = calculator.getRankedTagList(data.getUserID(), data.getWikiID(), true);
        results.add(map);
        //System.out.println(data.getTags() + "|" + map.keySet());
    }
    timer.stop();
    long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
    timeString += ("Full training time: " + trainingTime + "\n");
    timeString += ("Full test time: " + testTime + "\n");
    timeString += ("Average test time: " + testTime / (double) sampleSize) + "\n";
    timeString += ("Total time: " + (trainingTime + testTime) + "\n");

    return results;
}

From source file:processing.BLLCalculator.java

private static List<Map<Integer, Double>> startActCreation(BookmarkReader reader, int sampleSize,
        boolean sorting, boolean userBased, boolean resBased, double dVal, int beta, CalculationType cType,
        Double lambda) {/*from w  w  w .jav a 2  s  .  co  m*/
    int size = reader.getBookmarks().size();
    int trainSize = size - sampleSize;

    Stopwatch timer = new Stopwatch();
    timer.start();
    BLLCalculator calculator = new BLLCalculator(reader, trainSize, dVal, beta, userBased, resBased, cType,
            lambda);
    timer.stop();
    long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);
    List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
    if (trainSize == size) {
        trainSize = 0;
    }

    timer.reset();
    timer.start();
    for (int i = trainSize; i < size; i++) { // the test-set
        Bookmark data = reader.getBookmarks().get(i);
        Map<Integer, Double> map = calculator.getRankedTagList(data.getUserID(), data.getResourceID(), sorting,
                cType);
        results.add(map);
    }
    timer.stop();
    long testTime = timer.elapsed(TimeUnit.MILLISECONDS);

    timeString = PerformanceMeasurement.addTimeMeasurement(timeString, true, trainingTime, testTime,
            sampleSize);
    return results;
}

From source file:com.android.build.gradle.shrinker.AbstractShrinker.java

public static void logTime(String section, Stopwatch stopwatch) {
    if (System.getProperty("android.newShrinker.profile") != null) {
        System.out.println(section + ": " + stopwatch);
        stopwatch.reset();/*from   w  ww . j a va2 s .  c om*/
        stopwatch.start();
    }
}

From source file:processing.MalletCalculator.java

public static List<Map<Integer, Double>> startLdaCreation(BookmarkReader reader, int sampleSize,
        boolean sorting, int numTopics, boolean userBased, boolean resBased, boolean topicCreation,
        boolean smoothing) {
    timeString = "";
    int size = reader.getUserLines().size();
    int trainSize = size - sampleSize;

    Stopwatch timer = new Stopwatch();
    timer.start();
    MalletCalculator userCalc = null;/*w  w w . j a v  a 2  s.c  om*/
    List<Map<Integer, Integer>> userMaps = null;
    //List<Double> userDenoms = null;
    if (userBased) {
        userMaps = Utilities.getUserMaps(reader.getUserLines().subList(0, trainSize));
        userCalc = new MalletCalculator(userMaps, numTopics);
        userCalc.predictValuesProbs();
        //userDenoms = getDenoms(userPredictionValues);
        System.out.println("User-Training finished");
    }
    MalletCalculator resCalc = null;
    List<Map<Integer, Integer>> resMaps = null;
    //List<Double> resDenoms = null;
    if (resBased) {
        resMaps = Utilities.getResMaps(reader.getUserLines().subList(0, trainSize));
        resCalc = new MalletCalculator(resMaps, numTopics);
        resCalc.predictValuesProbs();
        //resDenoms = getDenoms(resPredictionValues);
        System.out.println("Res-Training finished");
    }
    List<Map<Integer, Double>> results = new ArrayList<Map<Integer, Double>>();
    if (trainSize == size) {
        trainSize = 0;
    }
    timer.stop();
    long trainingTime = timer.elapsed(TimeUnit.MILLISECONDS);

    timer = new Stopwatch();
    timer.start();
    for (int i = trainSize; i < size; i++) { // the test set
        UserData data = reader.getUserLines().get(i);
        int userID = data.getUserID();
        int resID = data.getWikiID();
        //Map<Integer, Integer> userMap = null;
        //if (userBased && userMaps != null && userID < userMaps.size()) {
        //   userMap = userMaps.get(userID);
        //}
        //Map<Integer, Integer> resMap = null;
        //if (resBased && resMaps != null && resID < resMaps.size()) {
        //   resMap = resMaps.get(resID);
        //}
        double userTagCount = 0.0;//Utilities.getMapCount(userMap);
        double resTagCount = 0.0;//Utilities.getMapCount(resMap);
        /*
        double userDenomVal = 0.0;
        if (userDenoms != null && userID < userDenoms.size()) {
           userDenomVal = userDenoms.get(userID);
        }
        double resDenomVal = 0.0;
        if (resDenoms != null && resID < resDenoms.size()) {
           resDenomVal = resDenoms.get(resID);
        }
        */
        Map<Integer, Double> userPredMap = null;
        if (userCalc != null) {
            userPredMap = userCalc.getValueProbsForID(userID, topicCreation);
        }
        Map<Integer, Double> resPredMap = null;
        if (resCalc != null) {
            resPredMap = resCalc.getValueProbsForID(resID, topicCreation);
        }
        Map<Integer, Double> map = getRankedTagList(reader, userPredMap, userTagCount, resPredMap, resTagCount,
                sorting, smoothing, topicCreation);
        results.add(map);
    }
    timer.stop();
    long testTime = timer.elapsed(TimeUnit.MILLISECONDS);
    timeString += ("Full training time: " + trainingTime + "\n");
    timeString += ("Full test time: " + testTime + "\n");
    timeString += ("Average test time: " + testTime / (double) sampleSize) + "\n";
    timeString += ("Total time: " + (trainingTime + testTime) + "\n");
    return results;
}

From source file:matching.naive.BranchAndBoundMatching.java

/**
 * Tiny tests / benchmarks./*ww  w  .  j  av a  2  s.com*/
 */
public static void _main(final String[] args) {

    /*
    final double[][] matrix =
    {
        { 0, 1, 2, 3 },
        { 1, 0, 10, 20 },
        { 2, 10, 0, 10 },
        { 3, 20, 10, 0 },
    };
    */

    final double[][] matrix = {
            //     A  B  C  D
            /*A*/{ 0, 1, 5, 10 }, /*B*/{ 1, 0, 1, 1 }, /*C*/{ 5, 1, 0, 1 }, /*D*/{ 10, 1, 1, 0 } };
    // TODO ? normaliser la matrice entre 0 et 1 ?
    /*
    final double[][] matrix =
    {
        { 0, 1, 1, 1, 1, 1 },
        { 1, 0, 1, 1, 1, 1 },
        { 1, 1, 0, 1, 1, 1 },
        { 1, 1, 1, 0, 1, 1 },
        { 1, 1, 1, 1, 0, 1 },
        { 1, 1, 1, 1, 1, 0 },
    };
    */

    /*
    final double[][] matrix =
    {
        { 0, 10, 1, 1, 1, 1, 1, 1 },
        { 10, 0, 1, 1, 1, 1, 1, 1 },
        { 1, 1, 0, 1, 1, 1, 1, 1 },
        { 1, 1, 1, 0, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 0, 1, 1, 1 },
        { 1, 1, 1, 1, 1, 0, 1, 1 },
        { 1, 1, 1, 1, 1, 1, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 0 },
    };
    */

    final BranchAndBoundMatching matching = new BranchAndBoundMatching(matrix);
    final List<String> labels = Lists.newArrayList("A", "B", "C", "D", "E", "F");

    final Function<Position, String> mapping = new Function<Position, String>() {

        @Override
        public String apply(final Position position) {
            return labels.get(position.getRowIndex()) + labels.get(position.getColumnIndex());
        }

    };

    final Stopwatch stopwatch = new Stopwatch();

    stopwatch.start();
    //final Match firstMatch = matching.match(Extremum.MIN);
    //System.out.println(firstMatch);
    //System.out.println(firstMatch.apply(mapping));
    final Iterable<Match> matches = matching.matchAll();
    stopwatch.stop();
    for (final Match match : matches)
        System.out.println(match.apply(mapping));
    System.out.println(stopwatch.elapsedTime(TimeUnit.NANOSECONDS) + " " + TimeUnit.NANOSECONDS.toString());

}