Last active
May 22, 2023 13:17
-
-
Save xaliphostes/f7994b95cac7b55fd59ae1cf8312ee75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simulate a 1 second computation | |
*/ | |
function computeFast() { | |
console.log("Init fast") | |
return new Promise(resolve => { | |
setTimeout(function() { | |
resolve(" Fast") | |
console.log(" End of fast") | |
}, 1000) | |
}) | |
} | |
/** | |
* Simulate a 2 seconds computation | |
*/ | |
function computeSlow() { | |
console.log("Init slow") | |
return new Promise(resolve => { | |
setTimeout(function() { | |
resolve(" Slow") | |
console.log(" End of slow") | |
}, 2000) | |
}) | |
} | |
/** | |
* In sequential program, each instruction is executed and completed | |
* before the next instruction is executed. | |
* Sequential programming is the simplest and most straightforward way of | |
* programming, but it does not take advantage of concurrent or parallel execution. | |
* It does not involve executing tasks concurrently or in parallel, and therefore | |
* it does not make use of multiple computing resources to speed up execution. | |
*/ | |
async function sequentialStart() { | |
console.log('------------------- Sequential -----------------------') | |
await computeSlow() | |
await computeFast() | |
} | |
/** | |
* Concurrency refers to the ability of a program to handle multiple tasks | |
* concurrently, meaning they can start, execute, and complete in overlapping | |
* time intervals. In concurrent programming, tasks are designed to make | |
* progress independently, often using techniques such as multitasking, | |
* multithreading, or event-driven programming. Concurrency is useful for | |
* improving responsiveness and resource utilization, especially in scenarios | |
* where tasks involve waiting for external events or input/output operations. | |
*/ | |
async function concurrentStart() { | |
console.log('------------------- Concurrent -----------------------') | |
const slow = computeSlow() | |
const fast = computeFast() | |
await slow | |
await fast | |
console.log('All concurrent tasks done') | |
} | |
/** | |
* Parallelism involves executing multiple tasks simultaneously by utilizing | |
* multiple computing resources, such as multiple processors or processor cores. | |
* In parallel programming, a program is divided into smaller parts that can be | |
* executed simultaneously, with each part assigned to a separate computing resource. | |
* Parallelism aims to improve performance and speed up execution by dividing the | |
* workload and processing it in parallel. | |
*/ | |
async function parallel() { | |
console.log('------------------- Parallel -----------------------') | |
await Promise.all([ | |
computeSlow(), | |
computeFast() | |
]).then(_ => console.log('All parralel tasks done')) | |
} | |
sequentialStart() | |
setTimeout(concurrentStart, 4000) | |
setTimeout(parallel, 7000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment