Features of the mql5 language, subtleties and tricks - page 256

 
amrali #:

With the help from codes by fxsaber here and here and some codes from this webpage source code, I was able to devise a very fast function to get year, month and day of year.

I updated the previous test done by Nokali before.

Results:

Edit:

GetMonthFast() uses branchless code, no if statements, and I think memoisation is not required because it takes only 2 nanosec to execute.

May be the comparisons with TimeToStruct() is not fair enough (it has to calculate all the other members of the struct), but it was included here to verify checksums.

Wow! Bravo!
Thanks, Amrali

 

Conclusion:

(1) The decoding of datetime variables into the calendar dates (year, month and day) is fast enough. The TimeToStruct() function is doing a very good job at extraction.

(2) The encoding routines for converting calendar dates into datetime variables are really slow, encoding benchmarks here  had showed speed-ups of100x times or more.

I think the StructToTime () and StringToTime () need some consideration from the developers , because these functions are frequently used in experts / indicators on the platform to encode datetime .

 
amrali StructToTime ( ) and StringToTime ( ) need some consideration from developers, as these functions are often used in experts/indicators on the platform to encode date and time.

Yes, there is an obvious performance-affecting semantic bug in StructToTime( ) function



 

As a temp solution to add an optimized StructToTime() to the existing experts and indicators that heavily use this function, this can be done with minimal code change:

StructToTime.mqh

#ifndef STRUCTTOTIME_UNIQUE_HEADER_ID_H
#define STRUCTTOTIME_UNIQUE_HEADER_ID_H
class GlobalApiHook
  {
public:
   static datetime   StructToTime_Hook(MqlDateTime &st)
     {
      datetime date = CreateDateTime5(st.year, st.mon, st.day);
      return date + (st.hour * 3600 + st.min * 60 + st.sec);
     }

private:
   static datetime   CreateDateTime5(const int Year, const int Month, const int Day)
     {
      static const uint Months[] = {0, 11512676, 11512180, 11511728, 11511232, 11510750, 11510256,
                                       11509774, 11509280, 11508781, 11508302, 11507806, 11507326};
      return (((Year * 5844 - Months[Month]) >> 4) + Day - 1) * (24 * 60 * 60);
     }
  };

#define StructToTime(st)       GlobalApiHook::StructToTime_Hook(st)
#endif

your existing code:

// add this line to your existing code
#include "StructToTime.mqh"

void OnStart()
  {
   MqlDateTime st = {2024, 12, 31, 09, 24, 59};

   datetime t = StructToTime(st);

   Print("Time: ", t);
  }

// Time: 2024.12.31 09:24:59
 
amrali #:

some codes from this webpage source code

Thanks, interesting source code.

I was able to devise a very fast function to get year, month and day of year.

Great to put the best solutions together. A little acceleration.

int GetMonthFast2(datetime time)   // return 1...12
  {
   int days    = (int)(time / (60 * 60 * 24));
   int year    = ((days << 2) + 2) / 1461;
   int yearday = days - ((year * 1461 + 1) >> 2);
   int isleap  = ((year & 3) == 2);
   int leapadj = ((yearday < (59 + isleap)) ? 0 : (2 - isleap));
   int month   = ((((yearday + leapadj) * 12) + 373) / 367);
   return (month);
  }
 
amrali #:

As a temporary solution to add the optimised StructToTime() function to existing EAs and indicators that actively use this function, this can be done with minimal code changes:

Why didn't CreateDateTime6?


Probably it makes sense to make a temporary solution for TimeToStruct as well.

 
fxsaber #:

Why didn't they use CreateDateTime6?

I found the performance in randomized benchmarks not different from CreateDateTime5() or CreateDateTime4().
Any of the above three options are great.
 
Nikolai Semko #:

We can summarise
A set of fast structure-less functions to get date and time parameters up to 2100 by a single input parameter datetime:

GetDayOfYeah?

 
Andrei Iakovlev #:

And GetDayOfYeah?

Forum on trading, automated trading systems and testing trading strategies

Peculiarities of mql5 language, subtleties and techniques of work

fxsaber, 2024.04.20 09:42

int GetMonthFast2(datetime time)   // return 1...12
  {
   int days    = (int)(time / (60 * 60 * 24));
   int year    = ((days << 2) + 2) / 1461;
   int yearday = days - ((year * 1461 + 1) >> 2);
   int isleap  = ((year & 3) == 2);
   int leapadj = ((yearday < (59 + isleap)) ? 0 : (2 - isleap));
   int month   = ((((yearday + leapadj) * 12) + 373) / 367);
   return (month);
  }
 
fxsaber #:

We should either create a separate topic for these codes,

or move them to the blog, otherwise they get lost.