Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools

Metatrader Forum | Forex Expert-Advisor | Broker & Forex Tools (http://www.expert-advisor.com/forum/index.php)
-   Codeschnipsel (http://www.expert-advisor.com/forum/forumdisplay.php?f=292)
-   -   Geschwindigkeitstest Systemfunktionen (http://www.expert-advisor.com/forum/showthread.php?t=5692)

Kronenchakra 29.05.17 00:00

Geschwindigkeitstest Systemfunktionen
 
Wie vermutet, sind die Abfragen von diversen Zuständen des Systems (zB AccountInfoDouble(), PositionGetDouble()) relativ langsam.
Möchte ich also das Maximum an Performance erzielen und verwende ich zB PositionGetDouble(POSITION_SL) oder ähnliches mehrmals in OnTick(), speichere ich diese Werte temporär in Variablen.
Am Beispiel AccountInfoDouble(ACCOUNT_BALANCE) sieht man den Unterschied:
Bei 1e7 Durchläufen beträgt die Differenz etwa 2 Sekunden (2094 zu 47 msec) mit meinem i7 mit 4GHz.
Code:

void OnStart()
{
  uint n=1e7;
  uint start,time;
  double v,w;
 
  start=GetTickCount();
  for(int i=0; i<n; i++)
      v=AccountInfoDouble(ACCOUNT_BALANCE);
  time=GetTickCount()-start;
  Print("Elapsed time for ",n," calculations 1: ",time," msec");

  w=AccountInfoDouble(ACCOUNT_BALANCE);
  start=GetTickCount();
  for(int i=0; i<n; i++)
      v=w;
  time=GetTickCount()-start;
  Print("Elapsed time for ",n," calculations 2: ",time," msec");
}

Des weiteren programmiert es sich einfach übersichtlicher auf diese Art.
Code:

void OnTick()
{
  PosSL=PositionGetDouble(POSITION_SL);
  PosTP=PositionGetDouble(POSITION_TP);
  .
  .
  .
}

Noch übersichtlicher wird der Programmablauf durch Auslagern in eine mqh-Datei, doch dazu mehr in einem nächsten Beitrag.

Collider 29.05.17 06:48

Hallo Otto,

ich hatte mir mal eine "Timer" Klasse gebaut mit der ich einfach im Programm Timer starten und stoppen konnte auch mehrere gleichzeitig.

Die Zeiten sind viel zu langsam bei dir.

47 msec für einfache Double ? Klingt sehr langsam. Ich meine das ich nie solche langen Zeiten hatte für einfache Sachen wie du schreibst.


Ich hänge mal den CE_Time Klasse unten an

Gruß

PHP-Code:

//+------------------------------------------------------------------+
//|                                                         Time.mqh |
//|                        Collider                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#include <Object.mqh>
#include <Arrays\List.mqh>
#include <CNodeInt.mqh>
#include <CiSingleList.mqh>
#include <CDoubleNode.mqh>


class CE_Time 
{
 private:
           
 
ulong     ticks_start       ;
 
ulong     ticks_stop        ;
 
double    result_ms         ;     
 
string    text_out          ;
 
string    timer_name        ;
 
bool      TEXT_OUT          ;
 
 
CiSingleList sList          ;
 
 public:
 
                     
CE_Time() {}                                                    ;
                    ~
CE_Time() {}                                                    ;
               
 
bool                init()                                                          ;//Initialize 
 
bool                set_Text_Out(const bool b_text_out)                             ;
 
bool                set_Size_List(const int i_size_list)                            ;   
 
bool                start(const string timer_name)                                  ;//Set Name
 
double              stop_Text_Out()                                                 ; 
 
bool                stop_SaveToList(const uint list_elements)                       ;
 
void                Print_List()                                                    ;     
 
double              Print_Avg_List()                                                ;
 
double              stop()                                                          ; 

 
//int                 getData()        {return 12;}                     ;


 
};




bool CE_Time::init(void)
{
 
  



 
  
  return 
true;
};

bool CE_Time::set_Text_Out(bool b_text_out)
{

  
TEXT_OUT b_text_out;
  return 
true ;
};



bool CE_Time::start(string s_name)
{
    
ticks_start=GetMicrosecondCount();
    
timer_name s_name;
    return 
true;
};



double CE_Time::stop_Text_Out()
{
    
ticks_stop=GetMicrosecondCount()-ticks_start;  
    
double d_result_msticks_stop/1000.0;
    
string comment=StringFormat("Total Time of %s : %.3f ms",timer_name,d_result_ms);

    
    if(
TEXT_OUT)
    {
      Print(
comment);
      
Comment(comment);
    }
    return 
d_result_ms;
};

double CE_Time::stop(void)
{
    
ticks_stop=GetMicrosecondCount()-ticks_start;  
    
double d_result_msticks_stop/1000.0;    
    return 
d_result_ms;
};

bool CE_Time::stop_SaveToList(uint ui_elements )
{
    
ticks_stop=GetMicrosecondCount()-ticks_start;  
    
//double d_result_ys= ticks;
    
sList.AddFront((int)ticks_stop);  
    if(
sList.Size() > ui_elements)
       {
         
sList.RemoveRear();
       }
    
    return 
true;    

};

void CE_Time::Print_List(void)
{
  
TRACE_CALL(_t_flag)
  
sList.PrintList(text_out);

};

double CE_Time::Print_Avg_List(void)
{
   
TRACE_CALL(_t_flag)
   
uint ui_size sList.Size();
   if(
ui_size 1) return 0;
   
uint ui_all  0;
   
   for(
uint i=0;i<ui_size;i++)
    {
     
ui_all += sList.GetValByIndex(i);

    }
    
double db_avg ui_all/ui_size ;
    
    
string comment=StringFormat("Avg Time of %s : %.3f ms with %i Elements in List",timer_name,db_avg/1000,ui_size);

    
    if(
TEXT_OUT)
    {
      Print(
comment);
      
Comment(comment);
    }
    return 
db_avg;
      
}; 



Alle Zeitangaben in WEZ +2. Es ist jetzt 20:48 Uhr.

Powered by vBulletin® Version 3.8.5 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.1
Powered by vBCMS® 2.7.0 ©2002 - 2024 vbdesigns.de
Copyright ©2009 - 2023 by Expert-Advisor.com - Das Metatrader Forum