From 5271207c752fd8c7713adce8fc6bb82140f0753d Mon Sep 17 00:00:00 2001 From: Zffu Date: Fri, 17 Jan 2025 20:17:51 +0100 Subject: [PATCH] fix: benchmarks --- benchmarks/main.c | 116 ++++++++++++++++++++++++++------------------- src/lexer/lexer.c | 3 +- src/lexer/lexer.h | 42 +++++++++++++++- src/lexer/tokens.h | 43 ----------------- 4 files changed, 107 insertions(+), 97 deletions(-) delete mode 100644 src/lexer/tokens.h diff --git a/benchmarks/main.c b/benchmarks/main.c index 0de2352..0a31a77 100644 --- a/benchmarks/main.c +++ b/benchmarks/main.c @@ -8,10 +8,17 @@ #include #include -#include "../src/lexer/lexer.h" +#include "../src/compiler/compiler.h" +#include "../src/compiler/structs.h" +#include "../src/compiler/pe/pe.h" + +#include "../src/ir/ir.h" +#include "../src/ir/instructions.h" +#include "../src/ir/structs.h" + #include "../src/parser/parser.h" +#include "../src/parser/structs/tree.h" #include "../src/parser/ast.h" -#include "../src/compiler/compiler.h" #include "../src/utils/logging.c" // Benchmark Settings @@ -91,18 +98,18 @@ void main(int argc, char* argv[]) { runs = atoi(argv[2]); } - char* c[5] = {"File IO (Open)", "Lexer", "Parser", "Compiler", "File IO (Close)"}; + char* c[6] = {"File IO (Open)", "Lexer", "AST", "IR", "Compiler", "Artifact Generator"}; categories = c; - for(int i = 0; i < 5; ++i) { - stats[i].total = 0; - stats[i].max = 0; - stats[i].low = 1000000; - stats[i].runs = malloc(sizeof(double) * runs); + for(int i = 0; i < 6; ++i) { + stats[i].total = 0; + stats[i].max = 0; + stats[i].low = 1000000; + stats[i].runs = malloc(sizeof(double) * runs); - for(int ii = 0; ii < runs; ++ii) { - stats[i].runs[ii] = 0; - } + for(int ii = 0; ii < runs; ++ii) { + stats[i].runs[ii] = 0; + } } for(int i = 0; i < runs; ++i) { @@ -117,7 +124,7 @@ void main(int argc, char* argv[]) { char* buff = (char*)malloc(size + 1); fread(buff, 1, size, fptr); - buff[size] = '\0'; + buff[size] = '\0'; fclose(fptr); endTimer(i, 0); @@ -125,68 +132,77 @@ void main(int argc, char* argv[]) { LEXER_RESULT result = runLexer(buff, size); - free(buff); + free(buff); endTimer(i, 1); startTimer(); - AST_NODE* node = parseNodes(result, 0, AST_ROOT); + void* node = parseRoot(result, 0, AST_TYPE_ROOT); endTimer(i, 2); startTimer(); - fptr = fopen("output.txt", "w"); + IR_OUTPUT* out = parseIR((AST_TREE_BRANCH*)node); - IR_CTX* ctx = makeContext(node); - compile(ctx, fptr); + endTimer(i, 3); + startTimer(); - fclose(fptr); + BYTECODE_BUFFER* b = compile(out); - endTimer(i, 3); + endTimer(i, 4); + startTimer(); + + fptr = fopen("output.exe", "w"); + compilePE(fptr, b); + + fclose(fptr); + + endTimer(i, 5); } printf("========= Benchmarking Results =========\n"); printf("Total time taken: %.3f micros, Average time per run: %.3f\n micros\n\n", totalTimeTaken, totalTimeTaken / runs); - for(int i = 0; i < 5; ++i) { - printf("Benchmarking Results of %s:\n", categories[i]); - printf(" Total time duration: %s%.2fus%s (%s%.1f%%%s over total running time)\n", TEXT_HCYAN, stats[i].total, RESET, TEXT_CYAN, (stats[i].total / totalTimeTaken) * 100, RESET); - printf(" Average: %s%.2fus%s\n", TEXT_HCYAN, stats[i].total / runs, RESET); - printf(" Range (%sFastest%s, %sLowest%s): %s%0.fus%s ... %s%.2fus%s\n\n", TEXT_HGREEN, RESET, TEXT_HRED, RESET, TEXT_HGREEN, stats[i].low, RESET, TEXT_HRED, stats[i].max, RESET); + + for(int i = 0; i < 6; ++i) { + printf("Benchmarking Results of %s:\n", categories[i]); + printf(" Total time duration: %s%.2fus%s (%s%.1f%%%s over total running time)\n", TEXT_HCYAN, stats[i].total, RESET, TEXT_CYAN, (stats[i].total / totalTimeTaken) * 100, RESET); + printf(" Average: %s%.2fus%s\n", TEXT_HCYAN, stats[i].total / runs, RESET); + printf(" Range (%sFastest%s, %sLowest%s): %s%0.fus%s ... %s%.2fus%s\n\n", TEXT_HGREEN, RESET, TEXT_HRED, RESET, TEXT_HGREEN, stats[i].low, RESET, TEXT_HRED, stats[i].max, RESET); - double averages[10]; - double highestAverage = 0; + double averages[10]; + double highestAverage = 0; - for(int ii = 0; ii < 10; ++ii) { - averages[ii] = 0; - } + for(int ii = 0; ii < 10; ++ii) { + averages[ii] = 0; + } - for(int ii = 0; ii < runs; ++ii) { - averages[ii / 10] += stats[i].runs[ii] / 10; - } + for(int ii = 0; ii < runs; ++ii) { + averages[ii / 10] += stats[i].runs[ii] / 10; + } - for(int ii = 0; ii < 10; ++ii) { - if(averages[ii] > highestAverage) highestAverage = averages[ii]; - } + for(int ii = 0; ii < 10; ++ii) { + if(averages[ii] > highestAverage) highestAverage = averages[ii]; + } - char spacing[3]; + char spacing[3]; - for(int ii = 0; ii < 10; ++ii) { - spacing[0] = '\0'; - if(ii == 0) strcat(spacing, " \0"); - else if(ii != 9) strcat(spacing, " \0"); + for(int ii = 0; ii < 10; ++ii) { + spacing[0] = '\0'; + if(ii == 0) strcat(spacing, " \0"); + else if(ii != 9) strcat(spacing, " \0"); - int clean = (averages[ii] / highestAverage) * BENCH_BAR_LENGTH; + int clean = (averages[ii] / highestAverage) * BENCH_BAR_LENGTH; - printf(" %d%% to %d%% Percentile:%s%s ", ii * 10, (ii + 1) * 10, spacing, TEXT_GRAY); - for(int c = 0; c < clean; ++c) { - printf("#"); - } - printf("%s (%s%.2fus%s average)\n", RESET, TEXT_HCYAN, averages[ii], RESET); - } + printf(" %d%% to %d%% Percentile:%s%s ", ii * 10, (ii + 1) * 10, spacing, TEXT_GRAY); + for(int c = 0; c < clean; ++c) { + printf("#"); + } + printf("%s (%s%.2fus%s average)\n", RESET, TEXT_HCYAN, averages[ii], RESET); + } - double timeWithoutHighest = stats[i].total - highestAverage * 10; - printf("\n Total time duration without highest avg: %s%.1fus%s (%s%.1f%%%s over total running time)", TEXT_HCYAN, timeWithoutHighest, RESET, TEXT_CYAN, (timeWithoutHighest / totalTimeTaken) * 100, RESET); - printf("\n Average without highest avg: %s%.2fus%s\n\n", TEXT_HCYAN, (timeWithoutHighest / runs), RESET); + double timeWithoutHighest = stats[i].total - highestAverage * 10; + printf("\n Total time duration without highest avg: %s%.1fus%s (%s%.1f%%%s over total running time)", TEXT_HCYAN, timeWithoutHighest, RESET, TEXT_CYAN, (timeWithoutHighest / totalTimeTaken) * 100, RESET); + printf("\n Average without highest avg: %s%.2fus%s\n\n", TEXT_HCYAN, (timeWithoutHighest / runs), RESET); } } diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 4d75f5f..ce915a7 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -8,13 +8,12 @@ #include #include "./lexer.h" -#include "./tokens.h" /** * Sets the token type of the currently selected token in the LexerResult with the provided token type. */ -void pushToken(LEXER_RESULT* result, TOKEN_TYPE type) { +void pushToken(LEXER_RESULT* result, LEXER_TOKEN_TYPE type) { result->tokens[result->size].type = type; result->size++; } diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h index 61bb123..959c297 100644 --- a/src/lexer/lexer.h +++ b/src/lexer/lexer.h @@ -2,7 +2,45 @@ #define LEXER_H #include -#include "./tokens.h" + +typedef enum { + ASM_FUNCTION, + FUNCTION, + RETURN, + VAR, + BRACKETS_OPEN, + BRACKETS_CLOSE, + PAREN_OPEN, + PAREN_CLOSE, + ARRAY_OPEN, + ARRAY_CLOSE, + NUMBER, + STRING, + BOOLEAN_VALUE, + NU, + KEYWORD, + SEMICOLON, + COMMA, + DECLARE, + USE, + NONE, + MATH_OP, + + TYPE_INT32, + TYPE_INT24, + TYPE_INT16, + TYPE_INT8, + TYPE_BIT +} LEXER_TOKEN_TYPE; + +/** + * An lexer token generated by the Lexer. + */ +typedef struct { + LEXER_TOKEN_TYPE type; + char* value; +} TOKEN; + /** * Contains the results of lexical analysis @@ -23,6 +61,6 @@ LEXER_RESULT runLexer(char* input, int size); * @param result The LexerResult to add the token to * @param type The type of the token to add */ -void pushToken(LEXER_RESULT* result, TOKEN_TYPE type); +void pushToken(LEXER_RESULT* result, LEXER_TOKEN_TYPE type); #endif diff --git a/src/lexer/tokens.h b/src/lexer/tokens.h deleted file mode 100644 index 68b20ab..0000000 --- a/src/lexer/tokens.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef TOKENS_H -#define TOKENS_H - -typedef enum { - ASM_FUNCTION, - FUNCTION, - RETURN, - VAR, - BRACKETS_OPEN, - BRACKETS_CLOSE, - PAREN_OPEN, - PAREN_CLOSE, - ARRAY_OPEN, - ARRAY_CLOSE, - NUMBER, - STRING, - BOOLEAN_VALUE, - NU, - KEYWORD, - SEMICOLON, - COMMA, - DECLARE, - USE, - NONE, - MATH_OP, - - TYPE_INT32, - TYPE_INT24, - TYPE_INT16, - TYPE_INT8, - TYPE_BIT -} TOKEN_TYPE; - -/** - * An lexer token generated by the Lexer. - */ -typedef struct { - TOKEN_TYPE type; - char* value; -} TOKEN; - - -#endif