Skip to content

Commit

Permalink
fix: benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Zffu committed Jan 17, 2025
1 parent 117cbe8 commit 5271207
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 97 deletions.
116 changes: 66 additions & 50 deletions benchmarks/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
#include <windows.h>
#include <math.h>

#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
Expand Down Expand Up @@ -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) {
Expand All @@ -117,76 +124,85 @@ 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);
startTimer();

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);
}
}
3 changes: 1 addition & 2 deletions src/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
#include <ctype.h>

#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++;
}
Expand Down
42 changes: 40 additions & 2 deletions src/lexer/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,45 @@
#define LEXER_H

#include <stdio.h>
#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
Expand All @@ -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
43 changes: 0 additions & 43 deletions src/lexer/tokens.h

This file was deleted.

0 comments on commit 5271207

Please sign in to comment.