[PATCH mmc-utils v2 4/5] Optimize to_binstr() function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Michael Heimpold <michael.heimpold@xxxxxxxx>

Appending multiple times to same string is slow since strcat() needs
to determine the end during each run. So manually maintain a pointer
to the end to speed-up things.

Signed-off-by: Michael Heimpold <michael.heimpold@xxxxxxxx>
Cc: Michael Heimpold <mhei@xxxxxxxxxxx>
Reviewed-by: Avri Altman <avri.altman@xxxxxxx>
---
 lsmmc.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/lsmmc.c b/lsmmc.c
index e64117c..86713f7 100644
--- a/lsmmc.c
+++ b/lsmmc.c
@@ -371,12 +371,14 @@ char *to_binstr(char *hexstr)
 		"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
 		"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111",
 	};
-	char *binstr;
+	char *binstr, *tail;
 
 	binstr = calloc(strlen(hexstr) * 4 + 1, sizeof(char));
 	if (!binstr)
 		return NULL;
 
+	tail = binstr;
+
 	while (hexstr && *hexstr != '\0') {
 		if (!isxdigit(*hexstr)) {
 			free(binstr);
@@ -384,13 +386,14 @@ char *to_binstr(char *hexstr)
 		}
 
 		if (isdigit(*hexstr))
-			strcat(binstr, bindigits[*hexstr - '0']);
+			strcat(tail, bindigits[*hexstr - '0']);
 		else if (islower(*hexstr))
-			strcat(binstr, bindigits[*hexstr - 'a' + 10]);
+			strcat(tail, bindigits[*hexstr - 'a' + 10]);
 		else
-			strcat(binstr, bindigits[*hexstr - 'A' + 10]);
+			strcat(tail, bindigits[*hexstr - 'A' + 10]);
 
 		hexstr++;
+		tail += 4;
 	}
 
 	return binstr;
-- 
2.17.1




[Index of Archives]     [Linux Memonry Technology]     [Linux USB Devel]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux