- Repeatedly calling a function or having that code directly into the indicator?
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
(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);
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
’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?