ScalerByLeo/Src/Scaler/Bench/MaxMin.mq5
2026-09-15 11:21:44 -05:00

122 lines
2.8 KiB
MQL5

//+------------------------------------------------------------------+
//| MaxMin.mq5 |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Fast(const vector& X, vector& out)
{
//---
const ulong fs = X.Size();
double max = -DBL_MAX, min = DBL_MAX;
out.Resize(fs);
for(ulong i = 0; i < fs; i++)
{
const double v = X[i];
out[i] = v;
//---
if(v < min)
min = v;
if(v > max)
max = v;
}
//--- check std
double diff = max - min;
if(diff < DBL_EPSILON)
return false;
//--- inv
diff = 1.0 / diff; // inversa
//--- final
out -= min;
out *= diff;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Normal(const vector &X, vector &out)
{
const ulong n = X.Size();
if(n == 0)
return false;
//---
const double max = X.Max();
const double min = X.Min();
//--- check std
double diff = max - min;
if(diff < DBL_EPSILON)
return false;
//--- inv
diff = 1.0 / diff; // inversa
//--- final
out = X - min;
out *= diff;
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
vector Ran()
{
vector v(1000);
for(int i = 0; i < 1000; i++)
{
v[i] = (double)MathRand();
}
return v;
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
MathSrand(10);
double vs = 0.0;
ulong a = GetMicrosecondCount();
for(int i = 1; i < 100000; i++)
{
vector v = Ran();
Normal(v, v);
vs += v[0];
}
ulong b = GetMicrosecondCount();
Print("native: ", (b - a), " vs : ", vs);
vs = 0.0;
MathSrand(10);
a = GetMicrosecondCount();
for(int i = 1; i < 100000; i++)
{
vector v = Ran();
Fast(v, v);
vs += v[0];
}
b = GetMicrosecondCount();
Print("fast: ", (b - a), " vs : ", vs);
}
//+------------------------------------------------------------------+