From 15c1bfda4455122d765054bf5533c4fa9b86be03 Mon Sep 17 00:00:00 2001 From: 0xf8 <0xf8.dev@proton.me> Date: Sat, 3 Jun 2023 15:35:34 -0400 Subject: [PATCH] Optimize --- Makefile | 2 +- compile_commands.json | 6 +++++ src/main.c | 53 ++++++------------------------------------- src/main.h | 3 +-- 4 files changed, 15 insertions(+), 49 deletions(-) create mode 100644 compile_commands.json diff --git a/Makefile b/Makefile index cdc2d99..71b0681 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ OBJ = $(patsubst src/%.c,%.o,$(wildcard src/*.c)) HEADERS = $(wildcard src/*.h) CC = clang -FLAGS = -std=c17 -O2 -pthread +FLAGS = -std=c17 -O3 -pthread LIB = -lncurses -lnotcurses -lnotcurses-core %.o: src/%.c $(HEADERS) diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..49c9194 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,6 @@ +[ + { + "arguments": [ "/usr/bin/gcc", "-c", "-o", "src/main.o", "src/main.c" ], + "file": "src/main.c" + } +] \ No newline at end of file diff --git a/src/main.c b/src/main.c index 563d008..c2f1ce6 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with thi #include #include +// 250000000ns = 250ms, 1/4s enum {SECS_TO_SLEEP = 0, NSEC_TO_SLEEP = 250000000}; struct notcurses *_nc; @@ -217,7 +218,7 @@ struct bignumber_t *bignumber(int start, uint mimick) { void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, struct ncplane *plane) { for (int x = 0; x < 3; x++) { for (int y = 0; y < 5; y++) { - struct string *padded_num = pad_int(bn->matrix[y][x], 2, 2); + struct string *padded_num = pad_int(bn->matrix[y][x]); uint currentSec = (uint)sec == bn->matrix[y][x]; uint partOfMimick = mimicks[bn->mimick][y][x]; @@ -245,53 +246,13 @@ void drawbignumber(struct bignumber_t *bn, int sec, int startx, int starty, stru } -// pad_uint -struct string *pad_int(int n, uint pad, uint maxsize) { - struct string *str = (struct string *)malloc(sizeof (struct string) + 1); - maxsize++; - - str->s = (char *)malloc((sizeof(char) * maxsize) + 1); - str->length = 0; +struct string *pad_int(int n) { + struct string *str = alloc_string(4); + snprintf(str->s, 4, "%02i%c", abs(n), 0); - memset(str->s, 0, maxsize); + return str; +}; - // Figure out how many place values are in n - int _n = abs(n); - uint places = 1; - - for (uint i = 0; i < pad; i++) { - if (_n >= 10) { - _n /= 10; - places++; - } - else break; - } - - char *padding = (char *)malloc((sizeof(char) * pad) + 2); - for (uint i = 0; i < pad + 1; i++) - padding[i] = '0'; - - uint required_padding = pad - places; - if (required_padding > pad) required_padding = 0; - - // Negative value - if (n < 0) { - required_padding++; - padding[0] = '-'; - } - - snprintf(str->s, maxsize, "%*.*s%i%c", 0, required_padding, padding, abs(n), 0); - free(padding); - - for (uint i = 0; i < maxsize + 1; i++) { - if (str->s[i] == 0) { - str->length = i; - break; - } - } - - return str; -} // alloc_string struct string *alloc_string(uint l) { diff --git a/src/main.h b/src/main.h index ce9ac6e..44aa5f3 100644 --- a/src/main.h +++ b/src/main.h @@ -15,7 +15,6 @@ You should have received a copy of the GNU General Public License along with thi #define __MAIN_H__ #include -#include #include // #include #include @@ -45,7 +44,7 @@ struct string *alloc_string(uint l); void free_string(struct string *s); // pads a number with 0's -struct string *pad_int(int n, uint pad, uint maxsize); +struct string *pad_int(int n); // this creates a bignumber_t struct bignumber_t *bignumber(int start, uint mimick);