c++cmktime

Slow mktime when parsing 100,000 YYYYMMDD.HHMMSS strings in C/C++


I have 100,000 std::strings in the form YYYYMMDD.HHMMSS, eg. "20160621.213500" which I need to parse and populate a structure with some values, including an epoch timestamp.

It runs very slowly and the culprit is the call to mktime. Any alternative to speed this up?

#define STRNCPY(dest, src, len) \
    { memcpy((dest), (src), (len)) ; dest[(len)] = '\0'; }

void
DvStorUtils::parseDateTimeString(const char *dateTimeStr, TDateTime &dateTime)
{
    // New, C-Style implementation
    strcpy(dateTime.dateTimeStr, dateTimeStr);

    char buf[32];
    STRNCPY(buf, dateTimeStr,    4);  dateTime.year   = atoi(buf);
    STRNCPY(buf, dateTimeStr+ 4, 2);  dateTime.month  = atoi(buf);
    STRNCPY(buf, dateTimeStr+ 6, 2);  dateTime.day    = atoi(buf);
    STRNCPY(buf, dateTimeStr+ 9, 2);  dateTime.hour   = atoi(buf);
    STRNCPY(buf, dateTimeStr+11, 2);  dateTime.minute = atoi(buf);
    STRNCPY(buf, dateTimeStr+13, 2);  dateTime.second = atoi(buf);


    struct tm tmStruct;
    tmStruct.tm_year = dateTime.year - 1900;
    tmStruct.tm_mon  = dateTime.month-1;
    tmStruct.tm_mday = dateTime.day;
    tmStruct.tm_hour = dateTime.hour;
    tmStruct.tm_min  = dateTime.minute;
    tmStruct.tm_sec  = dateTime.second;

    dateTime.totalSecElapsed = mktime(&tmStruct);
}

Solution

  • Jeremy had the right idea – mktime is very very slow.

    Comment out mktime and 100,000 lines are processed in an instant. Even when printing out each line. Put it back in and takes almost a minute to complete.

    I replaced mktime with the caching implementation libfast-mktime and it is almost as fast as without mktime at all.

    The memoized libfast-mktime works well here because each line is very close in time to the ones before it.