Slow StringReplace

 

Hi there,

Currently I am running a function to modify a time string:

string timeDB(string timeIn)
{  
   //         01234567890123456789012
   // timeIN: YYYY.MM.DD HH:MM:ss
   //timeOUT: YYYY-MM-DD HH:MM:ss.000

   string timeOut, timeHMS, timeDate;

   timeDate = StringSubstr(TimeToStr(StrToInteger(StrToTime(timeIn)), TIME_DATE|TIME_SECONDS), 0, 10);
   StringReplace(timeDate, ".", "-");
   
   timeHMS = StringSubstr(TimeToStr(StrToInteger(StrToTime(timeIn)), TIME_DATE|TIME_SECONDS), 11, 0);
   
   timeOut = timeDate + " " + timeHMS + ".000";
   
   //Print(timeOut);
   
   return(timeOut);
}


The problem is, this is pretty slow if I run it "every" tick on backtesting.

Is there a way to make it go faster?

 
grimmigenaaier:

Hi there,

Currently I am running a function to modify a time string:


The problem is, this is pretty slow if I run it "every" tick on backtesting.

Is there a way to make it go faster?

Try this:

string timeDB(string timeIn)
{  
   //         01234567890123456789012
   // timeIN: YYYY.MM.DD HH:MM:ss
   //timeOUT: YYYY-MM-DD HH:MM:ss.000


   StringReplace(timeIn, ".", "-");

   StringAdd(timeIn, ".000");
   
   //Print(timeIn);
   
   return(timeIn);
}
 

If timeIn format is always as written, it would be better to replace dot with dash using:

StringSetCharacter()


Other option is to handle formating at database.

 
grimmigenaaier:

Hi there,

Currently I am running a function to modify a time string:


The problem is, this is pretty slow if I run it "every" tick on backtesting.

Is there a way to make it go faster?

Beside the better solution as proposed by Mohammad, why would you need to do such thing every tick ? Don't do it.
 
Mohammad Hossein Sadeghi:

If timeIn format is always as written, it would be better to replace dot with dash using:


Other option is to handle formating at database.


Thanks. I have opted for formatting at the Database side.

I did sort out my problem though: just very inefficient coding on my behalf.

 
grimmigenaaier:

Is there a way to make it go faster? 

#property strict

string timeDB( const datetime timeIn )
{  
   //         01234567890123456789012
   // timeIN: YYYY.MM.DD HH:MM:ss
   //timeOUT: YYYY-MM-DD HH:MM:ss.000

   string timeOut = (string)timeIn;
   
   StringSetCharacter(timeOut, 4, '-');
   StringSetCharacter(timeOut, 7, '-');

   return(timeOut + ".000");
}

void OnStart()
{
  Print(timeDB(TimeLocal()));
}
Reason: