Kane
Kane

Reputation: 163

fast way to convert datetime to string

I want to know if there is faster way to convert a datetime to a string besides datestr.

datetime is inserted every other lines in my main function (including all of its dependency). I need time at that line of code is executed.

I think my only option is to convert datetime to string faster.

t = datetime('now');
DateString = datestr(t);

I profiled and it seems it is called 12570846 times. It takes 16030.021s in total.

My goal of doing this is get the current time when the line is executed and to match with other information that I get from other program with timestamps. I match two files (one from this MATLAB code and one from my other program) with time stamps.

Upvotes: 1

Views: 1428

Answers (2)

Suever
Suever

Reputation: 65430

One way you could do this would be to compare the current time to the time the previous time through the loop. You should only recompute the datestring value if it's different. But we can actually go a step further, because the output of datestr (as you're calling it) only shows seconds. So we can actually ignore microsecond differences.

Example Using now (~128 Seconds)

Below I have an example loop that caches the date string representation. It compares the serial date (in seconds) to the date for which the last date string was generated. Only if it's different is the date string updated.

% Number of seconds in a day to convert the serial date number
secperday = 60 * 60 * 24;

% Store the current time as a reference
lasttime = now;
datestring = datestr(lasttime);

for k = 1:12570846
    N = now;
    seconds = round(N * secperday);

    if ~isequal(lasttime, seconds)
    % Update the cache
        lasttime = seconds;
        datestring = datestr(N);
    end

    % Use datestring however you want
    disp(datestring)
end

Example using clock (~24 seconds)

Another option is to use clock which will give you the different date components in a vector. You can round the last element which represents seconds and milliseconds. By rounding it you suppress the milliseconds. This method seems to be a faster approach.

N = clock;

% Remove milliseconds
N(end) = round(N(end));
lasttime = N;
datestring = datestr(N);

for k = 1:12570846
    N = clock;
    % Ignore milliseconds
    N(end) = round(N(end));

    if ~isequal(N, lasttime)
        lasttime = N;
        datestring = datestr(N);
    end

    disp(datestring)
end

Funtion-Based Solution

If you want to get the current time as a date string at several points within your code, it is likely much better to create a function which will wrap this functionality. Here is an example of such a function.

function str = getDateString()
    % Use persistent variables to cache the current value
    persistent lasttime datestring

    % Get the current time
    thistime = clock;

    % Truncate the milliseconds
    thistime(end) = floor(thistime(end));

    % See if the time has changed since the last time we called this
    if ~isequal(thistime, lasttime)
        lasttime = thistime;

        % Update the cached string reprsentation
        datestring = datestr(thistime);
    end

    str = datestring;
end

You can then call this from anywhere within your code to get the date string and it will only be computed when necessary.

Upvotes: 4

R Keene
R Keene

Reputation: 71

If your loop time is pretty short you might convert the date time every 10th loop or something like that if it will be close enough.

Upvotes: 0

Related Questions