Simple linecount example to benchmark io performance in Java, NodeJS & Go.
$ ./run-benchmark.sh
[ Go] Counted 12319840 lines in 2.5675134s
[ Java] Counted 12319840 in 4303 ms
[NodeJS] Counted 12319840 in 4612 ms
package main | |
import ( | |
"bufio" | |
"time" | |
"os" | |
"fmt" | |
) | |
func lineCount(filename string) int { | |
count := 0 | |
inFile, err := os.Open(filename) | |
if err != nil { | |
fmt.Println(err) | |
return count | |
} | |
defer inFile.Close() | |
scanner := bufio.NewScanner(inFile) | |
for scanner.Scan() { | |
count++ | |
} | |
return count | |
} | |
func main() { | |
var filename string | |
if len(os.Args) > 2 { | |
filename = os.Args[3] | |
} else { | |
filename = "csv/order-details-filtered.csv" | |
} | |
start := time.Now() | |
count := lineCount(filename) | |
end := time.Now() | |
duration := end.Sub(start) | |
fmt.Printf("[ Go] Counted %d lines in %s\n", count, duration.String()) | |
} |
var fs = require('fs'); | |
var i; | |
var count = 0; | |
var filename = process.argv.length < 3 ? 'csv/order-details-filtered.csv' : process.argv[2]; | |
var nl = '\n'.charCodeAt(0); | |
var start = new Date().getTime(); | |
fs.createReadStream(filename) | |
.on('data', function(chunk) { | |
for (i=0; i < chunk.length; ++i) | |
if (chunk[i] == nl) count++; | |
}) | |
.on('end', function() { | |
console.log('[NodeJS] Counted %d in %d ms', count, new Date().getTime() - start); | |
}); |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
public class LineCount { | |
public static void main(String[] args) throws Exception { | |
String filename = "csv/order-details-filtered.csv"; | |
if ( args.length > 2 ) { | |
filename = args[2]; | |
} | |
try ( BufferedReader br = new BufferedReader(new FileReader(filename)) ) { | |
int count = 0; | |
long start = System.currentTimeMillis(); | |
while ( br.readLine() != null ) { | |
count++; | |
} | |
System.out.printf("[ Java] Counted %d in %d ms\n", count, System.currentTimeMillis() - start); | |
} | |
} | |
} | |
#!/bin/sh | |
rm -f line-count.exe LineCount.class | |
go build line-count.go | |
javac LineCount.java | |
node line-count.js & | |
java LineCount & | |
./line-count.exe & | |
sleep 5 |