forked from node-ffi/node-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
41 lines (31 loc) · 1.04 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var ffi = require('../')
var ref = require('ref')
var util = require('util')
function measureIterationsOverTime(what, duration, f, granularity) {
granularity = granularity || 1000
var iterations = 0
var start = Date.now()
while (Date.now() < (start + duration)) {
for (var i = 0; i < granularity; i++) {
f()
}
iterations += granularity
}
var duration = Date.now() - start
var persec = (iterations / (Date.now() - start)) * 1000
util.log('Executed ' + what + ' ' + iterations + ' times in '
+ duration + 'ms ' + '(' + persec + '/sec)')
}
var benchLibrary = new ffi.Library(null, {
'strtoul': [ 'ulong', [ 'string', 'pointer', 'int' ] ]
})
var string = '1234567890'
var strtoulPtr = ref.NULL
var strtoulFunc = require('../test/build/Release/ffi_tests').strtoul
var blstrtoul = benchLibrary.strtoul
measureIterationsOverTime('Binding strtoul', 5000, function() {
strtoulFunc(string, strtoulPtr, 0)
})
measureIterationsOverTime('ffi strtoul', 5000, function() {
blstrtoul(string, strtoulPtr, 0)
})