Execution time using a function or not

Questions about MultiCharts and user contributed studies.
User avatar
JoshM
Posts: 2075
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1523 times
Been thanked: 1480 times
Contact:

Execution time using a function or not

Postby JoshM » 01 Apr 2012

I was wondering what would give a quicker execution time:

    Repeatedly calling a function or having that code directly into the indicator?

My assumption was that using a function would be slower, since, depending on how the code is compiled by the PowerLanguage Editor, the code might have to “jump” to the function (in a separate file) and back to the indicator calling that function.

So I tried to measure that with the following code in an indicator:

Code: Select all

Variables:
   oneSecond(ELTimeToDateTime_s(1)), startTime(0), endTime(0), runs(0), b(0),
   outputStr(""), exampleStr(""), x(0), y(0), z(0), lengthStr(0), inStrPlace(0),
   fileName("C:\Temp\MC_ExecutionTimeTest.txt");

if (LastBarOnChart_s = True) and (BarStatus(1) = 2) then begin

   once cleardebug;
   
   runs = 1000;
   
   // Calling the function
   startTime = ComputerDateTime;   
   FileDelete(fileName);   // Delete old file
   
   Print("Beginning with calling the function for ", NumToStr(runs, 0), " times.", NewLine,
         "Current time is ", FormatTime("HH:mm:ss", startTime));

   for b = 1 to runs begin
   
      MyTestFunction3;
   
   end;

   endTime = ComputerDateTime;
   Print(NewLine, "Ending @ ", FormatTime("HH:mm:ss", endTime));
   Print("Runtime: ", NumToStr((endTime - startTime) / oneSecond, 0), " seconds", NewLine, NewLine);

   
   // Using the regular code
   startTime = ComputerDateTime;   
   FileDelete(fileName);   // Delete old file
   
   Print("Beginning with the regular code for ", NumToStr(runs, 0), " times.", NewLine,
         "Current time is ", FormatTime("HH:mm:ss", startTime));   
   
   for b = 1 to runs begin
   
      // String manipulations
      exampleStr = Text("This test is meant to measure the execution time of using a function or not.",
                     "Especially with string operations we can expect more execution time than with numeric calculations.");

      // Replace all n's in the string with x's
      inStrPlace    = InStr(exampleStr, "n");

      while (inStrPlace <> 0) begin

         lengthStr    = StrLen(exampleStr);
         
         exampleStr    = Text(
            LeftStr(exampleStr, inStrPlace - 1),
            "x",
            RightStr(exampleStr, (lengthStr - inStrPlace)));

         inStrPlace    = InStr(exampleStr, "n");
      end;

      // Replace all e's with 9's
      inStrPlace    = InStr(exampleStr, "e");

      while (inStrPlace <> 0) begin

         lengthStr    = StrLen(exampleStr);
         
         exampleStr    = Text(
            LeftStr(exampleStr, inStrPlace - 1),
            "9",
            RightStr(exampleStr, (lengthStr - inStrPlace)));

         inStrPlace    = InStr(exampleStr, "e");
      end;

      // FileAppend the example string
      FileAppend(fileName, Text(exampleStr, NewLine));

      // Numeric calculations
      y = 0; z = 0; outputStr = "";
      for x = 1 to 2500 begin

         y = Log(x / 2500);
         z = y * 25;

         // Make a string with these values
         outputStr = Text(outputStr, "y: ", NumToStr(y, 15), NewLine, "x: ", NumToStr(x, 15), NewLine, "z: ", NumToStr(z, 15), NewLine);

      end;

      // Also FileAppend
      FileAppend(fileName, outputStr);

   end; //: Loop end
   
   endTime = ComputerDateTime;
   Print(NewLine, "Ending @ ", FormatTime("HH:mm:ss", endTime));
   Print("Runtime: ", NumToStr((endTime - startTime) / oneSecond, 0), " seconds", NewLine, NewLine);
   
end; //: LastBarOnChart


And the following code in the “MyTestFunction3” function:
(Which is the same as the code in the indicator above)

Code: Select all

Variables:
   outputStr(""), exampleStr(""), x(0), y(0), z(0), lengthStr(0), inStrPlace(0),
   fileName("C:\Temp\MC_ExecutionTimeTest.txt");

// Delete fileName
once FileDelete(fileName);

// String manipulations
exampleStr = Text("This test is meant to measure the execution time of using a function or not.",
               "Especially with string operations we can expect more execution time than with numeric calculations.");

// Replace all n's in the string with x's
inStrPlace    = InStr(exampleStr, "n");

while (inStrPlace <> 0) begin

   lengthStr    = StrLen(exampleStr);
   
   exampleStr    = Text(
      LeftStr(exampleStr, inStrPlace - 1),
      "x",
      RightStr(exampleStr, (lengthStr - inStrPlace)));

   inStrPlace    = InStr(exampleStr, "n");
end;

// Replace all e's with 9's
inStrPlace    = InStr(exampleStr, "e");

while (inStrPlace <> 0) begin

   lengthStr    = StrLen(exampleStr);
   
   exampleStr    = Text(
      LeftStr(exampleStr, inStrPlace - 1),
      "9",
      RightStr(exampleStr, (lengthStr - inStrPlace)));

   inStrPlace    = InStr(exampleStr, "e");
end;

// FileAppend the example string
FileAppend(fileName, Text(exampleStr, NewLine));

// Numeric calculations
y = 0; z = 0; outputStr = "";
for x = 1 to 2500 begin

   y = Log(x / 2500);
   z = y * 25;

   // Make a string with these values
   outputStr = Text(outputStr, "y: ", NumToStr(y, 15), NewLine, "x: ", NumToStr(x, 15), NewLine, "z: ", NumToStr(z, 15), NewLine);

end;

// Also FileAppend
FileAppend(fileName, outputStr);


Results
This gave the following results on my pc:

Code: Select all

Beginning with calling the function for 1000 times.
Current time is 11:05:39

Ending @ 11:11:42
Runtime: 363 seconds


Beginning with the regular code for 1000 times.
Current time is 11:11:42

Ending @ 11:17:39
Runtime: 357 seconds


So, in this instance with this type of code, using the code directly gave an execution time that was around 1% quicker (I didn’t used the computer during this time, but background processes might have intervened, which could explain this insignificant difference).

’Conclusion’
Since the text file was quite big (174mb), I’m inclined to conclude that during “normal” use, there is no time difference in execution time between calling a function repeatedly or inserting the code directly in an indicator.

What’s your opinion on this? Do you agree with this ‘conclusion’ or have you experienced that there are indeed performance differences? :)

User avatar
TJ
Posts: 6523
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 957 times
Been thanked: 1883 times

Re: Execution time using a function or not

Postby TJ » 01 Apr 2012

JoshM wrote:I was wondering what would give a quicker execution time:

    Repeatedly calling a function or having that code directly into the indicator?

...

What’s your opinion on this? Do you agree with this ‘conclusion’ or have you experienced that there are indeed performance differences? :)


How many times did you run the code to arrive at the 1% difference?
How much of the differences can be attributed to other variances?


ps. In the old days, when computer memory were expensive, programmers were forced to use functions as a way to fit the program into the memory, and the computer had to go to the harddisk to retrieve a function segment. But MultiCharts' PowerLanguage is compiled, and everything is residing in the RAM, there is no speed penalties of yesteryears.

User avatar
JoshM
Posts: 2075
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1523 times
Been thanked: 1480 times
Contact:

Re: Execution time using a function or not

Postby JoshM » 01 Apr 2012

TJ wrote:How many times did you run the code to arrive at the 1% difference? How much of the differences can be attributed to other variances?

Once, and I don't know how much can be attributed to other processes, since well, I can't run MultiCharts without Windows itself running, so the measurement can always be somewhat 'messy'.

But let's not make this thread about that one percent :), because like I said in the post it's a insignificant difference that can easily be attributed to Windows processes (especially since there were repeatedly write to file actions).

TJ wrote:You might need to worry about speed with an interpreted language, but MultiCharts' PowerLanguage is compiled, there is no need to worry about speed penalties.

That was what I was wondering about, and what I was hoping to hear. :)

Edit:
TJ wrote:How many times did you run the code to arrive at the 1% difference?

Actually, that is a good idea to run it a bunch of times. Will look into that, thanks!


Return to “MultiCharts”