TSNStd/Src/String/Test/ToStr.mq5

219 lines
5.1 KiB
MQL5

2026-10-09 12:34:15 -05:00
//+------------------------------------------------------------------+
//| ToStr.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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include <TSN\\Tables\\Basic.mqh>
#include <TSN\\BInt\\Def.mqh>
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
int CountDigits(const ulong v)
{
if(v < 10000000000ULL)
{
if(v < 100000ULL)
{
if(v < 100ULL)
return (v < 10ULL) ? 1 : 2;
if(v < 1000ULL)
return 3;
return (v < 10000ULL) ? 4 : 5;
}
if(v < 10000000ULL)
return (v < 1000000ULL) ? 6 : 7;
if(v < 1000000000ULL)
return (v < 100000000ULL) ? 8 : 9;
return 10;
}
if(v < 1000000000000000ULL) // 11 a 15
{
if(v < 1000000000000ULL)
return (v < 100000000000ULL) ? 11 : 12;
if(v < 100000000000000ULL)
return (v < 10000000000000ULL) ? 13 : 14;
return 15;
}
if(v < 100000000000000000ULL)
return (v < 10000000000000000ULL) ? 16 : 17;
if(v < 10000000000000000000ULL)
return (v < 1000000000000000000ULL) ? 18 : 19;
return 20;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ToValueNSigned(const long value, uchar &in[], int& w)
{
//---
if(value == 0)
{
in[w++] = '0';
return;
}
//---
ulong uvalue;
if(value < 0)
{
in[w++] = '-';
uvalue = ulong(-(value + 1)) + 1;
}
else
uvalue = ulong(value);
//---
int pos = w + CountDigits(uvalue);
w = pos;
//---
while(uvalue >= 100)
{
const ulong prev = uvalue;
uvalue /= 100ULL;
const ulong idx = (prev - (uvalue * 100ULL)) << 1;
in[--pos] = g_tsn_digit_pairs[idx + 1];
in[--pos] = g_tsn_digit_pairs[idx];
}
if(uvalue >= 10)
{
const ulong idx = uvalue << 1;
in[--pos] = g_tsn_digit_pairs[idx + 1];
in[--pos] = g_tsn_digit_pairs[idx];
}
else
in[--pos] = uchar('0' + uvalue);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ToValueNSigned2(const long value, uchar &in[], int& w)
{
//---
if(value == 0)
{
in[w++] = '0';
return;
}
//--
int pos = 0;
ulong uvalue;
if(value < 0)
{
in[w++] = '-';
uvalue = ulong(-(value + 1)) + 1;
}
else
{
uvalue = ulong(value);
}
//---
static uchar digits[24];
int n = 0;
//---
while(uvalue >= 100)
{
const ulong prev = uvalue;
uvalue /= 100ULL;
const ulong idx = (prev - (uvalue * 100ULL)) << 1ULL;
digits[n++] = g_tsn_digit_pairs[idx + 1];
digits[n++] = g_tsn_digit_pairs[idx];
}
if(uvalue >= 10)
{
const ulong idx = (uvalue << 1);
digits[n++] = g_tsn_digit_pairs[idx + 1];
digits[n++] = g_tsn_digit_pairs[idx];
}
else
{
digits[n++] = uchar('0' + uvalue);
}
//---
for(int j = n - 1; j >= 0; j--)
in[w++] = digits[j];
}
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
const int N = 1000000;
uchar arr[];
ArrayResize(arr, 32); // una vez
long vals[];
ArrayResize(vals, N);
MathSrand(GetTickCount());
for(int i = 0; i < N; i++)
vals[i] = (long)MathRand() * MathRand() * MathRand();
ulong best1 = ULONG_MAX, best2 = ULONG_MAX;
for(int rep = 0; rep < 10; rep++)
{
for(int k = 0; k < 2; k++)
{
const bool first_is_new = ((rep + k) % 2 == 0);
//---
ulong a = GetMicrosecondCount();
if(first_is_new)
{
for(int i = 0; i < N; i++)
{
int w = 0;
TSN::ToValueNSigned(vals[i], arr, w);
}
}
else
{
for(int i = 0; i < N; i++)
{
int w = 0;
TSN::ToValueNSigned2(vals[i], arr, w);
}
}
ulong t = GetMicrosecondCount() - a;
//---
if(first_is_new)
{
if(t < best1)
best1 = t;
}
else
{
if(t < best2)
best2 = t;
}
}
}
Print("Nueva (min de 10): ", best1, " us Vieja (min de 10): ", best2, " us");
}
//+------------------------------------------------------------------+