Giter Club home page Giter Club logo

merge-intervals-go's Introduction

Merge Intervals

This package merges overlapping number intervals.

Usage

go run . <input intervals>

Larger datasets of intervals to merge can be provided via file and env var:

INTERVALS_FILE=<filename> go run .

Example

The input [25,30] [2,19] [14,23] [4,8] should yield the output [2,23] [25,30].

# Input
go run . "[25,30] [2,19] [14,23] [4,8]"

# Output
[2,23] [25,30]

Performance and Footprint

This package includes benchmark tests that show how different parts behave with different loads (input parsing, interval merge).

Sample output of go test -bench=. -run=XXX -benchmem:

pkg: github.com/LucaBernstein/merge-intervals-go
BenchmarkParseInputArgs-12           1230621        965.1 ns/op    1136 B/op    21 allocs/op
BenchmarkMerge-12                    9905893        120.6 ns/op     312 B/op     6 allocs/op
BenchmarkMergeOverlap1000-12         1868359        643.1 ns/op      88 B/op     3 allocs/op
BenchmarkMergeOverlap10000-12         199348       5978.0 ns/op      88 B/op     3 allocs/op
BenchmarkMergeOverlap100000-12         20149      60515.0 ns/op      88 B/op     3 allocs/op
BenchmarkMergeOverlap1000000-12         1995     613224.0 ns/op      88 B/op     3 allocs/op
BenchmarkMergeDistinct1000-12        1847392        643.8 ns/op      88 B/op     3 allocs/op
BenchmarkMergeDistinct10000-12        201364       6022.0 ns/op      88 B/op     3 allocs/op
BenchmarkMergeDistinct100000-12        19869      59441.0 ns/op      88 B/op     3 allocs/op
BenchmarkMergeDistinct1000000-12        1990     602862.0 ns/op      88 B/op     3 allocs/op
BenchmarkMergeDistinct10000000-12        196    6131859.0 ns/op      88 B/op     3 allocs/op
PASS
ok      github.com/LucaBernstein/merge-intervals-go     17.673s

In the current implementation, the calculations are executed in a single thread only. Hence, for a larger amount of intervals provided, the runtime increases linearly. The memory footprint also increases linearly for storing the intervals internally.

For example, during the BenchmarkMergeDistinct1000000 benchmark run, a memory allocation of around 40 MB during execution has been observed. Likewise, during the BenchmarkMergeDistinct10000000 benchmark run, the memory allocation observed has been around 400 MB.

Considerations and Assumptions

  • Order of intervals passed can be unordered, but interval definition itself is ordered ([ <start> , <end> ] with start <= end)
  • Interval start and end must be whole numbers (โ„ค)

merge-intervals-go's People

Contributors

lucabernstein avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.