3 ms·
Here are my results with JavaScript (node.js v14.15.4) added: Language | Simple | Optimized | Notes ------------- | ------ | --------- | -----
by frankenst1 6y ago
Here are my results with JavaScript (node.js v14.15.4) added:
Language | Simple | Optimized | Notes
------------- | ------ | --------- | -----
`wc -w` | 0.17 | 0.16 | `wc` reference; optimized sets `LC_ALL=C`
`grep` | 0.53 | 0.54 | `grep` reference; optimized sets `LC_ALL=C`
Go | 0.82 | 0.29 |
C | 0.83 | 0.20 |
Rust B | 1.10 | 0.28 | also by Andrew: bonus and custom hash
JavaScript | 1.29 | | no readline
JavaScript | 1.81 | | with readline
Rust A | 1.55 | 0.30 | by Andrew Gallant
Python | 2.14 | 1.17 |
Ruby | 3.59 | |
AWK | 4.28 | 1.22 | optimized uses `mawk`
C++ | 6.68 | 0.83 | "optimized" isn't very optimized
Shell | 42.60 | 9.50 | optimized does `LC_ALL=C sort -S 2G`
Btw: is there something wrong with my shell?
//with readline
const rl = require('readline').createInterface(process.stdin);
const wordCounter = {};
rl.on('line', (line) => {
let words = line.toLowerCase().split(/\s+/).forEach(word => {
wordCounter[word] = (wordCounter[word] || 0) + 1;
});
}).on('close',() => {
let output = Object.entries(wordCounter)
.sort(([,countA],[,countB]) => countB - countA)
.map(wc => wc.join(" "))
.join("\n");
console.log(output);
});
--------------------------------------------
//without readline
const wordCounter = {};
let lastChunk = '';
process.stdin.on('data', chunk => {
let words = (lastChunk + chunk.toString().toLowerCase()).split(/\s+/);
if (!words.length) return;
lastChunk = words.pop();
for (let word of words) {
if (word) wordCounter[word] = (wordCounter[word] || 0)+1;
}
}).on('end',() => {
if (lastChunk) wordCounter[lastChunk] = (wordCounter[lastChunk] || 0)+1;
let output = Object.entries(wordCounter)
.sort(([,countA],[,countB]) => countB - countA)
.map(wc => wc.join(" "))
.join("\n");
console.log(output);
});
- deleted 6y ago[deleted]