Languages
[Edit]
EN

CPU Benchmark - single core speed test with Fibonacci Sequence in AssemblyScript

8 points
Created by:
Gigadude
791

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.

AssemblyScript performance test with Fibonacci Sequence, compiled to *.wasm.
AssemblyScript performance test with Fibonacci Sequence, compiled to *.wasm.

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

  1. AssemblyScript project - GitHub

See also

  1. CPU Benchmark - Raspberry PI vs Banana PI - single core speed test with Fibonacci Sequence

  2. CPU Benchmark - single core speed test with Fibonacci Sequence in JavaScript

  3. CPU Benchmark - single core speed test with Fibonacci Sequence in Java

  4. CPU Benchmark - single core speed test with Fibonacci Sequence in Dart

  5. CPU Benchmark - single core speed test with Fibonacci Sequence in Python

  6. CPU Benchmark - single core speed test with Fibonacci Sequence in PHP

References

  1. Node.js (nodejs.org) - Homepage
  2. AssemblyScript - Homepage
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join