You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. From f1fc04d7609ab074647aa00e96d4c66d5135b155 Mon Sep 17 00:00:00 2001
  2. From: Michael Heimpold <michael.heimpold@i2se.com>
  3. Date: Tue, 18 Dec 2018 15:02:25 +0100
  4. Subject: [PATCH 4/9] Optimize to_binstr() function
  5. Appending multiple times to same string is slow since strcat() needs
  6. to determine the end during each run. So manually maintain a pointer
  7. to the end to speed-up things.
  8. Signed-off-by: Michael Heimpold <michael.heimpold@i2se.com>
  9. Cc: Michael Heimpold <mhei@heimpold.de>
  10. ---
  11. lsmmc.c | 11 +++++++----
  12. 1 file changed, 7 insertions(+), 4 deletions(-)
  13. diff --git a/lsmmc.c b/lsmmc.c
  14. index e64117c..86713f7 100644
  15. --- a/lsmmc.c
  16. +++ b/lsmmc.c
  17. @@ -371,12 +371,14 @@ char *to_binstr(char *hexstr)
  18. "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
  19. "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111",
  20. };
  21. - char *binstr;
  22. + char *binstr, *tail;
  23. binstr = calloc(strlen(hexstr) * 4 + 1, sizeof(char));
  24. if (!binstr)
  25. return NULL;
  26. + tail = binstr;
  27. +
  28. while (hexstr && *hexstr != '\0') {
  29. if (!isxdigit(*hexstr)) {
  30. free(binstr);
  31. @@ -384,13 +386,14 @@ char *to_binstr(char *hexstr)
  32. }
  33. if (isdigit(*hexstr))
  34. - strcat(binstr, bindigits[*hexstr - '0']);
  35. + strcat(tail, bindigits[*hexstr - '0']);
  36. else if (islower(*hexstr))
  37. - strcat(binstr, bindigits[*hexstr - 'a' + 10]);
  38. + strcat(tail, bindigits[*hexstr - 'a' + 10]);
  39. else
  40. - strcat(binstr, bindigits[*hexstr - 'A' + 10]);
  41. + strcat(tail, bindigits[*hexstr - 'A' + 10]);
  42. hexstr++;
  43. + tail += 4;
  44. }
  45. return binstr;
  46. --
  47. 2.17.1