New Multiline Comment function i made is Faster

 

After adding multi line comment to my EA i noticed huge spike in CPU usage while Backtesting in Visual mode on every tick

string comment = "First Line\n";
comment += "Second Line\n";
comment += "Third Line\n";

Comment(comment);

So i decided to crate my own Comment function and it turned out to be 5x faster than default one so i decided to share it here

void SetComment(string text, int lineNumber=0, color textColor = clrBlack, int fontSize = 10, int spaceBetweenLines = 12, int base_X_offset = 10, int base_Y_offset = 20, string fontName = "Arial") {
    static string oldTexts[]; // Array to store the previous texts for comparison
    static bool isLabelCreated[]; // Array to keep track of whether the label has been created
    static int maxLineNumber = -1; // Tracks the highest line number encountered

    // Resize the oldTexts and isLabelCreated arrays if the lineNumber is higher than any seen before
    if(lineNumber > maxLineNumber) {
        // Allocate or reallocate arrays
        ArrayResize(oldTexts, lineNumber + 1);
        ArrayResize(isLabelCreated, lineNumber + 1);
        for(int i = maxLineNumber + 1; i <= lineNumber; i++) {
            if(!isLabelCreated[i])isLabelCreated[i] = false; // Initialize new elements to false
        }
        maxLineNumber = lineNumber;
    }

    // Compare the new text with the stored text for this line number
    if(oldTexts[lineNumber] != text || !isLabelCreated[lineNumber]) {
        // If the text is different or the label hasn't been created, update the label and store the new text
        oldTexts[lineNumber] = text;
        string labelName = "CommentLabel_" + IntegerToString(lineNumber);

        // Create the label if it hasn't been created yet
        if(!isLabelCreated[lineNumber]) {
            int yOffset = base_Y_offset + (lineNumber * (fontSize + spaceBetweenLines)); 
            if(ObjectCreate(0, labelName, OBJ_LABEL, 0, 0, 0)) {
                ObjectSetInteger(0, labelName, OBJPROP_CORNER, CORNER_LEFT_UPPER);
                ObjectSetInteger(0, labelName, OBJPROP_XDISTANCE, base_X_offset);
                ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, yOffset);
                ObjectSetInteger(0, labelName, OBJPROP_COLOR, textColor);
                ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, fontSize);
                ObjectSetString(0, labelName, OBJPROP_FONT, fontName);
                ObjectSetString(0, labelName, OBJPROP_TEXT, text);
                isLabelCreated[lineNumber] = true; // Mark the label as created
            }
        } else {
            // Update the label's text if it has already been created
            ObjectSetString(0, labelName, OBJPROP_TEXT, text);
        }
    }
    // If the text is the same as before and the label was created, no update is needed
}

I tried to make it as fast as possible but if someone see any way to make it even faster then please give me some tips and tricks

Documentation on MQL5: Common Functions / Comment
Documentation on MQL5: Common Functions / Comment
  • www.mql5.com
This function outputs a comment defined by a user in the top left corner of a chart. Parameters ... [in]   Any values, separated by commas. To...