Algorithms, solution methods, comparison of their performance - page 16

 
Реter Konow:

This is an interesting and useful proposal. Maintaining parallel records. Did it in my other solutions.

The only thing we don't know is the number of orders which will be placed by the Expert Advisor. What size should we set for the int array?

That's why I decided to take the string.


You set a margin of 100 elements and add 100 elements at a time... String has the same implementation

 
Alexandr Andreev:

I can't find theGeneric file, it seems to be an old build. So, how will the navigation principle be provided - what is the source code?

https://www.mql5.com/ru/forum/221917

Библиотека Generic классов - ошибки, описание, вопросы, особенности использования и предложения
Библиотека Generic классов - ошибки, описание, вопросы, особенности использования и предложения
  • 2017.12.07
  • www.mql5.com
С 6 декабря 2017 года в стандартную поставку MetaTrader 5 стали входить так называемые Generic-классы, реализующие эффективные алгоритмы для хранен...
 
Vasiliy Sokolov:

Peter, there is a great function called ArrayResize(). It allows you to increase the size of an array at runtime.

As an option, I was thinking about it.

1. I doubted it from the viewpoint of speed.

2. From the code point of view - more lines and more confusion.

When the array is resized, the data disappear from it (if I'm not mistaken).

So, they have to be stored in another array which must also be incremented.

This results in the back and forth rewriting I mentioned earlier.

 
Реter Konow:

As an option - thought about it.

1. In terms of speed - questioned.

2. From the code point of view - more lines and more confusion.

When the array is resized, the data disappear from it (if I'm not mistaken).

So they have to be reserved in another array, which also has to be increased.

You get the back and forth rewriting I was talking about earlier.


No, they don't.

 
Alexandr Andreev:

No, they don't.

Are you sure?
 
Реter Konow:
Are you sure?

Yes. The data doesn't disappear.

 
Реter Konow:
Are you sure?


template<typename T> 
   void ArrayAdd(T &m[], T& a)    {m[ArrayResize(m,ArraySize(m)+1,100)-1)]=a;}

in fact, it's the line.... that's causing all the fuss. adds an element to the end of the array. Everything else is trivial.

The principle is the same in the sheet but packed into a class.
 
Реter Konow:

2. In terms of code - more lines and more confusion.

   #include <Generic\ArrayList.mqh>

   CArrayList<int> collection;
   
   int value_set = 1;
   collection.Add(value_set);
 
   int index = 0;
   int value_get = -1; 
   collection.TryGetValue(index,value_get); 


How confusing, how much effort it takes to read......
Your solution is 100% prettier, more elegant and faster.

 

An approximate solution on CHashMap:

//+------------------------------------------------------------------+
//|                                                RandomTickets.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include <Generic\HashMap.mqh>
input int RandomDeals = 24000;
CHashMap<int, int> MagicsByDeals;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   for(int i = 0; i < RandomDeals; i++)
      MagicsByDeals.Add(MathRand(), MathRand());
   MagicsByDeals.Add(1337, MathRand());
   ulong msec = GetMicrosecondCount();
   bool res = MagicsByDeals.ContainsKey(1337);
   string t = (string)(GetMicrosecondCount()-msec);
   printf("Время выполнения запроса: " + t + " микросекунд");
   if(res)
      printf("Сдлека с номером 1337 была удачно найдена");
   else
      printf("Сдлека с номером 1337 не найдено");
}
//+------------------------------------------------------------------+
 
Alexandr Andreev:

in fact, it's the line.... that's causing all the fuss. adds an element to the end of the array. Everything else is trivial.

in a sheet the principle of adding is the same only packed into a class


It seems the perfect variant has been found. I ask everyone to check it.

If I was sure that the data would not disappear from the array when it was resized, the solution would be this:

//+------------------------------------------------------------------+
//|                                                      Magic 2.mq5 |
//|                                                      Peter Konow |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Peter Konow"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
int    All_magics[];
int    order_number;
int    Random_orders_of_strategy;
//+------------------------------------------------------------------+
void Save_magic(int magic)
{
 order_number++;
 //---------------------------------
 //Записываем каждый магик вместе с порядковым номером ордера.
 //---------------------------------
 ArrayResize(All_magics,order_number);
 All_magics[order_number - 1] = magic;
 //---------------------------------
}
//+------------------------------------------------------------------+
void Trading()
{
 Random_orders_of_strategy = MathRand();
 //----------------------------------------
 //Имитируем открытие неопределенного количества ордеров стратегии.
 //----------------------------------------
 for(int a1 =  0; a1 < Random_orders_of_strategy; a1++)
   {
    int this_magic = MathRand();
    //----------------------------
    Save_magic(this_magic);
    //----------------------------
   }
 //----------------------------------------
}
//+------------------------------------------------------------------+
int Get_magic(int deal_number)
{
 return(All_magics[deal_number - 1]);
}
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   Trading();
   //--------------------------
   ulong t1 = GetMicrosecondCount();
   Get_magic(1000);
   ulong t2 = GetMicrosecondCount();
   //--------------------------
   Print("Время исполнения функции Get_magic() при количестве ордеров ",Random_orders_of_strategy," равно ",t2 - t1);
   //--------------------------
   Print("Random_orders_of_strategy  ",Random_orders_of_strategy);
   Print("magic 1:  ",Get_magic(1),"  magic 2: ",Get_magic(2),"  magic 3: ",Get_magic(3));
   
  }
//+------------------------------------------------------------------+