Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission: JavaScript (Multi-Process) #3

Open
mykeels opened this issue Sep 12, 2018 · 0 comments
Open

Submission: JavaScript (Multi-Process) #3

mykeels opened this issue Sep 12, 2018 · 0 comments

Comments

@mykeels
Copy link
Collaborator

mykeels commented Sep 12, 2018

  • Time ~2991.455ms
  • Hardware:
      Processor Name: Intel Core i5
      Processor Speed: 2.3 GHz
      Number of Processors: 1
      Total Number of Cores: 2
      L2 Cache (per Core): 256 KB
      L3 Cache: 4 MB
      Memory: 16 GB
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const basePath = '<path-to-files-directory>'
const workerFarm = require('worker-farm')
let worker = workerFarm(require.resolve('./worker.js'))

const getFiles = function* () {
    for (let dir of fs.readdirSync(basePath)) {
        const dirPath = path.join(basePath, dir)
        yield (() => new Promise((resolve, reject) => {
            fs.readdir(dirPath, function (err, files) {
                if (err) {
                    reject (err)
                }
                else {
                    resolve (files.map(file => path.join(dirPath, file)))
                }
            })
        }))()
    }
}

let total = 0
let count = 0

console.time('total')
for (let promise of getFiles()) {
    promise
        .then(files => {
            files.map(filename => {
                worker(filename, (err, sum) => {
                    if (err) {
                        throw err
                    }
                    else {
                        total += sum
                        count++;
                        if (count === 1000) {
                            console.log(total)
                            console.timeEnd('total')
                            process.exit(0)
                        }
                    }
                })
            })
        })
        .catch(err => console.error(err))
}
  • Worker Script
const fs = require('fs')
const readline = require('readline')

module.exports = function (filename, cb) {
    try {
        const lineReader = readline.createInterface({
            input: fs.createReadStream(filename)
        })
        
        let fileSum = 0

        lineReader.on('line', line => {
            let lineSum = 0
            let current = 0
            for(let i = 0; i < line.length; i++) {
                let char = line[i]
                if (char != ',' ) {
                    if (current === 0) {
                        current = +char
                    }
                    else {
                        current = (current * 10) + (+char)
                    }
                }
                if ((char === ',') || (i === (line.length - 1))) {
                    lineSum += current
                    current = 0
                }
            }
            fileSum += lineSum
        })

        lineReader.on('close', () => {
            cb(null, fileSum)
        })
    }
    catch (err) {
        cb(err)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant