diff --git a/.gitignore b/.gitignore index 4dce20b..2acc633 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ # Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +bin/* + # Logs -meow logs _.log npm-debug.log_ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bf32034 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "time.h": "c" + } +} \ No newline at end of file diff --git a/Cmatrix.c b/Cmatrix.c new file mode 100644 index 0000000..aadb51a --- /dev/null +++ b/Cmatrix.c @@ -0,0 +1,67 @@ +// For GCC and Clang + +#include +#include +#include +#include + +void parseH(char *p); +void parseL(char *p); + +void matrix_multiplication(int n) { + int **a = malloc(n * sizeof(int *)); + int **b = malloc(n * sizeof(int *)); + int **c = malloc(n * sizeof(int *)); + + for (int i = 0; i < n; i++) { + a[i] = malloc(n * sizeof(int)); + b[i] = malloc(n * sizeof(int)); + c[i] = malloc(n * sizeof(int)); + for (int j = 0; j < n; j++) { + a[i][j] = rand() % 100; + b[i][j] = rand() % 100; + c[i][j] = 0; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int k = 0; k < n; k++) { + c[i][j] += a[i][k] * b[k][j]; + } + } + } + + for (int i = 0; i < n; i++) { + free(a[i]); + free(b[i]); + free(c[i]); + } + free(a); + free(b); + free(c); +} + + +int main(int argc, char *argv[]) { + + // Start measuring time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + // END + + matrix_multiplication(1500); + + + // Stop measuring time and calculate the elapsed time + clock_gettime(CLOCK_REALTIME, &end); + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds * 1e-9; + + printf("Time measured: %.10f seconds.\n", elapsed); + // + + + return 0; +} diff --git a/README.md b/README.md deleted file mode 100644 index 058fdf8..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# BUN-test - -Test bun linux, windows ecc \ No newline at end of file diff --git a/bebbo.c b/bebbo.c new file mode 100644 index 0000000..9f33874 --- /dev/null +++ b/bebbo.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +void parseH(char *p); +void parseL(char *p); + +int main(int argc, char *argv[]) +{ + + // Start measuring time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + + // + + + + char c[] = "qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890"; + + parseH(c); + parseL(c); + //printf("%s\n", c); + + // Stop measuring time and calculate the elapsed time + clock_gettime(CLOCK_REALTIME, &end); + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds * 1e-9; + + printf("Time measured: %.10f seconds.\n", elapsed); + // + + + return 0; +} + +void parseH(char *p) +{ + + int ln = strlen(p); + + for (int a = 0; a < ln; a++) + { + if (p[a] < 123 && p[a] > 96) + { + p[a] = p[a] - 32; + } + } + + /* + + char *t ; + t=p; + while(*t!=0){ + if(*t>96 && *t<123){ + *t=*t-32; + } + t++; + } + */ +} + +void parseL(char *p) +{ + + /* + char *t ; + t=p; + while(*t!=0){ + if(*t>64 && *t<91){ + *t=*t+32; + } + t++; + } +*/ + + int ln = strlen(p); + for (int a = 0; a < ln; a++) + { + if (p[a] < 91 && p[a] > 64) + { + p[a] = p[a] + 32; + } + } +} \ No newline at end of file diff --git a/bebbow.c b/bebbow.c new file mode 100644 index 0000000..eebb6a8 --- /dev/null +++ b/bebbow.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +void parseH(char *p); +void parseL(char *p); + +int main(int argc, char *argv[]) +{ + + // Start measuring time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + + // + + + + char c[] = "qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890"; + + parseH(c); + parseL(c); + //printf("%s\n", c); + + // Stop measuring time and calculate the elapsed time + clock_gettime(CLOCK_REALTIME, &end); + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds * 1e-9; + + printf("Time measured: %.10f seconds.\n", elapsed); + // + + + return 0; +} + +void parseH(char *p) +{ + + + + char *t ; + t=p; + while(*t!=0){ + if(*t>96 && *t<123){ + *t=*t-32; + } + t++; + } + +} + +void parseL(char *p) +{ + + char *t ; + t=p; + while(*t!=0){ + if(*t>64 && *t<91){ + *t=*t+32; + } + t++; + } + +} \ No newline at end of file diff --git a/index.js b/index.js index db5618b..a84fed6 100644 --- a/index.js +++ b/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(); + diff --git a/libfibonacci.so b/libfibonacci.so deleted file mode 100755 index 525c72c..0000000 Binary files a/libfibonacci.so and /dev/null differ diff --git a/libtesticolo.so b/libtesticolo.so deleted file mode 100755 index 4f98f1a..0000000 Binary files a/libtesticolo.so and /dev/null differ diff --git a/matrix.c b/matrix.c new file mode 100644 index 0000000..87244d2 --- /dev/null +++ b/matrix.c @@ -0,0 +1,45 @@ +// for shared lib + +#include +#include +#include +#include + +int matrix(int n) { + n = 1500; + + int **a = malloc(n * sizeof(int *)); + int **b = malloc(n * sizeof(int *)); + int **c = malloc(n * sizeof(int *)); + + for (int i = 0; i < n; i++) { + a[i] = malloc(n * sizeof(int)); + b[i] = malloc(n * sizeof(int)); + c[i] = malloc(n * sizeof(int)); + for (int j = 0; j < n; j++) { + a[i][j] = rand() % 100; + b[i][j] = rand() % 100; + c[i][j] = 0; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int k = 0; k < n; k++) { + c[i][j] += a[i][k] * b[k][j]; + } + } + } + + for (int i = 0; i < n; i++) { + free(a[i]); + free(b[i]); + free(c[i]); + } + free(a); + free(b); + free(c); + + return EXIT_SUCCESS; +} + diff --git a/matrixcc.c b/matrixcc.c new file mode 100644 index 0000000..c7d748a --- /dev/null +++ b/matrixcc.c @@ -0,0 +1,45 @@ +// for TinyCC + +#include +#include +#include +#include + +int matrixcc(int n) { + n = 1500; + + int **a = malloc(n * sizeof(int *)); + int **b = malloc(n * sizeof(int *)); + int **c = malloc(n * sizeof(int *)); + + for (int i = 0; i < n; i++) { + a[i] = malloc(n * sizeof(int)); + b[i] = malloc(n * sizeof(int)); + c[i] = malloc(n * sizeof(int)); + for (int j = 0; j < n; j++) { + a[i][j] = rand() % 100; + b[i][j] = rand() % 100; + c[i][j] = 0; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + for (int k = 0; k < n; k++) { + c[i][j] += a[i][k] * b[k][j]; + } + } + } + + for (int i = 0; i < n; i++) { + free(a[i]); + free(b[i]); + free(c[i]); + } + free(a); + free(b); + free(c); + + return EXIT_SUCCESS; +} + diff --git a/meow b/meow deleted file mode 100755 index 3e0b13b..0000000 Binary files a/meow and /dev/null differ diff --git a/test.c b/test.c deleted file mode 100644 index 9deaf81..0000000 --- a/test.c +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - -int testicolo( void ) { - - char c[] = "qwertyuiopasdfghjkllzxcvbnm123456789mnbvcxzlkjhgfdsapoiuytrewq1234567890"; - - for(int a=0;a<100000;a++){ - parseH(&c); - parseL(&c); -// printf("%s",c); - } - - return 0; -} - - -void parseH(char *p){ - int ln=strlen(p); - for(int a=0; a96){ - p[a]=p[a]-32; - } - } - -} - -void parseL(char *p){ - int ln=strlen(p); - for(int a=0; a64){ - p[a]=p[a]+32; - } - } -}