How about caching results? Isn't that an opportunity? Given that this particular sprintf () call is made too often in your code, I assume that between most of these consecutive calls, the year, month, and day do not change.
So we can implement something like the following. Declare the old and current SYSTEMTIME structure:
SYSTEMTIME sysTime, oldSysTime;
In addition, declare separate parts for storing date and time:
char datePart[80]; char timePart[80];
The first time you will need to populate both sysTime, oldSysTime, and datePart and timePart. But subsequent sprintf () can be done pretty quickly, as follows:
sprintf (timePart, "% 02d:% 02d:% 02d", sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
if (oldSysTime.wYear == sysTime.wYear &&
oldSysTime.wMonth == sysTime.wMonth &&
oldSysTime.wDay == sysTime.wDay)
{
// we can reuse the date part
strcpy (buff, datePart);
strcat (buff, timePart);
}
else {
// we need to regenerate the date part as well
sprintf (datePart, "% 4d-% 02d-% 02d", sysTime.wYear, sysTime.wMonth, sysTime.wDay);
strcpy (buff, datePart);
strcat (buff, timePart);
}
memcpy (& oldSysTime, & sysTime, sizeof (SYSTEMTIME));
The above code has some redundancy to make the code more understandable. You can easily decompose. You can speed things up even more if you know that even the hour and minutes will not change faster than your subprogram call.
Jaywalker
source share