Finish Tests
This commit is contained in:
parent
3315118690
commit
4992ad603e
13 changed files with 398 additions and 53 deletions
93
index.js
93
index.js
|
|
@ -1,19 +1,40 @@
|
|||
import { dlopen, FFIType } from "bun:ffi";
|
||||
import { cc, dlopen, FFIType } from "bun:ffi";
|
||||
import source from "./matrixcc.c" with { type: "file"};
|
||||
|
||||
|
||||
const {
|
||||
symbols: { matrixcc },
|
||||
} = cc({
|
||||
source,
|
||||
symbols: {
|
||||
matrixcc: {
|
||||
args: [],
|
||||
returns: "int",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Funzione JavaScript equivalente
|
||||
function jsCC() {
|
||||
|
||||
function jsTest() {
|
||||
let c = "qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890";
|
||||
let j = '';
|
||||
let h = '';
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
for (let a = 0; a < 100000 ; a++) {
|
||||
h = c.toUpperCase();
|
||||
j = h.toLowerCase();
|
||||
c = j;
|
||||
}
|
||||
matrixcc();
|
||||
|
||||
const end = performance.now();
|
||||
console.log(`TinyCC Execution Time: ${end - start}ms`);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Funzione JavaScript equivalente
|
||||
function jsTest() {
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
matrixjs();
|
||||
|
||||
const end = performance.now();
|
||||
console.log(`JavaScript Execution Time: ${end - start}ms`);
|
||||
|
|
@ -21,9 +42,9 @@ function jsTest() {
|
|||
|
||||
|
||||
// Carica la libreria C
|
||||
const path = `./libtesticolo.so`;
|
||||
const path = `./bin/matrix.so`;
|
||||
const { symbols } = dlopen(path, {
|
||||
testicolo: {
|
||||
matrix: {
|
||||
args: [], // La funzione C non prende argomenti
|
||||
returns: FFIType.int, // La funzione C restituisce un intero
|
||||
},
|
||||
|
|
@ -34,13 +55,57 @@ function cTest() {
|
|||
const start = performance.now();
|
||||
|
||||
// Chiama la funzione C per eseguire il test
|
||||
symbols.testicolo();
|
||||
symbols.matrix();
|
||||
|
||||
const end = performance.now();
|
||||
console.log(`C Execution Time: ${end - start}ms`);
|
||||
}
|
||||
|
||||
// test matrix
|
||||
function matrixjs( n = 1500) {
|
||||
|
||||
let a = [];
|
||||
let b = [];
|
||||
let c = [];
|
||||
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
a[i] = [];
|
||||
b[i] = [];
|
||||
c[i] = [];
|
||||
|
||||
for (let j = 0; j < n; j++) {
|
||||
a[i][j] = Math.floor(Math.random() * 100);
|
||||
b[i][j] = Math.floor(Math.random() * 100);
|
||||
c[i][j] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
for (let k = 0; k < n; k++) {
|
||||
c[i][j] += a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
a[i].length = 0;
|
||||
b[i].length = 0;
|
||||
c[i].length = 0;
|
||||
}
|
||||
|
||||
a.length = 0;
|
||||
b.length = 0;
|
||||
c.length = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Avvia i test
|
||||
jsCC();
|
||||
jsTest();
|
||||
cTest();
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue