Skip to content

Submission: Java 8 #5

Open
Open
@topriddy

Description

@topriddy
  • 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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions