Slow StringReplace

To add comments, please log in or register
grimmigenaaier
12
grimmigenaaier  

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?

Mohammad Hossein Sadeghi
2701
Mohammad Hossein Sadeghi  
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);
}
Mohammad Hossein Sadeghi
2701
Mohammad Hossein Sadeghi  

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.

Alain Verleyen
38714
Alain Verleyen  
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.
grimmigenaaier
12
grimmigenaaier  
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.

fxsaber
15793
fxsaber  
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()));
}
To add comments, please log in or register