//+------------------------------------------------------------------+
//|                                                   UpdateCost.mq5 |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#property version   "1.10"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <ChartText\CanvasBlock.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input int    InpReps  = 200;                          // Set() calls per measurement
input int    InpRuns  = 3;                            // measurements per row of the table
input string InpFile  = "ChartText\\update_cost.csv"; // result file
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
int g_line_counts[]={1,5,10,20,40};
//+------------------------------------------------------------------+
//| MakeText                                                         |
//| line_count paragraphs. Every paragraph is one line of the        |
//| block, 55 characters, so both blocks draw the same rows. Row 0   |
//| carries a counter, and with all_change=true every row does.      |
//+------------------------------------------------------------------+
string MakeText(const int line_count,const int step,const bool all_change)
  {
   string text="";
   for(int i=0; i<line_count; i++)
     {
      int value=(i==0 || all_change) ? 10000+step+i : 10000+i;
      text+=(i>0 ? "\n" : "")+StringFormat("Row %02d: balance %d.19, equity %d.99, margin 487.35%%",i,value,value-41);
     }
   return(text);
  }
//+------------------------------------------------------------------+
//| BuildTexts                                                       |
//| The texts of one measurement, built before the clock starts, so  |
//| that the time does not include the building of the text.         |
//+------------------------------------------------------------------+
void BuildTexts(const int line_count,const string scenario,string &texts[])
  {
   ArrayResize(texts,InpReps+1);
   for(int r=0; r<=InpReps; r++)
      texts[r]=(scenario=="same" || scenario=="redraw") ? MakeText(line_count,0,false) : MakeText(line_count,r,scenario=="all");
  }
//+------------------------------------------------------------------+
//| TimeBlock                                                        |
//| Mean microseconds of one Set() call, each followed by a call of  |
//| ChartRedraw(), for the labels or for the canvas. The scenario    |
//| redraw leaves Set() out and times ChartRedraw() alone.           |
//+------------------------------------------------------------------+
double TimeBlock(const bool canvas,const int line_count,const string scenario,const long chart)
  {
   CLabelBlock labels;
   CCanvasBlock bitmap;
   if(canvas)
      bitmap.Init("CTC_canvas",10,40,700,"Arial",10,clrBlack,clrWhite,4,2,chart);
   else
      labels.Init("CTC_labels",10,40,700,"Arial",10,clrBlack,2,chart);
   string texts[];
   BuildTexts(line_count,scenario,texts);
   if(canvas)
      bitmap.Set(texts[0]);
   else
      labels.Set(texts[0]);
   ulong start=GetMicrosecondCount();
   for(int r=1; r<=InpReps; r++)
     {
      if(scenario=="redraw")
        {
         ChartRedraw(chart);
         continue;
        }
      if(canvas)
         bitmap.Set(texts[r]);
      else
         labels.Set(texts[r]);
      ChartRedraw(chart);
     }
   ulong spent=GetMicrosecondCount()-start;
   if(canvas)
      bitmap.Clear();
   else
      labels.Clear();
   return((double)spent/InpReps);
  }
//+------------------------------------------------------------------+
//| TimeWrap                                                         |
//| Mean microseconds of the wrap alone, with the same texts.        |
//+------------------------------------------------------------------+
double TimeWrap(const int line_count)
  {
   CScreenMeter meter;
   meter.Set("Arial",10);
   string lines[];
   string texts[];
   BuildTexts(line_count,"all",texts);
   ulong start=GetMicrosecondCount();
   for(int r=1; r<=InpReps; r++)
      CTextWrap::Wrap(meter,texts[r],700,TXT_LABEL_LIMIT,lines);
   return((double)(GetMicrosecondCount()-start)/InpReps);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string scenarios[]={"one","all","same","redraw"};
   int handle=FileOpen(InpFile,FILE_WRITE|FILE_CSV|FILE_ANSI,',');
   if(handle==INVALID_HANDLE)
     {
      PrintFormat("COST cannot open %s, error %d",InpFile,GetLastError());
      return;
     }
   FileWrite(handle,"scenario","lines","block","run","reps","microseconds_per_call");
   long chart=ChartOpen(_Symbol,PERIOD_H1);
   Sleep(1000);
   PrintFormat("COST build=%d reps=%d runs=%d",TerminalInfoInteger(TERMINAL_BUILD),InpReps,InpRuns);
   for(int s=0; s<ArraySize(scenarios); s++)
      for(int l=0; l<ArraySize(g_line_counts); l++)
         for(int run=1; run<=InpRuns; run++)
           {
            double labels=TimeBlock(false,g_line_counts[l],scenarios[s],chart);
            double bitmap=TimeBlock(true,g_line_counts[l],scenarios[s],chart);
            double wrap=(scenarios[s]=="all") ? TimeWrap(g_line_counts[l]) : -1.0;
            FileWrite(handle,scenarios[s],g_line_counts[l],"labels",run,InpReps,DoubleToString(labels,1));
            FileWrite(handle,scenarios[s],g_line_counts[l],"canvas",run,InpReps,DoubleToString(bitmap,1));
            if(wrap>=0)
               FileWrite(handle,scenarios[s],g_line_counts[l],"wrap",run,InpReps,DoubleToString(wrap,1));
            PrintFormat("COST scenario=%s lines=%d run=%d labels=%.1f canvas=%.1f wrap=%.1f",scenarios[s],g_line_counts[l],run,labels,bitmap,wrap);
           }
   FileClose(handle);
   ChartClose(chart);
  }
//+------------------------------------------------------------------+
