Skip to content

Question: What is the expected speed up when the communication is minimal? #124

Closed

Description

@Ghostblad3

Suppose you have a CPU bound task: "Count how many times you can calculate the square root in 10 secs"
When I tried a serial implementation, it was faster than 2 workers, 3 workers, 4 workers of doing the same task. Did not try with more workers.
Is this expected? I supposed I would see a linear speed up since these works do not send a message until the 10secs pass so I expected almost 0 overhead.

Activity

  1. AriPerkkio commented on Sep 27, 2025

    @AriPerkkio
    Member

    What is the expected speed up when the communication is minimal?

    It depends. Ideally <cpu-core-count> x times faster.

    Is this expected?

    On CPU with single core, yes.

    I supposed I would see a linear speed up since these works do not send a message until the 10secs pass so I expected almost 0 overhead.

    If each worker runs for 10 seconds, it will still take 10 second.

  2. Ghostblad3 commented on Sep 27, 2025

    @Ghostblad3
    Author

    I have a 12500H CPU with 4P Cores 8E Cores and Total Threads 16.

    Also I did not have more apps working that could push the system to its limits.
    I do not understand. Why the following has terrible speed up on 2 workers. 2 workers were slower than the serial operation.

    function timedCalculation() {
        const targetTime = 10000;
        const start = Date.now();
        let sum = 0;
        let operations = 0;
    
        // Busy loop until target time has passed
        while (Date.now() - start < targetTime) {
            ++operations;
            // Do some meaningless math to keep the CPU busy
            sum += Math.sqrt(Math.random() * 1000);
        }
    
        return operations;
    }
  3. AriPerkkio commented on Sep 28, 2025

    @AriPerkkio
    Member

    Can you show the full example? I'm seeing different results with following:

    // main.mjs
    
    import { availableParallelism } from 'node:os'
    import Tinypool from './dist/index.js'
    import timedCalculation from './worker.mjs'
    
    const WORKER_COUNT = availableParallelism() - 1
    const IDS = Array.from({ length: WORKER_COUNT }).keys()
    
    const main = timedCalculation()
    
    const pool = new Tinypool({
      filename: new URL('./worker.mjs', import.meta.url).href,
      maxThreads: WORKER_COUNT,
      minThreads: WORKER_COUNT,
    })
    
    const workerCounts = await Promise.all(IDS.map(() => pool.run()))
    
    const workers = workerCounts.reduce((sum, count) => sum + count, 0)
    
    console.log('main   ', main)
    console.log('workers', workers)
    // worker.mjs
    
    export default function timedCalculation() {
      const targetTime = 10_000
      const start = Date.now()
      let sum = 0
      let operations = 0
    
      // Busy loop until target time has passed
      while (Date.now() - start < targetTime) {
        ++operations
        // Do some meaningless math to keep the CPU busy
        sum += Math.sqrt(Math.random() * 1000)
      }
    
      return operations
    }
    Main thread calculated 25238502
    Starting workers after 5s cooldown
    Starting worker 1
    Starting worker 2
    Starting worker 3
    Starting worker 4
    Starting worker 5
    Starting worker 6
    Starting worker 7
    Worker 1 calculcated 17653445
    Worker 2 calculcated 17703995
    Worker 4 calculcated 17835409
    Worker 7 calculcated 18016969
    Worker 3 calculcated 17773668
    Worker 6 calculcated 17926318
    Worker 5 calculcated 17824129
    main    25238502
    workers 124733933
    

    So main did 25_238_502, workers did 124_733_933.

  4. locked and limited conversation to collaborators on Sep 28, 2025
  5. converted this issue into a discussion #125 on Sep 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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