CPU Benchmark - single core speed test with Fibonacci Sequence in AssemblyScript
In this short article, I would like to show Fibonacci Sequence test that is run on a single core. The test source code was converted from C++ to AssemblyScript - check source article.
AssemblyScript is like TypeScript with additional restrictions.
Example assembly/index.ts
file:
// The entry file of your WebAssembly module.
export function test(): i64[] {
let tmp: i64, a: i64 = 0, b: i64 = 1;
const t1: i64 = Date.now();
for (let i: i64 = 0; i < 10000; ++i) {
for (let j: i64 = 0; j < 1000000; ++j) {
tmp = b;
b = a + b;
a = tmp;
}
}
const t2: i64 = Date.now();
const dt: i64 = t2 - t1;
return [dt, b];
}
Note:
b
variable returning prevents against automatic optimization.
Example index.js
file:
const fs = require('fs');
const loader = require('@assemblyscript/loader');
const imports = { };
const path = __dirname + '/build/optimized.wasm';
const source = fs.readFileSync(path);
const instance = loader.instantiateSync(source, imports);
const pointer = instance.exports.test();
const array = instance.exports.__getInt64Array(pointer);
const time = Number(array[0]); // time in ms
const result = Number(array[1]); // b variable
console.log(`Time: ${0.001 * time}s`);
console.log(`Result: ${result}`);
Running with Node.js:
node index.js
Example output:
Time: 4.067s
Result: -1667179358074339000
Used Node.js: v16.13.0
Used OS: Windows 10 x64
Used PC:
- Ryzen 9 5900x
- DRR 4 (2x 32GB)
- Samsung SSD M.2 970 EVO (1TB)
- GeForce GTX 970 (4GB RAM)
Project configuration
Simple steps:
1. Create example project:
npm init
Hint: select other name than suggested
assemblyscript
.
2. Install dependencies:
npm install --save @assemblyscript/loader
npm install --save-dev assemblyscript
Hint: you can install globally as adminitrator AssemnblyScript using:
npm install -g assemblyscript
.
3. Configure the AssemblyScript project:
npx asinit .
4. Build *.wasm
files:
npm run asbuild
Resources
See also
-
CPU Benchmark - Raspberry PI vs Banana PI - single core speed test with Fibonacci Sequence
-
CPU Benchmark - single core speed test with Fibonacci Sequence in JavaScript
-
CPU Benchmark - single core speed test with Fibonacci Sequence in Java
-
CPU Benchmark - single core speed test with Fibonacci Sequence in Dart
-
CPU Benchmark - single core speed test with Fibonacci Sequence in Python
-
CPU Benchmark - single core speed test with Fibonacci Sequence in PHP