In which way program performance is best? - page 3

 
Alain Verleyen #:

If you want performance, do not use iOpen, iClose or other iXXX functions. Use CopyXXX functions.

If you want performance, you need to use more memory. In general, memory usage and performance are correlated.

Touché!

I'm glad to see you here again, I've been thinking for days; Where is Alain, post-Christmas holiday hangover? 😅

 
Alain Verleyen #:

If you want performance, do not use iOpen, iClose or other iXXX functions. Use CopyXXX functions.

If you want performance, you need to use more memory. In general, memory usage and performance are correlated.

Yes I have previously issues with iXXX functions So Is this better to MqlRates which is inbuilt structure?

class RateData {
private:
    MqlRates rates[];
    int dataCount;

public:
    // Constructor
    RateData() {
        dataCount = 0;
    }

    // Destructor
    ~RateData() {
        ArrayFree(rates);
    }

    // Add rate data
    void addRate(const MqlRates& rate) {
        ArrayResize(rates, dataCount + 1);
        rates[dataCount] = rate;
        dataCount++;
    }

    // Get the number of stored rates
    int getRateCount() const {
        return dataCount;
    }

    // Get high, low, open, or close value by index
    double getHigh(int index) const {
        if (index >= 0 && index < dataCount) {
            return rates[index].high;
        } else {
            Print("Invalid index");
            return 0.0;
        }
    }

    double getLow(int index) const {
        if (index >= 0 && index < dataCount) {
            return rates[index].low;
        } else {
            Print("Invalid index");
            return 0.0;
        }
    }

    double getOpen(int index) const {
        if (index >= 0 && index < dataCount) {
            return rates[index].open;
        } else {
            Print("Invalid index");
            return 0.0;
        }
    }

    double getClose(int index) const {
        if (index >= 0 && index < dataCount) {
            return rates[index].close;
        } else {
            Print("Invalid index");
            return 0.0;
        }
    }

    // Clear all stored rates
    void clearRates() {
        ArrayFree(rates);
        dataCount = 0;
    }
};

// Example usage:

void OnStart() {
    // Create an instance of RateData
    RateData rateData;

    MqlRates rate1 = { /*...*/ };
    MqlRates rate2 = { /*...*/ };
    MqlRates rate3 = { /*...*/ };

    // Add rates to the RateData instance
    rateData.addRate(rate1);
    rateData.addRate(rate2);
    rateData.addRate(rate3);

    // Get the number of stored rates
    int count = rateData.getRateCount();
    Print("Number of rates: ", count);

    // Access high, low, open, close values by index
    double highValue = rateData.getHigh(1);
    double lowValue = rateData.getLow(1);
    double openValue = rateData.getOpen(1);
    double closeValue = rateData.getClose(1);

    Print("High: ", highValue);
    Print("Low: ", lowValue);
    Print("Open: ", openValue);
    Print("Close: ", closeValue);

    // Clear all stored rates
    rateData.clearRates();
}
 
Dominik Egert #:

It doesnt matter. - Here is why:

iOpen and iClose are precompiled binary functions, provided by the terminal. - Your code has to do a jmp operation to call the functions. This includes a stack push, function stack initialization and, after the function (iClose) has done what it needs to do, a result push to the calling function, ending with a jmp back to the caller, in this case your function.

Now since the result needs to be stored on the stack of (your function) the caller, a memory area needs to be assigned anyways, let it be a dedicated variable, you define, or let it be an implicit declaration by the compiler. - No difference.

There is however a difference in calling an API function once or multiple times, the compiler is unable to optimize your callings, since the result of that calling is unknown at that point, also cacheing cannot be applied, as the code of the API function is precompiled, its inner workings re unknown to your code.

So no matter how or what you do, calling a (precompiled) function will under all circumstances result in a stack push jump and return. . Hence slower than calling the function once and storing the result in an explicit declared variable.

And it is completley irrelevant what the function you are calling actually does, accessing memory (pointer-arithmetic) or calculations on its input parameters, the penalty is always the same.


(End of story and discussion.... - Haha)

ahh i see . Thanks 

 
Arpit T #:

Yes I have previously issues with iXXX functions So Is this better to MqlRates which is inbuilt structure?

Simplified version:


static MqlRates arr_rates[];

void OnStart() 
{
    // Create an instance of RateData
    if(!CopyRates(_Symbol, PERIOD_CURRENT, 0, 300, arr_rates))
    {
        printf("Error: %i", _LastError);
        return;
    }

    // Get the number of stored rates
    printf("Number of rates: %i", ArraySize(arr_rates));

    // Access high, low, open, close values by index
    const int current_rates_idx = ArraySize(arr_rates) - 1;
    Print("High: ", arr_rates[current_rates_idx].high);
    Print("Low: ", arr_rates[current_rates_idx].low);
    Print("Open: ", arr_rates[current_rates_idx].open);
    Print("Close: ", arr_rates[current_rates_idx].close);

    // Clear all stored rates
    ArrayFree(arr_rates);
}