From 6194472e39f7d772b7adfbfd485b4eab492315ce Mon Sep 17 00:00:00 2001 From: stmctommyau Date: Tue, 26 Sep 2023 00:48:34 +0800 Subject: [PATCH] Optimization --- database.h | 23 +++++++++-------------- tran_control.h | 6 +++--- user_control.h | 6 +++--- utils.h | 5 ++++- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/database.h b/database.h index 00f39ee..39d5f83 100644 --- a/database.h +++ b/database.h @@ -169,7 +169,7 @@ void remove_admin(char* role){ } char line[100]; while(fgets(line,100,fp) != NULL){ - if(line[0] == '#') { + if(unlikely(line[0] == '#')) { fprintf(fp2,"%s",line); continue; } @@ -194,7 +194,7 @@ int basic_get_row_count(int end, FILE *fp){ while(!feof(fp)&&!ferror(fp)){ char buffer[100]; fgets(buffer, sizeof(buffer),fp); - if(buffer[0] == '#' || buffer[0] == '\0'){//catch comment line and ignore + if(unlikely(buffer[0] == '#' || buffer[0] == '\0')){//catch comment line and ignore continue; } colmun_count++; @@ -223,7 +223,7 @@ struct inventory read_db_invt(){//please open file in read mode for(INVENTORY j=category;j<=ENDOFINVENTORY;j++){ char buffer[100]; fgets(buffer, sizeof(buffer),fp); - if(buffer[0] == '#'){//catch comment line and ignore + if(unlikely(buffer[0] == '#')){//catch comment line and ignore j--;//decrement j to prevent skipping next column }else{ buffer[strlen(buffer)] = '\0'; @@ -265,8 +265,6 @@ struct inventory read_db_invt(){//please open file in read mode fclose(fp); db.db.init_status = true; return db; - - } struct transaction read_db_tran(){ FILE* fp = fopen(TRANSACTION_DB, "r"); @@ -285,7 +283,7 @@ struct transaction read_db_tran(){ for(TRANSACTION j=date;j<=ENDOFTRANSACTION;j++){ char buffer[100]; fgets(buffer, sizeof(buffer),fp); - if(buffer[0] == '#'){//catch comment line and ignore + if(unlikely(buffer[0] == '#')){//catch comment line and ignore j--;//decrement j to prevent skipping next column }else{ buffer[strlen(buffer)] = '\0'; @@ -344,7 +342,7 @@ struct user read_db_user(){ for(USER j=name;j<=ENDOFUSER;j++){ char buffer[100]; fgets(buffer, sizeof(buffer),fp); - if(buffer[0] == '#'){//catch comment line and ignore + if(unlikely(buffer[0] == '#')){//catch comment line and ignore j--;//decrement j to prevent skipping next column }else{ buffer[strlen(buffer)] = '\0';//prevent any garbage value @@ -463,15 +461,12 @@ bool update_db_invt(struct inventory invt){ return false; } for(int i=0;i #endif #define SIZE_OF_PAGE 20 - +#ifndef likely +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#endif void Cls(){ #ifdef _WIN32 system("cls");