Giter Site home page Giter Site logo

baggepinnen / prioritychannels.jl Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 15 KB

Provides a Channel where elements are associated with a priority. take! returns highest priority item.

License: MIT License

Julia 100.00%
parallel-programming priority channels priority-queue priority-scheduling priority-list

prioritychannels.jl's Introduction

PriorityChannels

Build Status

This package provides the type PriorityChannel (the only exported name) that mimics Base.Channel, but where each element is associated with a priority. take! always returns the highest priority element. Internally, a heap is used to keep track of priorities. Example usage:

using PriorityChannels, Test
c  = Channel(50)
pc = PriorityChannel(50)
for i = 1:50
    e = rand(1:500)
    put!(c,e)
    put!(pc,e,e) # Assign same priority as element for testing purposes
end
elems = [take!(c) for i = 1:50]
pelems = [take!(pc) for i = 1:50]
@test !issorted(elems) # A regular Channel does not return ordered elements
@test issorted(pelems) # A PriorityChannel returns elements in priority order

Difference between Channel and PriorityChannel

  • put!(pc, element, priority::Real) lower number indicates a higher priority (default = 0).
  • PriorityChannel can not be unbuffered (of length 0) and must have a positive length.
  • take!(pc) returns the highest priority item, PriorityChannel thus acts like a priority queue instead of a FIFO queue like Channel does
  • Pretty much all other functionality should be the same, including all constructors.

Performance

To get maximum performance, initialize a concretely typed PriorityChannel. The constructor PriorityChannel(N) creates a channel of length N that holds type Any and have integer priorities. These types can be specified with the constructor PriorityChannel{ElemType,PrioType}(N), e.g., PriorityChannel{Int,Int}(N). There is a rather striking difference in performance between these two:

using PriorityChannels
N = 1_000_000
r = rand(1:1000, N);
const c1 = PriorityChannel(N)
const c2 = PriorityChannel{Int,Int}(N)

@time map(ri->put!(c1,ri,ri), r);
@time map(ri->put!(c2,ri,ri), r);

@time map(i->take!(c1), 1:N);
@time map(i->take!(c2), 1:N);

# Output after pre-compilation
julia> @time map(ri->put!(c1,ri,ri), r);
  0.663640 seconds (4.33 M allocations: 150.086 MiB, 55.92% gc time)

julia> @time map(ri->put!(c2,ri,ri), r);
  0.103298 seconds (60.23 k allocations: 12.643 MiB)

julia> @time map(i->take!(c1), 1:N);
  3.282501 seconds (20.02 M allocations: 612.583 MiB, 27.25% gc time)

julia> @time map(i->take!(c2), 1:N);
  0.313285 seconds (63.44 k allocations: 10.791 MiB, 4.67% gc time)

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.