Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission: Java 8 #5

Open
topriddy opened this issue Sep 13, 2018 · 0 comments
Open

Submission: Java 8 #5

topriddy opened this issue Sep 13, 2018 · 0 comments

Comments

@topriddy
Copy link

  • Time ~ 1.4 seconds
  • Hardware

Processor Name: Intel Core i7
Processor Speed: 2.9GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256KB
L3 Cache: 8MB
Memory: 16GB

  • External Dependencies

    • JMH (Java Microbenchmark Harness) - used for benchmarking
  • Program

    • SumNumbersProgram.java - can be run standalone, replace pathName
    • BenchmarkSumNumbersProgram.java - runs the SumNumbersProgram using JMH.
  • Results from JMH

Result "com.topriddy.devslack.sumnumberschallenge.BenchmarkSumNumbersProgram.init":
  1.403 ±(99.9%) 0.043 s/op [Average]
  (min, avg, max) = (1.334, 1.403, 1.564), stdev = 0.050
  CI (99.9%): [1.360, 1.446] (assumes normal distribution)


# Run complete. Total time: 00:02:03

Benchmark                        Mode  Cnt  Score   Error  Units
BenchmarkSumNumbersProgram.init  avgt   20  1.403 ± 0.043   s/op
package com.topriddy.devslack.sumnumberschallenge;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.function.Function.identity;

public class SumNumbersProgram {
    private final static String pathName = "/Users/topriddy/dev/sum-files-challenge/files";

    public void sumFiles(String dataPath) throws Exception {
        System.out.println("Starting...");
        Long startTime = System.currentTimeMillis();

        List<Path> files = Files.walk(Paths.get(dataPath))
                .filter(p -> !Files.isDirectory(p))
                .collect(Collectors.toList());

        Long sum = files.parallelStream()
                .map(path -> {
                    try {
                        return Files.lines(path);
                    } catch (IOException ioex) {
                        ioex.printStackTrace();
                        return Stream.empty();
                    }
                })
                .map(lines -> lines.map(line -> asList(((String) line).split(",")).parallelStream())
                        .flatMap(identity())
                        .map(value -> Long.valueOf(value))
                )
                .flatMap(identity())
                .mapToLong(v -> v).sum();

        Long endTime = System.currentTimeMillis();

        System.out.println("Sum of numbers in file is : " + sum);
        System.out.printf("\nDuration: %f seconds", (endTime - startTime) / 1000.0);
        System.out.println("\nEnd");
    }

    public static void main(String args[]) throws Exception {
        SumNumbersProgram program = new SumNumbersProgram();
        program.sumFiles(pathName);
        // run second time to gain JVM warm up benefit
        program.sumFiles(pathName);
    }
}
package com.topriddy.devslack.sumnumberschallenge;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;

public class BenchmarkSumNumbersProgram {
    private final static String pathName = "/Users/topriddy/dev/sum-files-challenge/files";

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @Fork(value = 1, warmups = 1)
    public void init() throws Exception {
        SumNumbersProgram sumNumbersProgram = new SumNumbersProgram();
        sumNumbersProgram.sumFiles(pathName);
    }

    public static void main(String args[]) throws Exception {
        org.openjdk.jmh.Main.main(args);
    }
}
@topriddy topriddy changed the title Submission: Java Submission: Java 8 Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant