Coder Social home page Coder Social logo

parallelloop's Introduction

Tests codebeat badge

ParallelLoop πŸ’ž

Parallel + functional operations in swift

Features:

  • πŸ‘―β€β™‚οΈ Process data in parallel over many cpu-cores and awaits
  • πŸ’œ Functional operations you already know and love
  • βš›οΈ Thread safe values across cpu-cores with AtomicValue
  • ⏩ Easily stride big data-sets with the array divide operation
  • 🎚Toggle concurrency on / off

Examples:

// Parallel map
let result = [0, 1, 2, 3].concurrentMap { i in
   i * 2
}
print(result) // 0, 2, 4, 6

// Parallel forEach
[1, 2, 3, 4].concurrentForEach {
   print($0) // 1,2,3,4
}

// Parallel compactMap
let array = [0, 1, nil, 3].concurrentCompactMap { i in
   i * 2
}
print(array) // 0, 2, 6

// Parallel reduce
let str: String = [0, 1, 2].concurrentReduce("") {
   $0 + "\( $1)"
} // "012"
print(str)

// Atomic value:
let x: Atomic<Int> = .init(0) // can be written and read across cores and threads
DispatchQueue.concurrentPerform(iterations: 1000) { y in
   x.mutate { $0 += 1 }
}
print(x.value) // 1000

// Stride concurrent operations on big data sets
// We stride to utlize cores better
// The cost of managing threads out way the benefit on big data sets
let batches = Array(0..<1000).divideBy(by: 20) // try different amounts
batches.concurrentForEach { batch in // one batch at the time (50 times), avoids cpu admin overhead
   batch.forEach { $0 } // only assigns 20 operations at the time
} // Use .flatMap { $0 } if you need to flatten the result etc

// or even easier:
// The batches method also ensures a good distribution for big and small data sets
// great when the data-set count varies
Array(0..<1000).batches(spread: 20).concurrentForEach { batch in
   batch.forEach { $0 }
}

// Another example using flatMap:
let values: [Int] = Array(0..<1000).batches(spread: 20).concurrentFlatMap { batch in
   batch.map { $0 }
}

Installation:

  • Swift packag manager: .package(url: "https://github.com/passbook/ParallelLoop.git", .branch("master"))
  • XCode package-manager: search for ParallelLoop

parallelloop's People

Contributors

eonist avatar

Stargazers

Mindula Dilthushan Manamperi avatar yangfengzzz avatar  avatar Nick Beattie avatar Ben John avatar  avatar

Watchers

James Cloos 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.