diff --git a/.gitignore b/.gitignore index 9b1ee42..4dce20b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # Logs +meow logs _.log npm-debug.log_ diff --git a/index.js b/index.js index 55b32e9..8f77754 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,44 @@ import { dlopen, FFIType } from "bun:ffi"; -const path = `./libtesticolo.so`; +// Funzione JavaScript equivalente +function jsTest() { + let c = "qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890"; + let j = ''; + let h = ''; + + const start = performance.now(); -// Carica la libreria + for (let a = 0; a < 1000; a++) { + h = c.toUpperCase(); + j = h.toLowerCase(); + c = j; + } + + const end = performance.now(); + console.log(`JavaScript Execution Time: ${end - start}ms`); +} + +// Carica la libreria C +const path = `./libtesticolo.so`; const { symbols } = dlopen(path, { testicolo: { - args: [FFIType.pointer, FFIType.pointer], // Usato pointer per i buffer - returns: FFIType.void, + args: [], // La funzione C non prende argomenti + returns: FFIType.int, // La funzione C restituisce un intero }, }); -const input = "qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890"; +// Funzione per il test in C +function cTest() { + const start = performance.now(); -// Converti la stringa in un buffer (Uint8Array) -const inputBuffer = new TextEncoder().encode(input); + // Chiama la funzione C per eseguire il test + symbols.testicolo(); -// Crea un buffer di output della stessa dimensione -const outputBuffer = new ArrayBuffer(inputBuffer.length); + const end = performance.now(); + console.log(`C Execution Time: ${end - start}ms`); +} -// Passa i buffer alla funzione C -symbols.testicolo(inputBuffer, outputBuffer); - -// Decodifica l'output dal buffer -const result = new TextDecoder().decode(new Uint8Array(outputBuffer)); -console.log(result); +// Avvia i test +jsTest(); +cTest();